首页 > 数据库 > MySQL > mysql GTID
一.GTID概念
全局事物标识:global transaction identifieds
每次提交事务都会生成一个GTID,一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。
GTID=source_id:sequence_id
source_id使用服务器的uuid来进行标志(配置文件为数据目录下的auto.cnf,在集群中保持唯一)
sequence_id系统顺序分配的序列号,连续并且有序
二.工作原理
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。
备注:
1、slave在接受master的binlog时,会校验master的GTID是否已经执行过(一个服务器只能执行一次)。
2、为了保证主从数据的一致性,多线程只能同时执行一个GTID。
三.GTID vs 传统复制:
-优势
1、更简单的实现failover,不用以前那样在需要找log_file和log_Pos。
2、更简单的搭建主从复制。
3、比传统复制更加安全。
4、GTID是连续没有空洞的,因此主从库出现数据冲突时,可以用添加空事物的方式进行跳过。
-限制
1. 不支持非事务引擎(从库报错,stopslave; start slave; 忽略)
2. 不支持create table ... select 语句复制(主库直接报错)
3. 不允许在一个SQL同时更新一个事务引擎和非事务引擎的表
4. 在一个复制组中,必须要求统一开启GTID或是关闭GTID
5. 开启GTID需要重启(5.7中不需要)
6. 开启GTID后,就不在使用原来的传统的复制方式
7. 对于create temporary table 和drop temporary table语句不支持
8. 不支持sql_slave_skip_counter
四.环境搭建
1.修改配置文件
主库备库必须同时开启或关闭
gtid_mode=on
#开启gtid
log_slave_updates=1
enforce_gtid_consistency=1
#保证gtid的一致性
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000008
Position: 407
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 8e51bb43-3e02-11e8-a7be-000c29f55484:1
2.xtrabackup同步数据库
(1).备份
innobackupex --defaults-file=/etc/my5.7_3306.cnf --user=root --password=root --no-timestamp /mysql/backup/`date +%H-%M`.dbname
(2).拷贝到备库
(3).恢复
innobackupex --defaults-file=/etc/my5.7_3306.cnf --user=root --apply-log /mysql/backup/11-31.dbname/
innobackupex --defaults-file=/etc/my5.7_3306.cnf --user=root --copy-back /mysql/backup/11-31.dbname/
(4).开启mysql
3.开启GTID主从复制
(1).查看从库xtrabackup_info
cat xtrabackup_info
uuid = 0fdfc036-46bb-11e8-8aac-000c29f55484
name =
tool_name = innobackupex
tool_command = --defaults-file=/etc/my5.7_3306.cnf --user=root --password=... --no-timestamp /mysql/backup/13-56.dbname
tool_version = 2.4.9
ibbackup_version = 2.4.9
server_version = 5.7.21-log
start_time = 2018-04-23 13:56:20
end_time = 2018-04-23 13:56:27
lock_time = 0
binlog_pos = filename 'mysql-bin.000008', position '1166',
GTID of the last change '8e51bb43-3e02-11e8-a7be-000c29f55484:1-4'
innodb_from_lsn = 0
innodb_to_lsn = 4289362
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N
(2).从库执行
mysql> set GLOBAL GTID_PURGED='
8e51bb43-3e02-11e8-a7be-000c29f55484:1-4';
mysql> change master to master_host='109.115.12.40',master_port=3306,master_user='REPL_USER',master_password='repl1234',master_auto_position=1;
mysql> start slave;
(3).确认slave状态
mysql> show variables like '%gtid%';
+----------------------------------+------------------------------------------+
| Variable_name | Value |
+----------------------------------+------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | 8e51bb43-3e02-11e8-a7be-000c29f55484:1-4 |
| session_track_gtids | OFF |
+----------------------------------+------------------------------------------
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 109.115.12.40
Master_User: REPL_USER
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 1419
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 667
Relay_Master_Log_File: mysql-bin.000008
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: 1419
Relay_Log_Space: 870
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: 12403306
Master_UUID: 8e51bb43-3e02-11e8-a7be-000c29f55484
Master_Info_File: mysql.slave_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: 8e51bb43-3e02-11e8-a7be-000c29f55484:5
Executed_Gtid_Set: 8e51bb43-3e02-11e8-a7be-000c29f55484:1-5
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
4.GTID复制模式同样会有异步复制和半同步复制之分
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15412087/viewspace-2153305/ ,如需转载,请注明出处,否则将追究法律责任。