目录
0. 前言
1. 准备与下载
2. 安装MySQL主实例
3. 安装MySQL备实例
4. 主备实例建立连接
0. 前言
先给MySQL打个广告简单介绍一下
MySQL作为一个RDBMS数据库的优势:
MySQL集群的优势:
MySQL基础语法介绍见菜鸟网站: https://www.runoob.com/mysql/mysql-tutorial.html
前方预警,干货来了!!!
1. 准备与下载
主从集群,顾名思义就是一个master主机,多个slave从属机,为了简便,我们这里只用两台linux服务器做集群当演练,一个当master,一个当slave,后续如需要集群扩容,可以多加slave机器,操作与之相同
登陆MySQL官网,进入社区版https://dev.mysql.com/downloads/mysql/5.7.html#downloads,选择linux 通用版,这里我使用下载好的5.7.19版
把该安装包分别上传到主(192.168.xxx.133),备(192.168.xxx.134) 实例所在的服务器/opt中,需提前安装好jdk
2. 安装MySQL主实例
步骤1. 把下载好的安装包上传到主实例服务器(192.168.xxx.133),并切换到root用户
步骤2. 解压压缩包,并把压缩包改名为mysql
步骤3. 添加mysql用户和组
步骤4. 创建mysql目录并修改属组和权限
步骤5. 执行下面命令初始化数据库
打印下面的信息代表初始化成功,记住密码 qCf,j5bu-UfO
2019-08-02T02:52:12.927579Z 1 [Note] A temporary password is generated for root@localhost: qCf,j5bu-UfO
步骤6. 检查/opt/mysql/data/assettree-dbsvr下是否有下面文件
auto.cnf ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema sys
步骤7. 在/opt/mysql/data/assettree-dbsvr下创建my.cnf文件,并改权限为664,权限太大mysql会忽略这个文件,属组改为mysql:mysql
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql/data/assettree-dbsvr
port = 3306
socket = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.sock
log-error = /opt/mysql/data/assettree-dbsvr/mysql.err
pid-file = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.pid
character-set-server=utf8
tmpdir = /opt/mysql/tmp
binlog_format = row
default-storage-engine=INNODB
max_connections=1000
max_allowed_packet = 16M
innodb_buffer_pool_size = 1G
server-id = 100
log-bin = /opt/mysql/data/assettree-dbsvr/mysql_bin
log-bin-index = /opt/mysql/data/assettree-dbsvr/mysql_bin.index
relay-log = /opt/mysql/data/assettree-dbsvr/relay_bin.index
relay-log-index = /opt/mysql/data/assettree-dbsvr/relay_bin.index
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 16M
[client]
port = 3306
socket = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.sock
default-character-set=utf8
步骤8. 切换到mysql用户,映射my.cnf文件启动mysql,由于会进程一直跑,故可放到后台
这里如果需要关闭mysql,可以用 /opt/mysql/bin/mysqladmin -uroot -p -h127.0.0.1 shutdown, 也可以直接杀死进程,但 一般不推荐
步骤9. 切换到root用户,用之前生成的密码登陆mysql
步骤10. 执行下面mysql,进行更改密码为root,创建用户和刷新权限
3. 安装MySQL备实例
备实例安装和主实例前七步是一致的
步骤1. 把下载好的安装包上传到主实例服务器(192.168.xxx.134),并切换到root用户
步骤2. 解压压缩包,并把压缩包改名为mysql
步骤3. 添加mysql用户和组
步骤4. 创建mysql目录并修改属组和权限
步骤5. 执行下面命令初始化数据库
打印下面的信息代表初始化成功,记住密码 Q<(.!M5ppwn!
2019-08-02T02:52:12.927579Z 1 [Note] A temporary password is generated for root@localhost: Q<(.!M5ppwn!
步骤6. 检查/opt/mysql/data/assettree-dbsvr下是否有下面文件
auto.cnf ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema sys
步骤7. 在/opt/mysql/data/assettree-dbsvr下创建my.cnf文件,并改权限为664,权限太大mysql会忽略这个文件,属组改为mysql:mysql
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql/data/assettree-dbsvr
port = 3306
socket = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.sock
log-error = /opt/mysql/data/assettree-dbsvr/mysql.err
pid-file = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.pid
character-set-server=utf8
tmpdir = /opt/mysql/tmp
binlog_format = row
default-storage-engine=INNODB
max_connections=1000
max_allowed_packet = 16M
innodb_buffer_pool_size = 1G
server-id = 200 #每一个数据库id需要不同
log-bin = /opt/mysql/data/assettree-dbsvr/mysql_bin
log-bin-index = /opt/mysql/data/assettree-dbsvr/mysql_bin.index
relay-log = /opt/mysql/data/assettree-dbsvr/relay_bin.index
relay-log-index = /opt/mysql/data/assettree-dbsvr/relay_bin.index
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
read_only=1 #需添加
[mysqldump]
quick
max_allowed_packet = 16M
[client]
port = 3306
socket = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.sock
default-character-set=utf8
步骤8. 切换到mysql用户,切记一定要切到mysql用户,映射my.cnf文件启动mysql,由于会进程一直跑,故可放到后台
这里如果需要关闭mysql,可以用 /opt/mysql/bin/mysqladmin -uroot -p -h127.0.0.1 shutdown, 也可以直接杀死进程,但 一般不推荐
步骤9. 切换到root用户,用之前生成的密码登陆mysql
步骤10. 执行下面mysql,进行更改密码为root,创建用户和刷新权限
4. 主备实例建立连接
步骤1. 分别以管理员身份登陆主备数据库
步骤2. 在主数据库查看master 状态,记录file和position,下面会用
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 | 768 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
步骤3. 在备数据库执行命令,建立主备连接,并开启slave,查看slave状态
在展开的日志中找到
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
即代表成功!
可进行测试主备集群是否搭建成功,如果某个数据库DDL,DML操作等都能在其他数据库里立马体现出来,则证明真的成功 了!