MySQL基于GTID的主从复制
1、什么是GTID?
1、全局唯一,一个事务对应一个GTID 2、替代传统的binlog+pos复制;使用master_auto_position=1自动匹配GTID断点进行复制 3、MySQL5.6开始支持 4、在传统的主从复制中,slave端不用开启binlog;但是在GTID主从复制中,必须开启binlog 5、slave端在接受master的binlog时,会校验GTID值 6、为了保证主从数据的一致性,多线程同时执行一个GTID 2、组成 Master_UUID:序列号举例:ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5ceb0ca3d-8366-11e8-ad2b-000c298b7c9a其实就是master的uuid值;1-5是序列号,每次一个事务完成都会自增1,也就是说下一次为1-6。 3、工作原理 1、master更新数据时,会在事务前产生GTID,一同记录到binlog日志中。 2、slave端的i/o 线程将变更的binlog,写入到本地的relay log中。 3、sql线程从relay log中获取GTID,然后对比slave端的binlog是否有记录。 4、如果有记录,说明该GTID的事务已经执行,slave会忽略。 5、如果没有记录,slave就会从relay log中执行该GTID的事务,并记录到binlog。 6、在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描 4、GTID主从配置 版本:MySQL5.7 配置master vim /etc/my.cnf [client] socket=/usr/local/mysql/mysql.sock [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data user=mysql pid-file=/usr/local/mysql/data/mysqld.pid log-error=/usr/local/mysql/data/mysql.err socket=/usr/local/mysql/mysql.sock port=3306 server-id=1 gtid-mode=ON enforce-gtid-consistency=ON server-id=1 binlog_format=row log-bin=/usr/local/mysql/data/mysql-bin systemctl restart mysqld firewall-cmd --add-port=3306/tcp --permanent firewall-cmd --reload
配置slave
vim /etc/my.cnf [client] socket=/usr/local/mysql/mysql.sock [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data user=mysql pid-file=/usr/local/mysql/data/mysqld.pid log-error=/usr/local/mysql/data/mysql.err socket=/usr/local/mysql/mysql.sock port=3306 server-id=2 gtid-mode=ON enforce-gtid-consistency=ON server-id=2 binlog_format=ROW log-bin=/usr/local/mysql/data/mysql-bin log_slave_updates=ON skip-slave-start=1 systemctl restart mysqld firewall-cmd --add-port=3306/tcp --permanent firewall-cmd --reload
master授权配置
mysql -uroot -p mysql> grant replication slave on *.* to ‘rep‘@‘10.0.0.%‘ identified by ‘123‘; mysql> flush privileges;
slave配置同步
mysql -uroot -p mysql> change master to master_host=‘10.0.0.132‘, master_user=‘rep‘,master_password=‘123‘,master_port=3306,master_auto_position=1; mysql> start slave;
查看slave的状态
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.132 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 635 Relay_Log_File: slave-relay-bin.000005 Relay_Log_Pos: 848 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 635 Relay_Log_Space: 1308 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a Master_Info_File: /usr/local/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4 Executed_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
出现这两个yes表示同步成功
通过slave的状态信息,可以看到GTID的值、Matser_UUID等信息
查看master状态
mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000003 | 635 | | | ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-4 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
注意对比slave端,Executed_Gtid_Set的值应该是一样的。
5、验证主从
master上
mysql> create database test01; Query OK, 1 row affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000003 | 800 | | | ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec) slave上 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test01 | +--------------------+ 5 rows in set (0.07 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.132 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 800 Relay_Log_File: slave-relay-bin.000005 Relay_Log_Pos: 1013 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 800 Relay_Log_Space: 1473 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a Master_Info_File: /usr/local/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5 Executed_Gtid_Set: ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) 需要注意的是,GTID的值在完成一次事务后,变成了ceb0ca3d-8366-11e8-ad2b-000c298b7c9a:1-5(自增1) 6、排障 思路 a、确保master开放3306端口 b、最好关闭selinux c、master上授权同步,slave上change master命令指定master的信息不要写错 d、UUID问题 如果你出现了上图所示的问题,表示你的master和slave的UUID是一样的,一般这种情况多出现于克隆虚拟机 解决办法: 找到slave上的MySQL数据目录下的auto.cnf文件(这个文件其实是自动生成的mysql服务器的UUID值),将它删除,然后重启MySQL,然后MySQL会重新生成一个UUID。然后停掉slave,重新开启就可以了(我的mysql的数据目录是在/usr/local/mysql/data下,详情查看my.cnf配置文件) cd /usr/local/mysql/data rm -f auto.cnf systemctl restart mysql [ data]# cat auto.cnf [auto] server-uuid=020c7f26-be57-11e8-8e2d-000c29b63bad
通过cat命令查看该文件,发现UUID已经改变
mysql -uroot -p mysql> stop slave; mysql> start slave;
e、总结
排障过程中,注意需要停掉slave,做完修改之后在开启,否则你的修改可能是不会生效的。
相关推荐
林大鹏 2020-06-10
emmm00 2020-11-17
暗夜之城 2020-11-11
疯狂老司机 2020-09-08
yishouwangnian 2020-08-18
Jaystrong 2020-07-28
hxw0 2020-07-26
tangjianft 2020-07-04
wangshuangbao 2020-07-04
CharlesYooSky 2020-06-25
爱文学更爱编程 2020-06-20
KFLING 2020-06-13
TMD咯MySQL 2020-06-11
勇往直前 2020-06-09
weiguoxin 2020-06-09
hevenue 2020-06-08
msmysql 2020-06-06
javashixisheng 2020-06-05
minggehenhao 2020-06-02