赞
踩
GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识,保存在mysql数据目录下的auto.cnf文件里。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。下面是一个GTID的具体形式:3E11FA47-71CA-11E1-9E33-C80AA9429562:23。
全称为: Global Transaction ID ,可简化MySQL(5.6版本之后)的主从切换以及Failover。
GTID是一个字符串类似 `3E11FA47-71CA-11E1-9E33-C80AA9429562:23`
由UUID+TID组成,
UUID代表MySQL实例,
TID代表这个实例提交的事务数量。
从MySQL5.6开始增加了强大的GTID(Global Transaction ID,全局事务ID)这个特性,用来强化数据库的主备一致性, 故障恢复, 以及容错能力。用于取代过去传统的主从复制(即:基于binlog和position的异步复制)。借助GTID,在发生主备切换的情况下,MySQL的其他slave可以自动在新主上找到正确的复制位置,这大大简化了复杂复制拓扑下集群的维护,也减少了人为设置复制position发生误操作的风险。另外,基于GTID的复制可以忽略已经执行过的事务,减少了数据发生不一致的风险。
GTID是由server_uuid和事务id组成的,即GTID=server_uuid:transaction_id。
server_uuid,是在MySQL第一次启动时自动生成并持久化到auto.cnf文件(存放在数据目录下,每台机器的server_uuid都不一样。
transaction_id,是一个从1开始的自增计数,表示在这个主库上执行的第n个事务。MySQL会保证事务与GTID之间的1:1映射,如:6ba9a76d-606b-11ea-b3ce-000c29cb3421:1
表示在以6ba9a76d-606b-11ea-b3ce-000c29cb3421为唯一标识的MySQL实例上执行的第1个数据库事务。
一组连续的事务可以用 “-” 连接的事务序号范围表示。例如:6ba9a76d-606b-11ea-b3ce-000c29cb3421:1-15
在主从复制中,尤其是半同步复制中, 由于Master 的dump进程一边要发送binlog给Slave,一边要等待Slave的ACK消息,这个过程是串行的,即前一个事物的ACK没有收到消息,那么后一个事物只能排队候着; 这样将会极大地影响性能;有了GTID后,SLAVE就直接可以通过数据流获得GTID信息,而且可以同步
主从故障切换中,如果一台MASTER down,需要提取拥有最新日志的SLAVE做MASTER,这个是很好判断,而有了GTID,就只要以GTID为准即可方便判断;而有了GTID后,SLAVE就不需要一直保存这bin-log 的文件名和Position了;只要启用MASTER_AUTO_POSITION 即可
当MASTER crash的时候,GTID有助于保证数据一致性,因为每个事物都对应唯一GTID,如果在恢复的时候某事物被重复提交,SLAVE会直接忽略
数据库 | IP |
---|---|
master | 192.268.25.140 |
slave | 192.168.25.142 |
mysql> show variables like '%gtid%'; +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | OFF | | gtid_executed_compression_period | 1000 | | gtid_mode | OFF | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+ 8 rows in set (0.01 sec) mysql> create user 'zj'@'192.168.25.142' identified by "1"; Query OK, 0 rows affected (0.01 sec) mysql> grant replication slave on *.* to 'zj'@'192.168.25.142' ; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]# vim /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid skip-name-resolve log-bin = mysql-bin #开启二进制日志 server-id = 1 gtid-mode = on #开启gtid模式 enforce-gtid-consistency = on #强制gtid一致性,开启后对特定的create table不被支持 binlog-format = row #默认为mixed混合模式,更改成row复制,为了数据一致性 log-slave-updates = 1 #从库binlog才会记录主库同步的操作日志 skip-slave-start = 1 #跳过slave复制线程 // 重启数据库服务 [root@localhost ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! // 查看master数据库状态 mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000014 | 1951 | | | b736875b-097e-11ec-b557-000c29810dc2:1-9 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
[root@localhost ~]# vim /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid skip-name-resolve server-id = 2 log-bin = mysql-bin binlog-format = row skip-slave-start = 1 log-slave-updates = 1 gtid-mode = on enforce-gtid-consistency = on [root@localhost ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
// 检查gtid模式状态 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 | | | session_track_gtids | OFF | +----------------------------------+-----------+ mysql> reset slave; Query OK, 0 rows affected (0.01 sec) mysql> change master to -> master_host='192.168.25.140' , -> master_user='zj' , -> master_password='1' , -> master_auto_position=1; Query OK, 0 rows affected, 2 warnings (0.01 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.25.140 Master_User: zj Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 154 Relay_Log_File: localhost-relay-bin.000003 Relay_Log_Pos: 367 Relay_Master_Log_File: mysql-bin.000014 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | JJ | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+-------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------------------------------+ | mysql_bin.000006 | 1389 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 | +------------------+----------+--------------+------------------+-------------------------------------------+ 1 row in set (0.00 sec)
数据库 | IP |
---|---|
master | 192.168.25.140 |
slave | 192.168.25.142 |
slave | 192.268.25.144 |
[root@localhost ~]# vim /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid skip-name-resolve server-id = 3 //第三台从服务器id 不能与前面使用过的一样 log-bin = mysql-bin binlog-format = row skip-slave-start = 1 log-slave-updates = 1 gtid-mode = on enforce-gtid-consistency = on [root@localhost ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS
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 | | | session_track_gtids | OFF | +----------------------------------+-----------+ mysql> reset slave; Query OK, 0 rows affected (0.01 sec) mysql> change master to -> master_host='192.168.25.140' , -> master_user='zj' , -> master_password='1' , -> master_auto_position=1; Query OK, 0 rows affected, 2 warnings (0.01 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.25.140 Master_User: zj Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 1776 Relay_Log_File: localhost-relay-bin.000004 Relay_Log_Pos: 454 Relay_Master_Log_File: mysql-bin.000014 Slave_IO_Running: Yes Slave_SQL_Running: Yes mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | JJ | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+ | mysql_bin.000008 | 750 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10, ba11a7ff-097f-11ec-9272-000c29959565:1, ddd6f885-0a26-11ec-81cc-000c29f2278e:1 | +------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+
数据库 | IP |
---|---|
master | 192.168.25.140 |
master | 192.168.25.142 |
slave | 192.168.25.144 |
//主库1 mysql> show master status; +------------------+----------+--------------+------------------+-------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------------------------------+ | mysql-bin.000014 | 2128 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 | +------------------+----------+--------------+------------------+-------------------------------------------+ 1 row in set (0.00 sec) 主库2 mysql> show master status; +------------------+----------+--------------+------------------+-------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------------------------------+ | mysql-bin.000002 | 154 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 | +------------------+----------+--------------+------------------+-------------------------------------------+ 1 row in set (0.00 sec)
//检查gtid模式状态 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 | | | session_track_gtids | OFF | +----------------------------------+-----------+ mysql> reset slave; Query OK, 0 rows affected (0.01 sec)
//配置主库1 //下面两条命令不执行,后续会报错 mysql> set global master_info_repository='table'; Query OK, 0 rows affected (0.00 sec) mysql> set global relay_log_info_repository='table'; Query OK, 0 rows affected (0.00 sec) mysql> change master to -> master_host='192.168.25.140' , //这里是主库IP -> master_user='zj' , -> master_password='1' , -> master_log_file='mysql-bin.000014' -> master_log_pos=2128 -> for channel 'master-1'; //创建隧道 配置主库2 mysql> change master to -> master_host='192.168.25.142' , -> master_user='zj' , -> master_password='1' , -> master_log_file='mysql-bin.000002' -> master_log_pos=154 -> for channel 'master-2'; //创建隧道 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.25.140 Master_User: zj Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 2128 Relay_Log_File: localhost-relay-bin-master@002d1.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000014 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: 2128 Relay_Log_Space: 544 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: b736875b-097e-11ec-b557-000c29810dc2 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: Executed_Gtid_Set: baf6e1ae-0a32-11ec-bf3d-000c29f2278e:1 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: master-1 Master_TLS_Version: *************************** 2. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.25.142 Master_User: zj Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 154 Relay_Log_File: localhost-relay-bin-master@002d2.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000002 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: 154 Relay_Log_Space: 544 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: 2 Master_UUID: ba11a7ff-097f-11ec-9272-000c29959565 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: Executed_Gtid_Set: baf6e1ae-0a32-11ec-bf3d-000c29f2278e:1 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: master-2 Master_TLS_Version: 2 rows in set (0.00 sec)
主库1上创建数据库ll mysql> create database ll; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ll | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) 主库2上创建数据库mm mysql> create database mm; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | mm | +--------------------+ 5 rows in set (0.00 sec) // 从库上开启从服务后查看 mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ll | | mysql | | performance_schema | | sys | | mm | +--------------------+ 6 rows in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。