基于主从演变成为双主。
一、master1的配置
1、修改配置项,基于原来的Gtid主动模式设置
vim /etc/my.cnf 修改内容为:
log-bin=mysql-bin
Git_mode=ON
server-id=158
enforce_gtid_consistency=1
2、重启master1的服务生效配置
systemctl restart mysql
3、登陆系统,授权远程登陆用户
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'rep01'@'192.168.31.%'
4、导出数据库(mysqldump)
先quit;退出。
执行导出语句:
mysqldump -uroot -pbgx --all-databases --single-transaction --master-data=1 --flush-logs > /root/backup/db-$(date +%F)-all.sql
5、将数据库发送给master2.
scp db-2018-08-24-all.sql root@slave:/root/backup/
在master2上面可以看到拷贝过去的数据:
------------
二、master2 的配置
1、和master1一样,修改配置文件。
[mysqld]
log-bin=mysql-bin
server-id=175
basedir=/soft/mysql
datadir=/soft/mysql/data
gtid_mode = ON
enforce_gtid_consistency=1
2、重启服务,配置生效。
systemctl restart mysq
3、授权
设置相同的授权账号密码,与master1保持一致。
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'rep01'@'192.168.31.%'
4、恢复master设置,导入数据
mysql> reset master;
[root@MiWiFi-R1CL-srv backup]# mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"
有一个错误,这个错误我们也不陌生了:
[root@MiWiFi-R1CL-srv backup]# mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1840 (HY000) at line 24 in file: '/root/backup/db-2018-08-24-all.sql': @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
1、到master1上执行 reset master;
2、再导出master1的数据库。执行第一点的第4步。
mysqldump -uroot -pbgx --all-databases --single-transaction --master-data=1 --flush-logs > /root/backup/db-$(date +%F)-all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
查看结果:
总用量 788
-rw-r--r--. 1 root root 804118 8月 24 11:55 db-2018-08-24-all.sql
3、参照第一点的第5步。
4、开始执行第 二点的第4步。
以上错误还是存在。。。为什么?
ERROR 1776 (HY000) at line 30 in file: '/root/backup/db-2018-08-24-all.sql': Parameters MASTER_LOG_FILE, MASTER_LOG_POS, RELAY_LOG_FILE and RELAY_LOG_POS cannot be set when MASTER_AUTO_POSITION is active.
以上无法解决,那么重新初始化数据库。方法参考:
http://blog.51cto.com/13683138/2163647 中的“4、导入备份的数据到slave。中的解决方法”
初始化语句:
/soft/mysql/bin/mysqld --initialize --user=mysql --basedir=/soft/mysql --datadir=/soft/mysql/data
完成后,再执行:
[root@MiWiFi-R1CL-srv mysql]# mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1840 (HY000) at line 24 in file: '/root/backup/db-2018-08-24-all.sql': @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
检查错误:
查看master1中的数据库master 状态
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
并么有reset master成功。
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
再次执行一次reset master 后再导出数据。
mysqldump -uroot -pbgx --all-databases --single-transaction --master-data=1 --flush-logs > /root/backup/db-$(date +%F)-all.sql
scp db-2018-08-24-all.sql root@slave:/root/backup/
在master2的服务器上面,查看master状态:
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
导入数据;
mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"
[root@MiWiFi-R1CL-srv data]# mysql -uroot -pbgx -e "source /root/backup/db-2018-08-24-all.sql"
mysql: [Warning] Using a password on the command line interface can be insecure.
此步骤执行成功。。
---------------
5、清理从主库的二进制日志
mysql> reset master;
Query OK, 0 rows affected (0.00 sec)
6、changemasterto master1
mysql> change master to master_host='master', master_user='rep01',master_password='Rep01', master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
7、启动 slave 角色查看状态
mysql > start slave;
三、配置主mastser1 changemaster
1、刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2、changemaster
change master to master_host='slave', master_user='rep01',master_password='Rep01', master_auto_position=1;
mysql> change master to master_host='slave', master_user='rep01',master_password='Rep01', master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
3、启动slave角色
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
4、查看状态
这里有个错误,正常应该是yes,不应为connectiong。所以,去看看
是否有其他问题。
查找问题入口:
查看日志错误,发现是防火墙的原因。
关闭 master2的防火墙,就可以了。
[root@MiWiFi-R1CL-srv data]# systemctl stop firewalld
[root@MiWiFi-R1CL-srv data]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
再查看master1 的状态:
四、对 master1与master2进行交换添加数据查看状态结果
1、master1 添加数据
mysql> create database master11111DB;
查看master2已经有了master1创建的数据库。
2、通过master2创建数据库,看master1是否同步。
mysql> create database master22222DB;
查看master1 已经有了master2创建的数据库。
以上,双主同步设置完成。后续我们在进行 多源复制,M-M -S-S