Mysql传统复制空库搭建过程中reset slave以及reset slave all对复制的影响

Mysql传统复制空库搭建过程中reset slave以及reset slave all对复制的影响

主库信息

  • IP:192.168.1.21
  • 版本:5.7.18
  • 空库搭建主从

从库信息

  • IP:192.168.1.128
  • 版本:5.6.36
  • 空库搭建主从

操作过程

主库上操作
  • 赋予复制账户:
 
 
  1. mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.128' IDENTIFIED by 'repl';
  • 查看master信息:
 
 
  1. mysql> show master status;
  2. +------------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +------------------+----------+--------------+------------------+-------------------+
  5. | mysql-bin.000022 | 612 | | | |
  6. +------------------+----------+--------------+------------------+-------------------+
  7. 1 row in set (0.00 sec)
从库上操作
  • 直接指定新的master:
 
 
  1. root@localhost : (none) 05:16:09> change master to master_host='192.168.1.21',master_user='repl',master_password='repl',master_log_file='mysql-bin.000022',master_log_pos=612;
  2. ERROR 1776 (HY000): Parameters MASTER_LOG_FILE, MASTER_LOG_POS, RELAY_LOG_FILE and RELAY_LOG_POS cannot be set when MASTER_AUTO_POSITION is active.
  3. #报错是因为128这台mysql server之前是使用gtid复制指向另一台mysql主库;而且没有经过reset slave清理相关数据,直接指向新的master肯定也会有问题。
  • 关闭gtid设置:
 
 
  1. [root@host-192-168-1-128 ~]# service mysql stop
  2. Shutting down MySQL.... SUCCESS!
  3. [root@host-192-168-1-128 ~]# vim /etc/my.cnf 在配置文件中关闭gtid
  4. #gtid-mode=on # GTID only
  5. #enforce-gtid-consistency=true # GTID only
  6. 或者
  7. enforce_gtid_consistency=OFF
  8. gtid_mode=OFF
  9. [root@host-192-168-1-128 ~]# service mysql start
  10. Starting MySQL.. SUCCESS!
  • 重新指定master,并开启slave:
 
 
  1. root@localhost : (none) 05:19:34> change master to master_host='192.168.1.21',master_user='repl',master_password='repl',master_log_file='mysql-bin.000022',master_log_pos=612;
  2. Query OK, 0 rows affected, 2 warnings (0.03 sec)
  3. root@localhost : (none) 05:20:21> start slave;
  4. ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
  5. root@localhost : (none) 05:20:37> reset slave;
  6. Query OK, 0 rows affected (0.03 sec)
  7. root@localhost : (none) 05:20:56> start slave;
  8. Query OK, 0 rows affected (0.10 sec)
  • 查看复制情况:
 
 
  1. root@localhost : (none) 05:20:57> show slave status\G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.1.21
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000022
  9. Read_Master_Log_Pos: 612
  10. Relay_Log_File: mysql-relay-bin.000005
  11. Relay_Log_Pos: 333
  12. Relay_Master_Log_File: mysql-bin.000022
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: No
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 1008
  22. Last_Error: Error 'Can't drop database 'sbtest'; database doesn't exist' on query. Default database: 'sbtest'. Query: 'drop database sbtest'
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 154
  25. Relay_Log_Space: 964
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: NULL
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 1008
  40. Last_SQL_Error: Error 'Can't drop database 'sbtest'; database doesn't exist' on query. Default database: 'sbtest'. Query: 'drop database sbtest'
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 12001
  43. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  44. Master_Info_File: mysql.slave_master_info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State:
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp: 170516 05:20:58
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. 1 row in set (0.00 sec)
  58. # 注意Exec_Master_Log_Pos: 154
主库上查看
  • 查看主库binlog中的events:
 
 
  1. mysql> show binlog events in 'mysql-bin.000022';
  2. +------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
  4. +------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | mysql-bin.000022 | 4 | Format_desc | 12001 | 123 | Server ver: 5.7.18-log, Binlog ver: 4 |
  6. | mysql-bin.000022 | 123 | Previous_gtids | 12001 | 154 | |
  7. | mysql-bin.000022 | 154 | Anonymous_Gtid | 12001 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
  8. | mysql-bin.000022 | 219 | Query | 12001 | 317 | drop database sbtest |
  9. | mysql-bin.000022 | 317 | Anonymous_Gtid | 12001 | 382 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
  10. | mysql-bin.000022 | 382 | Query | 12001 | 612 | GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.128' IDENTIFIED WITH 'mysql_native_password' AS '*A424E797037BF97C19A2E88CF7891C5C2038C039' |
  11. +------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  12. 6 rows in set (0.00 sec)
  13. # 对比slave的Exec_Master_Log_Pos: 154,确实会去执行drop database sbtest。问题来了:为什么我指定的master_log_pos=612但是却会执行这个位置之前的binlog日志?
解决方法
  • 先关闭slave:
 
 
  1. root@localhost : (none) 05:39:13> stop slave;
  2. Query OK, 0 rows affected (0.02 sec)
  • 清空master.info文件和relay-log.info 文件以及所有的relay log 文件,all也包括在内存中的信息:
 
 
  1. root@localhost : (none) 05:39:17> reset slave all;
  2. Query OK, 0 rows affected (0.08 sec)
  • 重新指定master并开启slave:
 
 
  1. root@localhost : (none) 05:39:26> change master to master_host='192.168.1.21',master_user='repl',master_password='repl',master_log_file='mysql-bin.000022',master_log_pos=612;
  2. Query OK, 0 rows affected, 2 warnings (0.10 sec)
  3. root@localhost : (none) 05:39:44> start slave;
  4. Query OK, 0 rows affected (0.02 sec)
  • 查看复制情况:
 
 
  1. root@localhost : (none) 05:39:48> show slave status\G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.1.21
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000022
  9. Read_Master_Log_Pos: 612
  10. Relay_Log_File: mysql-relay-bin.000002
  11. Relay_Log_Pos: 286
  12. Relay_Master_Log_File: mysql-bin.000022
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 612
  25. Relay_Log_Space: 459
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 12001
  43. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  44. Master_Info_File: mysql.slave_master_info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. 1 row in set (0.00 sec)

思路探讨

  • reset slave删除master.info文件和relay-log.info 文件以及所有的relay log 文件并重新启用一个新的relaylog文件,但并不会清理存储于内存中的复制信息比如master host, master port, master user, or master password,也就是说如果没有使用change master 命令做重新定向,执行start slave 还是会指向旧的master 上面。
  • 在5.5.16 版本以及以后,可以使用 RESET SLAVE ALL 来完全的清理复制连接参数信息。 
    In MySQL 5.5.16 and later, you can instead use RESET SLAVE ALL to reset these connection parameters
  • 至于明明指定了master_log_pos=612,从库却执行了这之前的位置Exec_Master_Log_Pos: 154。 
    猜测:这应该和128这个从库之前指定的是另一个master(192.168.1.12),而这些旧的信息还保存在内存中导致的。
  • 当时出现错误的第一反应是用skip去跳过错误,问了小波之后说尝试下reset slave all,才能进一步去了解这其中的区别。

参考资料

https://www.percona.com/blog/2013/04/17/reset-slave-vs-reset-slave-all-disconnecting-a-replication-slave-is-easier-with-mysql-5-5/ 
https://dev.mysql.com/doc/refman/5.5/en/reset-slave.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值