环境介绍:
Centos7 | 192.168.1.94 | MySQL-5.7 |
Centos7 | 192.168.1.95 | MySQL-5.7 |
实现方式
通过GTID来实现双主复制
192.168.1.94服务器中操作:
root@server1 ~]# vim /etc/my.cnf
[mysqld]
#bin-log 开启二进制日志
server_id=94
log_bin=mysql-bin
## GTID
gtid_mode=ON
enforce_gtid_consistency=true
重启MySQL使配置文件生效
[root@server1 ~]# systemctl restart mysqld
[root@server1 ~]# ss -antp | grep 3306 #确保端口启动成功
LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=7697,fd=32))
mysql> show master status; #查看二进制日志是否开启
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> select @@server_uuid; #查看GTID的UUID
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| e073964e-eba4-11e8-9eb4-000c2978fbe2 |
+--------------------------------------+
1 row in set (0.00 sec)
创建一个数据同步用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'cmzw'@'192.168.1.%' IDENTIFIED BY 'Cmzw.2018';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> select User,Host from mysql.user;
+---------------+-------------+
| User | Host |
+---------------+-------------+
| cmzw |192.168.1.%|
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-------------+
4 rows in set (0.00 sec)
如果是已经使用过一段时间的数据库,先把所有的数据入到数据库中,本文中的数据库是没有进行使用。所以就没有进行备份。
导出数据
mysqldump --user=root --password=Long110427. --flush-logs --master-data=2 --lock-all-tables --all-databases > /tmp/aa.sql
scp /tmp/aa.sql 192.168.1.95:/tmp/aa.sql
导入数据
mysql> SOURCE /tmp/aa.sql
192.168.1.95服务器中的操作
开启二进制日志,启动GTID。
[root@server2 ~]# vim /etc/my.cnf
[mysqld]
server_id=95
log_bin=mysql-bin
gtid_mode=on
enforce_gtid_consistency=true
重启MySQL使配置文件生效
[root@server1 ~]# systemctl restart mysqld
[root@server1 ~]# ss -antp | grep 3306 #确保端口启动成功
LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=7697,fd=32))
mysql> select @@server_uuid; #查看GTID的UUID
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| e073964e-eba4-11e8-9eb4-000c2978fbe2 |
+--------------------------------------+
1 row in set (0.00 sec)
连接192.168.1.94服务器的数据库
恢复数据,使两个数据库的数据一致
作者在95数据库中没有创建这个用户为了使两个数据库完全一致,所以创建同样的用户
GRANT REPLICATION SLAVE ON *.* TO 'cmzw'@'192.168.1.%' IDENTIFIED BY 'Cmzw.2018';
如果你是使用数据恢复的方式操作的话可以不创建这个账户。
数据恢复,把刚才scp到本地的SQL文件恢复到本地数据库中
mysql> SOURCE /tmp/aa.sql
查询的时候注意SQL线程和IO线程是否Yes
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.94',
-> MASTER_USER='cmzw',
-> MASTER_PASSWORD='Cmzw.2018',
-> MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.94
Master_User: cmzw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 447
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 660
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
返回192.168.1.94服务器进行连接192.168.1.95数据库。(因为是双主复制)
注意:如果创建的用户是不是以192.168.1.%的权限,还需要创建一个可以连接192.168.1.95的账户
SQL线程和IO线程是yes主从复制已经完成了。
连接192.168.1.95的数据库
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.95',
-> MASTER_USER='cmzw',
-> MASTER_PASSWORD='Cmzw.2018',
-> MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.95
Master_User: cmzw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: server1-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table: