1. 准备工作

配置环境:centos6.9,mysql5.7

先安装、配置好两台MySQL服务器

server1 IP:192.168.1.1

server2 IP:192.168.1.2

mysql的安装请参考之前的文章:

LAMP环境搭建(centos6.9+apache2.4+mysql5.7+php7.1)

2. 在server1上操作

vi /etc/my.cnf

修改或添加下面这几行:

[mysqld]
server-id=1
log-bin=mysql-bin # 启用二进制日志
auto-increment-increment = 2 #每次增长2
auto-increment-offset = 1 #设置自动增长的字段的偏移量
#两个可选参数(2选1):
binlog-do-db=db1,db2 #需要同步的库
binlog-ignore-db=db1,db2 #忽略不同步的库

保存后重启MySQL

/etc/init.d/mysqld restart

运行mysql客户端

mysql -uroot -p

创建同步账号

grant replication slave on *.* to 'repl2'@'192.168.1.2' identified by '1234';
flush privileges;

锁表,不让数据写入

show master status;


mysql> show master status;
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
| mysql-bin.000001 | 613 | | mysql | |
1 row in set (0.00 sec)

记录下二进制日志文件名和位置

3. 在server2操作

vi /etc/my.cnf

修改或增加:

[mysqld]
server-id=2 #这个数值不能和主一样
log-bin=mysql-bin # 启用二进制日志
auto-increment-increment = 2 #每次增长2
auto-increment-offset = 2 #设置自动增长的字段的偏移量
#可选参数(2选1,这两个参数设置成和主一样):
replicate-do-db=db1,db2
replicate-ignore-db=db1,db2

保存后重启

/etc/init.d/mysqld restart

运行mysql客户端

mysql -uroot -p

创建同步账号

grant replication slave on *.* to 'repl1'@'192.168.1.1' identified by '1234';
flush privileges;

show master status;


mysql> show master status;
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
| mysql-bin.000001 | 613 | | mysql | |
1 row in set (0.00 sec)

记录下二进制日志文件名和位置

执行以下命令

stop slave;
change master to master_host='192.168.1.1',master_user='repl2',master_password='1234',master_log_file='mysql-bin.000001',master_log_pos=613;
(master_log_file和master_log_pos填上刚才记录下的二进制日志文件名和位置)


start slave;

查看从服务器的状态:

show slave status\G


mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: repl2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 613
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

4. 回到server1上操作

运行mysql客户端

mysql -uroot -p

执行以下命令

stop slave;
change master to master_host='192.168.1.2',master_user='repl1',master_password='1234',master_log_file='mysql-bin.000001',master_log_pos=613;
(master_log_file和master_log_pos填上刚才记录下的二进制日志文件名和位置)


start slave;

查看从服务器的状态:

show slave status\G


mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: repl1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 613
Relay_Log_File: server1-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

5. 主主复制测试

经测试,主主复制配置成功。

https://cloud.tencent.com/developer/article/1401402

https://cloud.tencent.com/search/%E4%B8%BB%E4%B8%BB%E5%A4%8D%E5%88%B6/1_1