mysql开启gtid并配置主从
默认主从都开启了bin log.
1.主从都在/etc/my.cnf中加入并重启服务
gtid_mode = ON
enforce_gtid_consistency = ON
2.在主库创建用户并授权
create user slave identified with mysql_native_password by '123456'
mysql>GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456'; //如果用户已存在,会改密码,如果不存在会直接创建这个用户,所以不需要create user单独创建用户
mysql>FLUSH PRIVILEGES;
2.在从库10.153.35.5中执行
CHANGE MASTER TO
MASTER_HOST='10.153.35.2',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_AUTO_POSITION = 1;
3.在从库开启复制线程
mysql> SHOW SLAVE STATUS\G
Slave_IO_Running: No
Slave_SQL_Running: No
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.01 sec)
mysql> START SLAVE SQL_THREAD;
Query OK, 0 rows affected (0.01 sec)
3.在从库开启复制线程
mysql> SHOW SLAVE STATUS\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果从库报错,需要调过这个报错的事务,怎么处理?
第一种方式使用GTID_NEXT进行单个事务跳跃:
从库:
stop slave sql_thread;
SET @@SESSION.GTID_NEXT= '0f7a8bd3-d7cd-11ef-9ed5-00505695e953:5'; <<===GTID对应第三步查出来的失败事务的GTID
show global variables like '%gtid%'; <<===查看全局GTID信息
begin;commit; <<===执行空事务
SET @@SESSION.GTID_NEXT=automatic; <<===GTID自动执行
start slave sql_thread; <<===启动SQL线程
show slave status\G; <<===确认事务已跳过
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.153.35.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 1740
Relay_Log_File: t3-hkucm-hkucmsdb-db-oracle01-st01-relay-bin.000003
Relay_Log_Pos: 454
Relay_Master_Log_File: mysql-bin.000004
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: 1740
Relay_Log_Space: 1577
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: 123454
Master_UUID: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953
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: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:5-7
Executed_Gtid_Set: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:1-7
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
2.使用gtid_purged跳过多个事务。
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.153.35.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 1925
Relay_Log_File: t3-hkucm-hkucmsdb-db-oracle01-st01-relay-bin.000003
Relay_Log_Pos: 454
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1051
Last_Error: Error 'Unknown table 'mysql.test1'' on query. Default database: 'mysql'. Query: 'DROP TABLE `test1` /* generated by server */'
Skip_Counter: 0
Exec_Master_Log_Pos: 1740
Relay_Log_Space: 3257
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1051
Last_SQL_Error: Error 'Unknown table 'mysql.test1'' on query. Default database: 'mysql'. Query: 'DROP TABLE `test1` /* generated by server */'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 123454
Master_UUID: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 250217 16:20:07
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:1-8
Executed_Gtid_Set:
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
stop slave;
reset master;
set global gtid_purged='0f7a8bd3-d7cd-11ef-9ed5-00505695e953:5-8';
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.153.35.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 1925
Relay_Log_File: t3-hkucm-hkucmsdb-db-oracle01-st01-relay-bin.000005
Relay_Log_Pos: 454
Relay_Master_Log_File: mysql-bin.000004
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: 1925
Relay_Log_Space: 1949
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: 123454
Master_UUID: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953
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: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:1-8
Executed_Gtid_Set: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:1-8
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>