非中斷業務數據庫遷移一例
前期準備, 配置數據從Source Master復制到Target Master, 即Target Master成為Source Master的Slave(若Target Master是新庫的話, 還可添加replicate-do-db=name過濾條件).
遷移開始時, 在Source Master上執行flush no_write_to_binlog tables, 和flush tables with read lock.
在Target Master上查看延時為0時, 執行flush binary logs. 然後stop slave, 和reset slave all, 清除Source Master至Target Master的復制關系.
再後, 依次重啟各應用節點, 切換至Target Master新數據源.
最後在Source Master上unlock tables, 至此遷移結束.
該過程中的主角flush tables with read lock, 其含義為Closes all open tables and locks all tables for all databases with a global read lock, 即在MySQL數據庫Server層, 獲取一個全局讀鎖.
那麽配角flush no_write_to_binlog tables, 其含義為Closes all open tables, forces all tables in use to be closed, 起到加速flush tables with read lock操作完成的作用.
在數據庫管理的過程中, 經常會見到flush tables with read lock的身影.
如mysqldump備份時, 通過general log會看到如下輸出.
mysqldump --login-path=mytest --default-character-set=utf8mb4 --single-transaction --routines --triggers --events --quick --force --master-data=2 --hex-blob --all-databases > full.sql
25 Query FLUSH /*!40101 LOCAL */ TABLES
25 Query FLUSH TABLES WITH READ LOCK
25 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
25 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
25 Query SHOW VARIABLES LIKE 'gtid\_mode'
25 Query SELECT @@GLOBAL.GTID_EXECUTED
25 Query SHOW MASTER STATUS
25 Query UNLOCK TABLES
又如xtrabackup備份時, 也可見類似日誌.
xtrabackup --defaults-file=/etc/my.cnf --user=abc --password=xyz --socket=/3306/mysql.sock --target-dir=./pxb_full --backup
34 QuerySET SESSION lock_wait_timeout=31536000
34 QueryFLUSH NO_WRITE_TO_BINLOG TABLES
34 QueryFLUSH TABLES WITH READ LOCK
34 QuerySHOW MASTER STATUS
34 QuerySHOW VARIABLES
34 QueryFLUSH NO_WRITE_TO_BINLOG ENGINE LOGS
34 QueryUNLOCK TABLES
MHA在線手動切換時, 也用到了flush tables with read lock, 詳見如下輸出.
masterha_master_switch --conf=/etc/masterha/app2/app2.cnf --master_state=alive --orig_master_is_new_slave
...
It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.168.4.33(192.168.4.33:3307)? (YES/no): yes
Wed Feb 28 21:58:41 2018 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Wed Feb 28 21:58:41 2018 - [info] ok.
...
Wed Feb 28 21:58:43 2018 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Wed Feb 28 21:58:43 2018 - [info] Executing FLUSH TABLES WITH READ LOCK..
Wed Feb 28 21:58:43 2018 - [info] ok.
非中斷業務數據庫遷移一例