mysql5.7主主(双主)复制

1. 准备工作

平远网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联公司2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

(本文永久地址: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主从配置 里的方法。


网站名称:mysql5.7主主(双主)复制
网站URL:http://hbruida.cn/article/piihhp.html