MySQL 基于GTID的复制
一 GTID简介
GTID是全局事务标识符,每一个提交的事务,当前执行线程都会分配给一个唯一标识符,该标识符无论在源端还是在复制环境中都是唯一的。由此可见,环境中的所有事务都和GTID一一对应,GTId格式如下:
GTID=source_id:sequnence_id
——source_id 是源服务器的唯一标识,通常为server_uuid;
——sequnence_id是事务在提交时由系统分配的一个***。
二 复制的流程
1. MASTER执行一个事务,相关进程产生一个GTID与之对应,并保存在Binlog中;
2. MASTER的Binlog发送到SLAVE,并存储在RelayLog中,SLAVE读取GTID并将其设置为gtid_next;
3. SLAVE在自己Binlog中验证读取的GTID是否有冲突,若无冲突,SLAVE则写入该GTID,并应用相应事务,且写入二进制日志;
4. SLAVE不会自己产生GTID,而是从Relay Log获取,将其设置为gtid_next,然后标识事务。
三 搭建GTId主从
1. 参数配置
相对传统模式,GTID模式需要配置一下参数:
gtid_mode=ON ——开启GTID模式
#enforce_gtid_consistency=on——开启GTID模式时,保证GTID一致性
binlog_format=row ——GTID模式强烈建议使用ROW模式
log_slave_updates=ON ——建议SLAVE记录Binlog
skip-slave-start=1 ——SLAVE不自动启动
2. 搭建主从
说明:该搭建过程是针对MASTER具有大量数据时的情况,MASTER使用innobackupex进行备份,然后在SLAVE上恢复,并初始化SLAVE。
2.1 MATSER使用innobackupex进行备份
创建测试库TEST,创建测试表t_doc
备份:
xtrabackup/bin/innobackupex --defaults-file=/mysql/my3306.cnf --slave-info --user=root --password=123qwe --socket=/mysql/3306/mysql_3306.sock --parallel=4 .
备份后产生的目录文件:2018-01-19_11-15-02
2.2 创建复制用户并授权:
> create user [email protected]'192.168.17.%'identified by 'tinayun';
> grant replication slave,replication client on *.* to [email protected]'192.168.17.%' identified by 'tinayun';
> flush privileges;
2.3 在备库进行恢复:
xtrabackup/bin/innobackupex --defaults-file=/mysql/my3307.cnf --copy-back--slave-info 2018-01-19_11-15-02
xtrabackup/bin/innobackupex --defaults-file=/mysql/my3307.cnf --apply-log--slave-info 2018-01-19_11-15-02
检查恢复状况:
2.4 使用CHANGE MASTER 配置主从:
CHANGEMASTER TO
MASTER_HOST='192.168.17.100',
MASTER_PORT=3306,
MASTER_USER='repp',
MASTER_PASSWORD='tinayun',
MASTER_AUTO_POSITION=1;
2.5 启动SLAVE复制,跳过相应GTID范围。
> start slave ;
>show slave status\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.17.100
Master_User: repp
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 93671263
Relay_Log_File:mysql-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1007
Last_Error: Error 'Can'tcreate database 'test'; database exists' on query. Default database: 'test'.Query: 'create database test'
Skip_Counter: 0
Exec_Master_Log_Pos: 194
Relay_Log_Space: 93671643
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: NULL
Master_SSL_Verify_Server_Cert:No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1007
Last_SQL_Error: Error 'Can't createdatabase 'test'; database exists' on query. Default database: 'test'. Query:'create database test'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 13783306
Master_UUID: 541d40a8-fcc3-11e7-b444-000c29273545
Master_Info_File:/mysql/mysql3307/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 180119 11:25:21
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:541d40a8-fcc3-11e7-b444-000c29273545:2-111
Executed_Gtid_Set: 541d40a8-fcc3-11e7-b444-000c29273545:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
查看状态,发现会有报错,因为SLAVE是从MASTER最看是的Binlog 执行,所以需要设置选项gtid_purged方式跳过这相应的GTID。Innobackupex备份会保留xtrabackup_info文件,该文件包含gtid_purged信息。(这一点很重要,在Group Replication 搭建时也有用到。)
跳过相应的GTID操作:
> reset master;
>set @@GLOBAL.GTID_PURGED='541d40a8-fcc3-11e7-b444-000c29273545:1-108';
>show slave status\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.17.100
Master_User: repp
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 93671263
Relay_Log_File:mysql-relay-bin.000003
Relay_Log_Pos: 454
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: 93671263
Relay_Log_Space: 93671943
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: 13783306
Master_UUID:541d40a8-fcc3-11e7-b444-000c29273545
Master_Info_File: /mysql/mysql3307/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has readall 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:541d40a8-fcc3-11e7-b444-000c29273545:2-111
Executed_Gtid_Set:541d40a8-fcc3-11e7-b444-000c29273545:1-111
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
主从模式已经正常。若还有相同报错,重启SLAVE。
(另,最好在CHANGE MASTER之前执行该操作)
2.6 验证主从复制:
主库多表t_doc 进行DML操作:
Delete 操作:
Insert 操作:
备库检查:
主Delete 操作后:
主Insert 操作后: