一 GITD介绍
1.1 gtid的含义
Global Transaction Identifier,全局事务标识
阿里云的rds目前已经使用gtid
基于gtid的主从复制原理
每个mysql数据库上都有一个唯一uuid
每个事务生成一个id
gtid由上面两者组合: uuid+事务id
1.2 优势
相对使用binlog+位置的方法来说
gtid让配置主从更加方便
从提升为主时比较方便
二 配置
2.1 主库的配置
[mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 net_read_timeout = 120 performance_schema_max_table_instances = 400 server-id = 1 gtid_mode=on enforce_gtid_consistency=on log-slave-updates=1 log-bin=master-bin log-bin-index = master-bin.index relay-log = relay-log relay-log-index = relay-log.index binlog_format=row [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid
[root@master mysql]# vim /etc/my.cnf
[root@master mysql]# systemctl stop mysqld
[root@master mysql]# rm -rf /data/mysql/*
[root@master mysql]# mysqld --initialize --user=mysql
查看uuid
[root@master mysql]# pwd
/data/mysql
[root@master mysql]# ll
查看数据库的uuid
在数据目录的auto.cnf
[root@master mysql]# cat auto.cnf
在mysql里面使用命令查看show global variables like 'server_uuid'
[root@master mysql]# systemctl start mysqld [root@master mysql]# mysql -u root -p Enter password: rrV.sL)!d9zE mysql> show global variables like 'server_uuid'; #查看uuid +---------------+--------------------------------------+ | Variable_name | Value | +---------------+--------------------------------------+ | server_uuid | ad5ee77e-9f2e-11e9-a76d-000c2991dd19 | +---------------+--------------------------------------+ 1 row in set (0.01 sec)
2.2 从库配置
[mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 net_read_timeout = 120 performance_schema_max_table_instances = 400 server-id = 2 gtid_mode=on enforce_gtid_consistency=on log-slave-updates=1 log-bin=master-bin log-bin-index = master-bin.index relay-log = relay-log relay-log-index = relay-log.index binlog_format=row [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid
开启数据库,查看uuid
[root@slave ~]# systemctl stop mysqld [root@slave ~]# rm -rf /data/mysql/* [root@slave ~]# mysqld --initialize --user=mysql 2019-07-05T14:24:23.975473Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 96435cd6-9f30-11e9-a575-000c2963fd11. 2019-07-05T14:24:23.977416Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-07-05T14:24:23.980860Z 1 [Note] A temporary password is generated for root@localhost: RN=hsqIFX2BO [root@slave ~]# systemctl start mysql Failed to start mysql.service: Unit not found. [root@slave ~]# systemctl start mysqld [root@slave ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.26-log mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> show global variables like 'server_uuid'; +---------------+--------------------------------------+ | Variable_name | Value | +---------------+--------------------------------------+ | server_uuid | 96435cd6-9f30-11e9-a575-000c2963fd11 | +---------------+--------------------------------------+ 1 row in set (0.00 sec)
[root@slave ~]# cd /data/mysql/
[root@slave mysql]# cat auto.cnf
三 配置主从
3.1 主库给从库复制权限
mysql> grant replication slave on *.* to 'replication'@'192.168.132.122' identified by '1234567'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> show master logs; +-------------------+-----------+ | Log_name | File_size | +-------------------+-----------+ | master-bin.000001 | 177 | | master-bin.000002 | 854 | +-------------------+-----------+ mysql> show binlog events in 'master-bin.000002'; +-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | master-bin.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 | | master-bin.000002 | 123 | Previous_gtids | 1 | 154 | | | master-bin.000002 | 154 | Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1' | # 事件类型是GTID 数据库的uuid+事务ID | master-bin.000002 | 219 | Query | 1 | 398 | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | | master-bin.000002 | 398 | Gtid | 1 | 463 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:2' | | master-bin.000002 | 463 | Query | 1 | 702 | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.132.122' IDENTIFIED WITH 'mysql_native_password' AS '*6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5' | | master-bin.000002 | 702 | Gtid | 1 | 767 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3' | | master-bin.000002 | 767 | Query | 1 | 854 | flush privileges | +-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
3.2 从库使用命令进行复制
mysql> change master to master_host='192.168.132.121',master_port=3306,master_user='replication',master_password='1234567',master_auto_position = 1; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.132.121 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000002 Read_Master_Log_Pos: 854 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 1069 Relay_Master_Log_File: master-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: 854 Relay_Log_Space: 1270 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: ad5ee77e-9f2e-11e9-a76d-000c2991dd19 #主库的UUID Master_Info_File: /data/mysql/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: ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1-3 #执行的GTID位置 Executed_Gtid_Set: 96435cd6-9f30-11e9-a575-000c2963fd11:1, ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1-3 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
3.3 观察从的binlog日志
mysql> show master logs; +-------------------+-----------+ | Log_name | File_size | +-------------------+-----------+ | master-bin.000001 | 177 | | master-bin.000002 | 1098 | +-------------------+-----------+ mysql> show binlog events in 'master-bin.000002'; +-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | master-bin.000002 | 4 | Format_desc | 2 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 | | master-bin.000002 | 123 | Previous_gtids | 2 | 154 | | | master-bin.000002 | 154 | Gtid | 2 | 219 | SET @@SESSION.GTID_NEXT= '96435cd6-9f30-11e9-a575-000c2963fd11:1' | | master-bin.000002 | 219 | Query | 2 | 398 | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | | master-bin.000002 | 398 | Gtid | 1 | 463 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1' | | master-bin.000002 | 463 | Query | 1 | 642 | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | | master-bin.000002 | 642 | Gtid | 1 | 707 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:2' | | master-bin.000002 | 707 | Query | 1 | 946 | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.132.122' IDENTIFIED WITH 'mysql_native_password' AS '*6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5' | | master-bin.000002 | 946 | Gtid | 1 | 1011 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3' | | master-bin.000002 | 1011 | Query | 1 | 1098 | flush privileges | +-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
四 验证
增加数据来验证是否同步成功
4.1 建库验证
主创建一个数据库
mysql> create database darren;
mysql> show binlog events in 'master-bin.000002';
从端查看
mysql> show databases;
mysql> show slave status\G;
4.2 建表验证
主创建一个表
mysql> use darren; Database changed mysql> create table test (id int); Query OK, 0 rows affected (0.01 sec) mysql> show binlog events in 'master-bin.000002'; +-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | master-bin.000002 | 702 | Gtid | 1 | 767 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3' | | master-bin.000002 | 767 | Query | 1 | 854 | flush privileges | | master-bin.000002 | 854 | Gtid | 1 | 919 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:4' | | master-bin.000002 | 919 | Query | 1 | 1019 | create database darren | | master-bin.000002 | 1019 | Gtid | 1 | 1084 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:5' | | master-bin.000002 | 1084 | Query | 1 | 1188 | use `darren`; create table test (id int) #建表,同是增长ID | +-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
从端:
mysql> show slave status\G;
4.3.插改验证
mysql> insert into test values (1);
mysql> update test set id = 3 where id = 1;
mysql> show binlog events in 'master-bin.000002';
从端查看
mysql> show slave status\G;
mysql> select * from darren.test;
删除
mysql> delete from test;
mysql> drop database darren;
mysql> show binlog events in 'master-bin.000002';
从端查看
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> show binlog events in 'master-bin.000002';
基本的配置验证完成
五 日志名未指定,主机名改变导致主从失效
前面介绍过类似的情况,因为配置文件没有指定日志名,导致MySQL在重启的时候,日志名发生改变,导致同步失败
从端的slave日志未指定,解决
stop slave; #停掉salve
reset slave; #重置salve
change master to master_host='192.168.132.121',master_port=3306,master_user='replication',master_password='1234567',master_auto_position = 1; #重新配从端复制数据
start slave; #开启slave
主端未指定,修复主从
stop slave; #停掉slave
reset slave; #重置slave
reset master; #重置master,必须做,很重要
change master to master_host='192.168.132.121',master_port=3306, master_user='replication',master_password='123456',master_auto_position = 1; 3重新配置主从
start slave; #开启slave