MySQL GTID空库复制搭建

来源:互联网 发布:雅思听力短期提高知乎 编辑:程序博客网 时间:2024/05/18 14:14

MySQL GTID空库复制搭建
环境
1. 主备库开启gtid
2. 备库上停掉之前的传统复制并清空信息
3. 指定主库
4. 备库重新连接

MySQL GTID空库复制搭建

环境

  • 主库:IP:192.168.1.21;版本:5.7.18
  • 备库:IP:192.168.1.128;版本:5.6.36

1. 主备库开启gtid

  1. #vim my.cnf
  2. gtid_mode=on
  3. enforce_gtid_consistency=on
  4. #log-slave-updates=ON(5.7不需要添加,会将gtid信息放在mysql.gtid_executed表中)

2. 备库上停掉之前的传统复制并清空信息

  1. root@localhost : (none) 11:56:00> stop slave;
  2. Query OK, 0 rows affected (0.02 sec)
  3. root@localhost : (none) 11:56:33> reset slave all;
  4. Query OK, 0 rows affected (0.08 sec)

3. 指定主库

  1. #复制账号沿用先前的账号repl和密码repl
  2. root@localhost : (none) 01:43:29> change master to master_host='192.168.1.21',master_user='repl',master_password='repl',master_auto_position=1;
  3. Query OK, 0 rows affected, 2 warnings (0.08 sec)
  4. root@localhost : (none) 01:57:13> show slave status\G;
  5. *************************** 1. row ***************************
  6. Slave_IO_State:
  7. Master_Host: 192.168.1.21
  8. Master_User: repl
  9. Master_Port: 3306
  10. Connect_Retry: 60
  11. Master_Log_File:
  12. Read_Master_Log_Pos: 4
  13. Relay_Log_File: mysql-relay-bin.000001
  14. Relay_Log_Pos: 4
  15. Relay_Master_Log_File:
  16. Slave_IO_Running: No
  17. Slave_SQL_Running: Yes
  18. Replicate_Do_DB: sbtest
  19. Replicate_Ignore_DB:
  20. Replicate_Do_Table:
  21. Replicate_Ignore_Table:
  22. Replicate_Wild_Do_Table:
  23. Replicate_Wild_Ignore_Table:
  24. Last_Errno: 0
  25. Last_Error:
  26. Skip_Counter: 0
  27. Exec_Master_Log_Pos: 0
  28. Relay_Log_Space: 151
  29. Until_Condition: None
  30. Until_Log_File:
  31. Until_Log_Pos: 0
  32. Master_SSL_Allowed: No
  33. Master_SSL_CA_File:
  34. Master_SSL_CA_Path:
  35. Master_SSL_Cert:
  36. Master_SSL_Cipher:
  37. Master_SSL_Key:
  38. Seconds_Behind_Master: 0
  39. Master_SSL_Verify_Server_Cert: No
  40. Last_IO_Errno: 1593
  41. Last_IO_Error: The slave IO thread stops because the master has @@GLOBAL.GTID_MODE OFF and this server has @@GLOBAL.GTID_MODE ON
  42. Last_SQL_Errno: 0
  43. Last_SQL_Error:
  44. Replicate_Ignore_Server_Ids:
  45. Master_Server_Id: 12001
  46. Master_UUID:
  47. Master_Info_File: mysql.slave_master_info
  48. SQL_Delay: 0
  49. SQL_Remaining_Delay: NULL
  50. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  51. Master_Retry_Count: 86400
  52. Master_Bind:
  53. Last_IO_Error_Timestamp: 170523 01:57:13
  54. Last_SQL_Error_Timestamp:
  55. Master_SSL_Crl:
  56. Master_SSL_Crlpath:
  57. Retrieved_Gtid_Set:
  58. Executed_Gtid_Set:
  59. Auto_Position: 1
  60. 1 row in set (0.00 sec)
  61. #报错显示主库没有开启gtid
  62. #查看主库是否开启gtid:
  63. mysql> show variables like 'GTID_MODE%';
  64. +---------------+-------+
  65. | Variable_name | Value |
  66. +---------------+-------+
  67. | gtid_mode | OFF |
  68. +---------------+-------+
  69. 1 row in set (0.03 sec)
  • 查看主库配置文件发现,主库的gtid_mode=on以及enforce_gtid_consistency=on放在了[mysqld_safe]下,而没有放在[mysqld]下
  1. #修改vim /usr/local/mysql/my.cnf(需要在mysqld下添加,否则会导致不生效)
  2. [mysqld]
  3. gtid_mode=on
  4. enforce_gtid_consistency=on
  1. #重启主库mysql
  2. [root@host-192-168-1-21 mysql]# service mysql stop
  3. Shutting down MySQL.... SUCCESS!
  4. [root@host-192-168-1-21 mysql]# service mysql start
  5. Starting MySQL.. SUCCESS!
  6. mysql> show variables like 'GTID_MODE%';
  7. +---------------+-------+
  8. | Variable_name | Value |
  9. +---------------+-------+
  10. | gtid_mode | ON |
  11. +---------------+-------+
  12. 1 row in set (0.01 sec)

