mysql5.7 GTID 主從複製模式-增加新的slave1(好文章!!)

HD243608836發表於2020-11-23

實驗場景描述及目的:

GTID的主從模式,目前是一主一從,希望再加一個slave做成一主兩從的模式。由於測試的資料庫不大,通過mysqldump備份資料,如果資料庫比較大的情況下,可以想辦法通過其它slave的備份來搭建新的slave。

 

1.備份master

 
  1. mysql> FLUSH TABLE WITH READ LOCK;

  2. Query OK, 0 rows affected (0.01 sec)

[root@qht131 backup]# mysqldump -u root -p --lock-all-tables --master-data=2 --flush-logs --all-databases --triggers --routines --events > full.sql
[root@qht131 backup]# scp full.sql 172.17.61.133:/u01/backup

記錄當前的gtid:

 
  1. mysql> show global variables like 'gtid_%';

  2. +----------------------------------+-------------------------------------------+

  3. | Variable_name | Value |

  4. +----------------------------------+-------------------------------------------+

  5. | gtid_executed | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-11 |

  6. | gtid_executed_compression_period | 1000 |

  7. | gtid_mode | ON |

  8. | gtid_owned | |

  9. | gtid_purged | |

  10. +----------------------------------+-------------------------------------------+

  11. 5 rows in set (0.09 sec)

如果是Xtrabackup的話,Xtrabackup_binlog_info檔案中包含global.gtid_purged='XXXXXX:XXXX'的資訊。

2.在新的slave上建好的資料庫並恢復Master的備份

    2.1.建庫方法:https://blog.csdn.net/jolly10/article/details/79566640

    2.2.恢復全備:

[root@qht133 backup]# mysql -uroot -p < full.sql

    2.3新slave上修改/etc/my.cnf,開啟gtid    

 
  1. [root@qht133 backup]# cat /etc/my.cnf

  2. [mysqld]

  3. socket = /usr/local/mysql/mysql.sock

  4. character_set_server= utf8

  5. init_connect= 'SET NAMES utf8'

  6. basedir= /usr/local/mysql

  7. datadir= /u01/mysql

  8. socket = /u01/mysql/mysql.sock

  9. log-error= /u01/log/mysql/mysql_3306.err

  10. pid-file= /u01/mysql/mysqld.pid

  11. lower_case_table_names = 1

  12. sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

  13. secure-file-priv = /u01/backup

  14. gtid_mode=on #開啟gtid

  15. enforce_gtid_consistency=on #強制gtid一致性

  16. server-id=10003 #server id

  17. skip_slave_start=1 #開啟備庫後不自動應用恢復程式

  18. #log_bin = /u01/mysql/mysql_bin

  19. #skip-grant-tables

  20. #innodb_flush_log_at_trx_commit=1

  21. #sync_binlog=1

  22. relay-log=/u01/mysql/slave_relay_bin

  23. expire_logs_days=10

  24. relay_log_recovery=on

  25. relay_log_info_repository=TABLE

    重啟資料庫:

 
  1. [root@qht133 backup]# service mysql restart

  2. Shutting down MySQL.. [ OK ]

  3. Starting MySQL.. [ OK ]

3.為了檢驗新的slave複製的正確性,在master上做些操作,看slave能否正確的同步。

 
  1. mysql> use l5m

  2. Database changed

  3. mysql> select count(*) from test_emp;

  4. +----------+

  5. | count(*) |

  6. +----------+

  7. |  1099000 |

  8. +----------+

  9. 1 row in set (0.67 sec)

  10.  
  11. mysql> delete from test_emp limit 1000;

  12. Query OK, 1000 rows affected (0.08 sec)

  13.  
  14. mysql> select count(*) from test_emp;

  15. +----------+

  16. | count(*) |

  17. +----------+

  18. |  1098000 |

  19. +----------+

  20. 1 row in set (0.18 sec)

  21.  
  22. mysql> commit;

  23. Query OK, 0 rows affected (0.00 sec)

  24.  
  25. mysql> show global variables like 'gtid_%';

  26. +----------------------------------+-------------------------------------------+

  27. | Variable_name                    | Value                                     |

  28. +----------------------------------+-------------------------------------------+

  29. | gtid_executed                    | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-12 |

  30. | gtid_executed_compression_period | 1000                                      |

  31. | gtid_mode                        | ON                                        |

  32. | gtid_owned                       |                                           |

  33. | gtid_purged                      |                                           |

  34. +----------------------------------+-------------------------------------------+

  35. 5 rows in set (0.01 sec)

4.在新的slave上開啟複製:

先檢查一下當前的gtid:

 
  1. mysql> show global variables like 'gtid_%';

  2. +----------------------------------+-------------------------------------------+

  3. | Variable_name | Value |

  4. +----------------------------------+-------------------------------------------+

  5. | gtid_executed | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-11 |

  6. | gtid_executed_compression_period | 1000 |

  7. | gtid_mode | ON |

  8. | gtid_owned | |

  9. | gtid_purged | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-11 |

  10. +----------------------------------+-------------------------------------------+

  11. 5 rows in set (0.00 sec)

新的slave庫是從master恢復過來的,gtid_purged現在自動就有了值1-11,並不需要手動的執行reset master; set global gtid_purged = 'xxxxx',看來mysql5.7這方面又加強了!(只有在@@global.gtid_executed為空的情況下,才可以動態設定@@global.gtid_purged。因此可以通過RESET MASTER的方式來清空@@global.gtid_executed。)

下面直接開啟複製就行了:

 
  1. mysql> change master to

  2. -> master_host='172.17.61.131',

  3. -> master_port=3306,

  4. -> master_user='repl',

  5. -> master_password='repl',

  6. -> master_auto_position=1;

  7. Query OK, 0 rows affected, 2 warnings (0.07 sec)

mysql> start slave;
 
  1. mysql> show global variables like 'gtid_%';

  2. +----------------------------------+-------------------------------------------+

  3. | Variable_name | Value |

  4. +----------------------------------+-------------------------------------------+

  5. | gtid_executed | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-12 |

  6. | gtid_executed_compression_period | 1000 |

  7. | gtid_mode | ON |

  8. | gtid_owned | |

  9. | gtid_purged | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-12 |

  10. +----------------------------------+-------------------------------------------+

  11. 5 rows in set (0.01 sec)

驗證一下全備之後master上修改的資料有沒有同步過來:

 
  1. mysql> use l5m;

  2. Database changed

  3. mysql> select count(*) from test_emp;

  4. +----------+

  5. | count(*) |

  6. +----------+

  7. | 1098000 |

  8. +----------+

  9. 1 row in set (0.40 sec)

沒有問題,資料都同步到了新的slave。

 

參考:

https://www.cnblogs.com/zejin2008/p/7705473.html

http://www.cnblogs.com/luckcs/articles/6295992.html

相關文章