GTID的概念
从 MySQL 5.6.5 开始新增了一种基于 GTID 的复制方式。通过 GTID 保证了每个在主库上提交的事务在集群中有一个唯一的ID。这种方式强化了数据库的主备一致性,故障恢复以及容错能力。
什么是GTID
一个GITD由两部分组成的,分别是source_id 和transaction_id,GTID=source_id:transaction_id,其中source_id就是执行事务的主库的server-uuid值,server-uuid值是在mysql服务首次启动生成的,保存在数据库的数据目录中,在数据目录中有一个auto.conf文件,这个文件保存了server-uuid值(唯一的)。
而transaction_id(事务id)则是从1开始自增的序列,表示这个事务是在主库上执行的第几个事务,Mysql会保证这个事务和GTID是一比一的关系。
例如:ce243af7-e6dd-11e8-822d-000c29d5a4b5:1-3
GTID的工作原理
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。
需求说明
配置GTID主从
环境说明
ip | 服务器类型 |
---|---|
172.16.11.22 | mysql-master端 |
172.16.11.21 | mysql-slave端 |
操作步骤
- 172.16.11.22
①.修改配置文件,添加如下两行
[root@lizihan ~]# vim /etc/my.cnf
[mysqld]
datadir=/opt/data
basedir = /usr/local/mysql
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
server-id=5 //数据库服务器的唯一标识符,主服务器的id要比从服务器小
gtid-mode=on //开启gtid模式
enforce_gtid_consistency=on //强制gtid一致性,用于保证启动gtid后事务的安全
log_bin=master-log //开启bin-log日志
log-slave-updates=1 //在mysql5.7之前主从架构上使用gtid模式,必须使用此选项
binlog_format=row //建以row
skip_slave_start=1
②.创建同步账号给从数据库同步用
mysql> create user 'xixi'@'172.16.11.21' identified by 'xixi123!'; //创建账号
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'xixi'@'172.16.11.21' identified by 'xixi123!'; //同步授权
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges; //刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+-------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+------------------------------------------+
| master-log.000001 | 856 | | | ce243af7-e6dd-11e8-822d-000c29d5a4b5:1-3 |
+-------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.01 sec)
- 172.16.11.21
①.修改配置文件,添加如下两行
[root@lizihan ~]# vim /etc/my.cnf
[mysqld]
datadir=/opt/data
basedir = /usr/local/mysql
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
server-id=10
log_bin=slave-log
gtid-mode=on
enforce_gtid_consistency=true
binlog_format=row
skip_slave_start=1
②.配置并启动gtid主从复制
[root@lizihan ~]# mysql -uroot -plzh123!
mysql> change master to
-> master_host='172.16.11.22',
-> master_user='xixi',
-> master_password='xixi123!',
-> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.28 sec)
mysql> start slave;
Query OK, 0 rows affected (0.08 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.11.22
Master_User: xixi
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000001
Read_Master_Log_Pos: 856
Relay_Log_File: lizihan-relay-bin.000002
Relay_Log_Pos: 1071
Relay_Master_Log_File: master-log.000001
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: 856
Relay_Log_Space: 1280
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: 5
Master_UUID: ce243af7-e6dd-11e8-822d-000c29d5a4b5
Master_Info_File: /opt/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: ce243af7-e6dd-11e8-822d-000c29d5a4b5:1-3 //接收的事务
Executed_Gtid_Set: ce243af7-e6dd-11e8-822d-000c29d5a4b5:1-3 //执行成功的事务
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
结果测试
- 172.16.11.22
在主库上写入数据
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
- 172.16.11.21
在从库上查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
5 rows in set (0.00 sec)