MySQL5.6 GTID模式下同步复制报错不能跳过的解决方法
数据库版本:
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.10-log |
+------------+
1 row in set (0.02 sec)
同步复制信息:
mysql> show slave status\g;
*************************** 1. row ***************************
slave_io_state: waiting for master to send event
master_host: 192.168.8.25
master_user: repl
master_port: 3306
connect_retry: 60
master_log_file: mysql-bin.000007
read_master_log_pos: 5036
relay_log_file: m2-relay-bin.000008
relay_log_pos: 408
relay_master_log_file: mysql-bin.000007
slave_io_running: yes
slave_sql_running: no
replicate_do_db:
replicate_ignore_db: mysql
replicate_do_table:
replicate_ignore_table:
replicate_wild_do_table:
replicate_wild_ignore_table:
last_errno: 1062
last_error: could not execute write_rows event on table test.t; duplicate entry '12'
for key 'primary', error_code: 1062; handler error ha_err_found_dupp_key; the event's master log mysql-bin.000007,
end_log_pos 2267
skip_counter: 0
exec_master_log_pos: 2045
relay_log_space: 3810
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: 1062
last_sql_error: could not execute write_rows event on table test.t; duplicate entry '12'
for key 'primary', error_code: 1062; handler error ha_err_found_dupp_key; the event's master log mysql-bin.000007,
end_log_pos 2267
replicate_ignore_server_ids:
master_server_id: 25
master_uuid: cf716fda-74e2-11e2-b7b7-000c290a6b8f
master_info_file: /usr/local/mysql/data2/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: 130313 07:24:43
master_ssl_crl:
master_ssl_crlpath:
retrieved_gtid_set: cf716fda-74e2-11e2-b7b7-000c290a6b8f:141-151
executed_gtid_set: cf716fda-74e2-11e2-b7b7-000c290a6b8f:1-140
auto_position: 1
1 row in set (0.02 sec)
error:
no query specified
提示主键冲突,由于是测试机,于是我直接跳过,
mysql> set global sql_slave_skip_counter=1;
error 1858 (hy000): sql_slave_skip_counter can not be set when the server is running with gtid_mode = on.
instead, for each transaction that you want to skip, generate an empty transaction with the same gtid as the transaction
提示:由于运行在gtid模式,所以不支持sql_slave_skip_counter
语法,如果你想跳过,就必须把事务id设置为空值。
看来只能用这个方法了。
mysql> show global variables like '%gtid%';
+--------------------------+--------------------------------------------+
| variable_name | value |
+--------------------------+--------------------------------------------+
| enforce_gtid_consistency | on |
| gtid_executed | cf716fda-74e2-11e2-b7b7-000c290a6b8f:1-140 |
| gtid_mode | on |
| gtid_owned | |
| gtid_purged | cf716fda-74e2-11e2-b7b7-000c290a6b8f:1-140 |
+--------------------------+--------------------------------------------+
5 rows in set (0.04 sec)
mysql> set global gtid_executed='';
error 1238 (hy000): variable 'gtid_executed' is a read only variable
mysql>
mysql> set global gtid_purged='';
error 1840 (hy000): gtid_purged can only be set when gtid_executed is empty.
郁闷,直接设置还不行。
查看了手册,需要执行reset master才可以(注:在从上执行啊,千万别在主上)。
mysql> reset master;
query ok, 0 rows affected (0.16 sec)
mysql> reset slave;
error 1198 (hy000): this operation cannot be performed with a running slave; run stop slave first
mysql> stop slave;
query ok, 0 rows affected (0.08 sec)
mysql> reset slave;
query ok, 0 rows affected (0.16 sec)
执行reset slave的目的是清空master.info和relay-log.info,以便后面重新change master to主从复制。
还记得刚才的gtid_purged那个点吗,只需重新设置下一个点即可。
下面是步骤:
mysql> show global variables like '%gtid%';
+--------------------------+-------+
| variable_name | value |
+--------------------------+-------+
| enforce_gtid_consistency | on |
| gtid_executed | |
| gtid_mode | on |
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+
5 rows in set (0.06 sec)
mysql> set global gtid_purged='cf716fda-74e2-11e2-b7b7-000c290a6b8f:1-141';
query ok, 0 rows affected (0.16 sec)
mysql> change master to master_host='192.168.8.25',master_user='repl',master_password='repl'
,master_auto_position = 1;
query ok, 0 rows affected, 2 warnings (0.32 sec)
mysql> start slave;
query ok, 0 rows affected (0.13 sec)
mysql> show slave status\g;
*************************** 1. row ***************************
slave_io_state: waiting for master to send event
master_host: 192.168.8.25
master_user: repl
master_port: 3306
connect_retry: 60
master_log_file: mysql-bin.000007
read_master_log_pos: 5036
relay_log_file: m2-relay-bin.000008
relay_log_pos: 408
relay_master_log_file: mysql-bin.000007
slave_io_running: yes
slave_sql_running: no
replicate_do_db:
replicate_ignore_db: mysql
replicate_do_table:
replicate_ignore_table:
replicate_wild_do_table:
replicate_wild_ignore_table:
last_errno: 1050
last_error: error 'table 't0' already exists' on query.
default database: 'test'. query: 'create table t0 like t'
skip_counter: 0
exec_master_log_pos: 2298
relay_log_space: 3557
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: 1050
last_sql_error: error 'table 't0' already exists' on query.
default database: 'test'. query: 'create table t0 like t'
replicate_ignore_server_ids:
master_server_id: 25
master_uuid: cf716fda-74e2-11e2-b7b7-000c290a6b8f
master_info_file: /usr/local/mysql/data2/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: 130313 07:50:42
master_ssl_crl:
master_ssl_crlpath:
retrieved_gtid_set: cf716fda-74e2-11e2-b7b7-000c290a6b8f:142-151
executed_gtid_set: cf716fda-74e2-11e2-b7b7-000c290a6b8f:1-141
auto_position: 1
1 row in set (0.02 sec)
error:
no query specified
### 看,这里的报错信息已经不一样了,按照这种方法,重复执行,直到同步复制正常。
mysql> stop slave;
query ok, 0 rows affected (0.07 sec)
mysql> reset master;
query ok, 0 rows affected (0.17 sec)
mysql> reset slave;
query ok, 0 rows affected (0.16 sec)
mysql> set global gtid_purged='cf716fda-74e2-11e2-b7b7-000c290a6b8f:1-151';
query ok, 0 rows affected (0.13 sec)
mysql> change master to master_host='192.168.8.25',master_user='repl',master_password='repl'
,master_auto_position = 1;
query ok, 0 rows affected, 2 warnings (0.33 sec)
mysql> start slave;
query ok, 0 rows affected (0.11 sec)
mysql> show slave status\g;
*************************** 1. row ***************************
slave_io_state: waiting for master to send event
master_host: 192.168.8.25
master_user: repl
master_port: 3306
connect_retry: 60
master_log_file: mysql-bin.000007
read_master_log_pos: 5036
relay_log_file: m2-relay-bin.000008
relay_log_pos: 408
relay_master_log_file: mysql-bin.000007
slave_io_running: yes
slave_sql_running: yes
replicate_do_db:
replicate_ignore_db: mysql
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: 5036
relay_log_space: 819
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: 25
master_uuid: cf716fda-74e2-11e2-b7b7-000c290a6b8f
master_info_file: /usr/local/mysql/data2/master.info
sql_delay: 0
sql_remaining_delay: null
slave_sql_running_state: slave has read all relay log; waiting for the slave i/o thread to update it
master_retry_count: 86400
master_bind:
last_io_error_timestamp:
last_sql_error_timestamp:
master_ssl_crl:
master_ssl_crlpath:
retrieved_gtid_set:
executed_gtid_set: cf716fda-74e2-11e2-b7b7-000c290a6b8f:1-151
auto_position: 1
1 row in set (0.01 sec)
error:
no query specified
呵呵,总算是解决好了,真麻烦啊。如果大家有更好、更方便的解决方法,也给我回帖哦,谢谢。
上一篇: Mysql数据库设计三范式实例解析