从MySQL5.6版本开始,支持以事务的方式来做主从同步,最大限度的保证MySQL的主从一致性。实现这一复制特性的关键是GTID(Global Transaction Identifiers)全局事务ID,通过GTID来强化数据库的主从一致性,故障恢复以及容错能力
MySQL5.7支持在线修改复制类型,MySQL5.6只能离线修改,本篇文章主要介绍离线修改复制类型的方法,后续的文章中会介绍如何在线更换复制类型
什么是GTID 官方文档:
A GTID is represented as a pair of coordinates, separated by a colon character (:), as shown here:
1 GTID = source_id:transaction_id
每一个 GTID 代表一个数据库事务。在上面的定义中,source_id
表示执行事务的主库 uuid(server_uuid),transaction_id
是一个从 1 开始的自增计数,表示在这个主库上执行的第 n 个事务。MySQL 会保证事务与 GTID 之间的 1 : 1 映射
服务器资源
master:192.168.10.153
slave:192.168.10.157
安装MySQL5.7 略
可以参考上一篇文章来安装MySQL5.7
master配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 > vim /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8 [mysql] no-auto-rehash default-character-set = utf8 [mysqld] server-id = 10153 port = 3306 user = mysql basedir = /usr/local /mysql datadir = /data/mysqldata socket = /tmp/mysql.sock default-storage-engine = INNODB character-set-server = utf8 connect_timeout = 60 interactive_timeout = 28800 wait_timeout = 28800 back_log = 500 event_scheduler = ON skip_name_resolve = ON log -bin = /data/mysqlLog/logs/mysql-bin binlog_format = row max_binlog_size = 128M binlog_cache_size = 2M expire-logs-days = 5 log -slave-updates=true binlog_checksum = CRC32 master_verify_checksum = 1 slave_sql_verify_checksum = 1 binlog_rows_query_log_events = 1 gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=4 slow_query_log = 1 slow_query_log_file = /data/mysqlLog/logs/mysql.slow long_query_time = 1 log_error = /data/mysqlLog/logs/error.log max_connections = 3000 max_connect_errors = 32767 log_bin_trust_function_creators = 1 transaction_isolation = READ-COMMITTED
slave配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 > vim /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8 [mysql] no-auto-rehash default-character-set = utf8 [mysqld] server-id = 10157 port = 3306 user = mysql basedir = /usr/local /mysql datadir = /data/mysqldata socket = /tmp/mysql.sock default-storage-engine = INNODB character-set-server = utf8 connect_timeout = 60 wait_timeout = 18000 back_log = 500 event_scheduler = ON log -bin = /data/mysqlLog/logs/mysql-binbinlog_format = row max_binlog_size = 128M binlog_cache_size = 2M expire-logs-days = 5 log -slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=4 slow_query_log = 1 slow_query_log_file = /data/mysqlLog/logs/mysql.slow long_query_time = 2 log -error = /data/mysqlLog/logs/error.logmax_connections = 3000 max_connect_errors = 10000 log_bin_trust_function_creators = 1 transaction_isolation = READ-COMMITTED
分别启动主库和从库 1 > systemctl restart mysqld
在主库中创建复制用户 1 2 3 4 5 6 7 8 mysql> SET GLOBAL validate_password_policy = LOW; Query OK, 0 rows affected (0.00 sec) mysql> create user 'repl' identified by '12345678'; Query OK, 0 rows affected (0.05 sec) mysql> grant replication slave on *.* to repl@'%'; Query OK, 0 rows affected (0.02 sec)
查看主库与从库GTID状态 1 2 3 4 5 6 7 mysql> show variables like 'gtid_mode'; + | Variable_name | Value | + | gtid_mode | ON | + 1 row in set (0.01 sec)
在从库启动复制线程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 mysql> change master to -> master_host='192.168.10.153', -> master_port=3306, -> master_user='repl', -> master_password='12345678', -> master_auto_position=1 for channel "db153"; Query OK, 0 rows affected, 2 warnings (0.27 sec) mysql> start slave for channel "db153"; Query OK, 0 rows affected (0.09 sec) mysql> show slave status for channel "db153" \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.153 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 595 Relay_Log_File: localhost-relay-bin-db153.000002 Relay_Log_Pos: 808 Relay_Master_Log_File: mysql-bin.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: 595 Relay_Log_Space: 1025 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: 10153 Master_UUID: 1349d343-6611-11e6-b341-005056ad5f2f 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: 1349d343-6611-11e6-b341-005056ad5f2f:1-2 Executed_Gtid_Set: 1349d343-6611-11e6-b341-005056ad5f2f:1-2 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: db153 Master_TLS_Version: 1 row in set (0.00 sec) mysql>
验证主从同步 master 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 mysql> show master status; + | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | + | mysql-bin.000001 | 595 | | | 1349d343-6611-11e6-b341-005056ad5f2f:1-2 | + 1 row in set (0.00 sec) mysql> create database polarsnow; Query OK, 1 row affected (0.03 sec) mysql> show master status; + | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | + | mysql-bin.000001 | 769 | | | 1349d343-6611-11e6-b341-005056ad5f2f:1-3 | + 1 row in set (0.00 sec) mysql>
slave 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 mysql> show slave status for channel "db153"\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.153 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 769 Relay_Log_File: localhost-relay-bin-db153.000002 Relay_Log_Pos: 982 Relay_Master_Log_File: mysql-bin.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: 769 Relay_Log_Space: 1199 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: 10153 Master_UUID: 1349d343-6611-11e6-b341-005056ad5f2f 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: 1349d343-6611-11e6-b341-005056ad5f2f:1-3 Executed_Gtid_Set: 1349d343-6611-11e6-b341-005056ad5f2f:1-3 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: db153 Master_TLS_Version: 1 row in set (0.00 sec) mysql> show databases ; + | Database | + | information_schema | | mysql | | performance_schema | | polarsnow | | sys | + 5 rows in set (0.00 sec) mysql>
至此,事务同步配置成功!
在之前使用二进制复制的主从模式时,经历了各种主从数据不一致的情况,而从MySQL5.6开始新引入的事务复制能否解决二进制bin-log复制的数据一致性的痛点还有待观察和检验……
附录