4. 备库重新连接

  1. root@localhost : (none) 02:30:41> stop slave;
  2. Query OK, 0 rows affected (0.01 sec)
  3. root@localhost : (none) 02:30:46> start slave;
  4. Query OK, 0 rows affected (0.02 sec)
  5. root@localhost : (none) 02:30:48> show slave status\G;
  6. *************************** 1. row ***************************
  7. Slave_IO_State: Waiting for master to send event
  8. Master_Host: 192.168.1.21
  9. Master_User: repl
  10. Master_Port: 3306
  11. Connect_Retry: 60
  12. Master_Log_File: mysql-bin.000031
  13. Read_Master_Log_Pos: 154
  14. Relay_Log_File: mysql-relay-bin.000002
  15. Relay_Log_Pos: 364
  16. Relay_Master_Log_File: mysql-bin.000031
  17. Slave_IO_Running: Yes
  18. Slave_SQL_Running: Yes
  19. Replicate_Do_DB: sbtest
  20. Replicate_Ignore_DB:
  21. Replicate_Do_Table:
  22. Replicate_Ignore_Table:
  23. Replicate_Wild_Do_Table:
  24. Replicate_Wild_Ignore_Table:
  25. Last_Errno: 0
  26. Last_Error:
  27. Skip_Counter: 0
  28. Exec_Master_Log_Pos: 154
  29. Relay_Log_Space: 568
  30. Until_Condition: None
  31. Until_Log_File:
  32. Until_Log_Pos: 0
  33. Master_SSL_Allowed: No
  34. Master_SSL_CA_File:
  35. Master_SSL_CA_Path:
  36. Master_SSL_Cert:
  37. Master_SSL_Cipher:
  38. Master_SSL_Key:
  39. Seconds_Behind_Master: 0
  40. Master_SSL_Verify_Server_Cert: No
  41. Last_IO_Errno: 0
  42. Last_IO_Error:
  43. Last_SQL_Errno: 0
  44. Last_SQL_Error:
  45. Replicate_Ignore_Server_Ids:
  46. Master_Server_Id: 12001
  47. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  48. Master_Info_File: mysql.slave_master_info
  49. SQL_Delay: 0
  50. SQL_Remaining_Delay: NULL
  51. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  52. Master_Retry_Count: 86400
  53. Master_Bind:
  54. Last_IO_Error_Timestamp:
  55. Last_SQL_Error_Timestamp:
  56. Master_SSL_Crl:
  57. Master_SSL_Crlpath:
  58. Retrieved_Gtid_Set:
  59. Executed_Gtid_Set:
  60. Auto_Position: 1
  61. 1 row in set (0.00 sec)