1. 准备工作

(本文永久地址:http://woymk.blog.51cto.com/10000269/1922786)

配置环境: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
修改或添加下面这几行:
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 #忽略不同步的库

保存后重启

/etc/init.d/mysqld restart

运行mysql客户端
mysql -uroot -p

创建同步账号

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

锁表,不让数据写入

flush tables with read lock;
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)

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

备份数据库

在mysql1上执行
mysqldump -uroot -p db1 > back.sql;

把备份好的数据拷贝到从服务器

scp back.sql 192.168.1.2:~


3. 在server2操作

vi /etc/my.cnf
修改或增加:
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

把在server1上备份的数据导入server2

在server2上执行
[root@server2 ~]# mysqldump -uroot -p db1 < back.sql

运行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

取消server1上数据库的锁定

mysql> unlock tables;

执行以下命令
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. 主主复制测试

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

具体过程略,请自行请参考上一篇 mysql5.7主从配置 里的方法。