mysql5.7 GTID 主從複製模式-增加新的slave1(好文章!!)
實驗場景描述及目的:
GTID的主從模式,目前是一主一從,希望再加一個slave做成一主兩從的模式。由於測試的資料庫不大,通過mysqldump備份資料,如果資料庫比較大的情況下,可以想辦法通過其它slave的備份來搭建新的slave。
1.備份master
-
mysql> FLUSH TABLE WITH READ LOCK;
-
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:
-
mysql> show global variables like 'gtid_%';
-
+----------------------------------+-------------------------------------------+
-
| Variable_name | Value |
-
+----------------------------------+-------------------------------------------+
-
| gtid_executed | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-11 |
-
| gtid_executed_compression_period | 1000 |
-
| gtid_mode | ON |
-
| gtid_owned | |
-
| gtid_purged | |
-
+----------------------------------+-------------------------------------------+
-
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
-
[root@qht133 backup]# cat /etc/my.cnf
-
[mysqld]
-
socket = /usr/local/mysql/mysql.sock
-
character_set_server= utf8
-
init_connect= 'SET NAMES utf8'
-
basedir= /usr/local/mysql
-
datadir= /u01/mysql
-
socket = /u01/mysql/mysql.sock
-
log-error= /u01/log/mysql/mysql_3306.err
-
pid-file= /u01/mysql/mysqld.pid
-
lower_case_table_names = 1
-
sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
-
secure-file-priv = /u01/backup
-
gtid_mode=on #開啟gtid
-
enforce_gtid_consistency=on #強制gtid一致性
-
server-id=10003 #server id
-
skip_slave_start=1 #開啟備庫後不自動應用恢復程式
-
#log_bin = /u01/mysql/mysql_bin
-
#skip-grant-tables
-
#innodb_flush_log_at_trx_commit=1
-
#sync_binlog=1
-
relay-log=/u01/mysql/slave_relay_bin
-
expire_logs_days=10
-
relay_log_recovery=on
-
relay_log_info_repository=TABLE
重啟資料庫:
-
[root@qht133 backup]# service mysql restart
-
Shutting down MySQL.. [ OK ]
-
Starting MySQL.. [ OK ]
3.為了檢驗新的slave複製的正確性,在master上做些操作,看slave能否正確的同步。
-
mysql> use l5m
-
Database changed
-
mysql> select count(*) from test_emp;
-
+----------+
-
| count(*) |
-
+----------+
-
| 1099000 |
-
+----------+
-
1 row in set (0.67 sec)
-
mysql> delete from test_emp limit 1000;
-
Query OK, 1000 rows affected (0.08 sec)
-
mysql> select count(*) from test_emp;
-
+----------+
-
| count(*) |
-
+----------+
-
| 1098000 |
-
+----------+
-
1 row in set (0.18 sec)
-
mysql> commit;
-
Query OK, 0 rows affected (0.00 sec)
-
mysql> show global variables like 'gtid_%';
-
+----------------------------------+-------------------------------------------+
-
| Variable_name | Value |
-
+----------------------------------+-------------------------------------------+
-
| gtid_executed | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-12 |
-
| gtid_executed_compression_period | 1000 |
-
| gtid_mode | ON |
-
| gtid_owned | |
-
| gtid_purged | |
-
+----------------------------------+-------------------------------------------+
-
5 rows in set (0.01 sec)
4.在新的slave上開啟複製:
先檢查一下當前的gtid:
-
mysql> show global variables like 'gtid_%';
-
+----------------------------------+-------------------------------------------+
-
| Variable_name | Value |
-
+----------------------------------+-------------------------------------------+
-
| gtid_executed | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-11 |
-
| gtid_executed_compression_period | 1000 |
-
| gtid_mode | ON |
-
| gtid_owned | |
-
| gtid_purged | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-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。)
下面直接開啟複製就行了:
-
mysql> change master to
-
-> master_host='172.17.61.131',
-
-> master_port=3306,
-
-> master_user='repl',
-
-> master_password='repl',
-
-> master_auto_position=1;
-
Query OK, 0 rows affected, 2 warnings (0.07 sec)
mysql> start slave;
-
mysql> show global variables like 'gtid_%';
-
+----------------------------------+-------------------------------------------+
-
| Variable_name | Value |
-
+----------------------------------+-------------------------------------------+
-
| gtid_executed | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-12 |
-
| gtid_executed_compression_period | 1000 |
-
| gtid_mode | ON |
-
| gtid_owned | |
-
| gtid_purged | 8d8746fb-2cc6-11e8-b1b6-000c295c63e0:1-12 |
-
+----------------------------------+-------------------------------------------+
-
5 rows in set (0.01 sec)
驗證一下全備之後master上修改的資料有沒有同步過來:
-
mysql> use l5m;
-
Database changed
-
mysql> select count(*) from test_emp;
-
+----------+
-
| count(*) |
-
+----------+
-
| 1098000 |
-
+----------+
-
1 row in set (0.40 sec)
沒有問題,資料都同步到了新的slave。
參考:
https://www.cnblogs.com/zejin2008/p/7705473.html
http://www.cnblogs.com/luckcs/articles/6295992.html
相關文章
- MySQL主從複製之GTID複製MySql
- mysql5.7主從複製,主主複製MySql
- Mysql 基於GTID主從複製MySql
- MySQL 5.7基於GTID的主從複製MySql
- MySQL5.7主從複製教程MySql
- MySQL5.7主從複製-半同步複製搭建MySql
- MySQL主從複製之GTID模式詳細介紹鞴嬈MySql模式
- Mysql 8.4.0 結合 Docker 搭建GTID主從複製,以及傳統主從複製MySqlDocker
- MySQL 5.7 基於GTID搭建主從複製MySql
- MySQL8.0輕鬆搞定GTID主從複製MySql
- MySQL5.7在滴滴雲主機上的主從複製MySql
- Mysql基於GTID的複製模式MySql模式
- 線上將傳統模式複製改為GTID複製模式模式
- MySQL8.0輕鬆搞定GTID主主複製MySql
- mysql GTID主從複製故障後不停機恢復同步流程MySql
- 淺談:redis的主從複製 + 哨兵模式Redis模式
- 主從複製
- MySQL 5.7傳統複製到GTID線上切換(一主一從)MySql
- MySQL的主從複製MySql
- mysql複製--主從複製配置MySql
- 主從複製是啥或者主從複製的原理是什麼?
- Windows 環境下,MySQL 的主從複製和主主複製WindowsMySql
- windows環境下,Mysql的主從複製和主主複製WindowsMySql
- MySQL 8 複製(四)——GTID與複製MySql
- MySQL 8 複製(五)——配置GTID複製MySql
- Linux下MySQL主從複製(GTID)+讀寫分離(ProxySQL)-實施筆記LinuxMySql筆記
- Redis:主從複製Redis
- Redis - 主從複製Redis
- MySQL主從複製MySql
- Redis主從複製Redis
- mysql資料庫的主從複製和主主複製實踐MySql資料庫
- 聊聊MySQL主從複製的幾種複製方式MySql
- redis的主從複製的原理Redis
- MySQL主從複製之半同步複製MySql
- MySQL主從複製之非同步複製MySql非同步
- 16.1.3 使用GTID 配置複製
- Q複製的一篇好文章
- mysql主從複製(一):一主多從MySql