GTID原理:
当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中。
binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,即告诉Slave,下一个要执行的GTID值。
sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID。
如果有记录,说明该GTID的事务已经执行,slave会忽略。
如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,
在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行。
在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
环境要求:
数据库角色 | IP | 系统版本与应用 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.225.128 | redhat7 mysql-5.7 | 无数据 |
从数据库 | 192.168.225.129 | redhat7 mysql-5.7 | 无数据 |
修改主数据库配置文件
[root@hxdserver ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
#GTID:
server-id=128 //服务器id
gtid-mode=on //开启GTID模式
enforce-gtid-consistency=on //强制gtid一致性,开启后对于特定create table不被支持
#binlog
log-bin=master-binlog
log-slave-updates=1
binlog-format=row //强烈建议,其他格式可能造成数据不一致
#relay log
skip-slave-start=1
修改从数据库配置文件
[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
user = mysql
skip-name-resolve
#GTID
gtid-mode=on
enforce-gtid-consistency=on
server-id=129
#binglog
log-bin=slave-binglog
log-slave-updates=1
binlog-format=row
#relay log
skip-slave-start=1
- 在主数据库里创建一个同步账号授权给从库使用
mysql> create user 'repl'@'192.168.225.129' identified by 'repl123.';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.225.129';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
192.168.225.129(从库)
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.225.128',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl123.',
-> MASTER_PORT=3306,
-> MASTER_AUTO_POSITION = 1;
Query OK, 0 rows affected, 2 warnings (0.06 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.225.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-binlog.000002
Read_Master_Log_Pos: 1019
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 1240
Relay_Master_Log_File: master-binlog.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: 1019
Relay_Log_Space: 1451
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: 128
Master_UUID: 85620474-e669-11e8-bdf5-000c294b7cb8
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: 85620474-e669-11e8-bdf5-000c294b7cb8:1-4
Executed_Gtid_Set: 85620474-e669-11e8-bdf5-000c294b7cb8:1-4
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
验证:
在主库上创建一个库
mysql> create database dubai;
Query OK, 1 row affected (0.00 sec)
在从库上查看
[root@localhost ~]# mysql -uroot -phxd123456. -e'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| dubai |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@localhost ~]#
同步成功