MySQL 主備庫切換記錄

來源:互聯網
上載者:User

MySQL 主備庫切換記錄

舊主庫:

切換前將主庫的表上鎖,防止切換期間資料寫到主庫

mysql> flush tables with read lock;

舊備庫:

mysql> show processlist ;

Slave has read all relay log; waiting for the slave I/O thread to update it

mysql> show slave status \G
 

舊主庫:
 

mysql> show processlist ;
 

Master has sent all binlog to slave; waiting for binlog to be updated

舊從庫

mysql> STOP SLAVE IO_THREAD;
 

mysql> SHOW PROCESSLIST;
 

確保狀態為:has read all relay log
 

舊主庫:
 

mysql> show master status
 

 
舊從庫變主庫
mysql> STOP SLAVE;

mysql> RESET MASTER;

mysql> RESET SLAVE;

mysql> show master status \G
 

關閉舊主庫

/etc/init.d/mysql stop

待主庫應用切到備庫,關閉原主庫的表lock狀態
unlock tables; 

從庫變主庫
mysql> RESET MASTER;

mysql> RESET SLAVE;

主庫重新做備庫

新主庫:

mysql> grant replication slave  on *.* to 'root'@'192.168.1.29' identified by 'test123';

vi /etc/my.cnf

log-bin=mysql-masterbin

service mysql restart

mysql> flush tables with read lock;

mysql> show master status;    記下file和positon

 /usr/bin/mysqldump -uroot -ptest123 --log-error=mysqldump47.log --all-databases > mysqldump47.sql

mysql> unlock tables;

scp mysqldump47.sql 192.168.234.29:/root/   

新備庫:

mysql> stop slave;

mysql> /usr/bin/mysql -uroot -ptest123 < mysqldump47.sql

mysql> change master to master_host='192.168.1.47',master_user='root',master_password='test123',master_log_file='mysql-masterbin.00001' ,master_log_pos=65238;

mysql> start slave;

新主庫操作資料,新備庫驗證一下

附錄:

不重啟MySQL修改配置參數(萬不得已使用)

system gdb -p $(pidof mysqld) -ex "set log_bin = mysql-masterbin47" -batch
gdb -p $(pidof mysqld) -ex "set log_bin = mysql-masterbin47" -batch

資料庫唯讀鎖定命令,防止匯出資料庫的時候有資料寫入

flush tables with read lock;   

解除鎖定
 

unlock tables;

備份或者準備主備切換前設定資料為唯讀狀態(具有超級系統管理權限的使用者仍然可以DML動作表)

SET GLOBAL READ_ONLY = ON;

SET GLOBAL READ_ONLY = OFF; 

SHOW VARIABLES LIKE '%read_only%'; 

set global read_only=on/off是DBA經常用的一個操作:進行主備切換的時候,一般都會先對主庫進行唯讀操作(on),然後主備同步完成後,再把備庫置為可讀寫(off)。這樣可以避免切換的過程中雙寫引起髒資料。

mysqld.cc中同時定義了2個變數:my_bool read_only= 0, opt_readonly= 0; opt_readonly是當前系統的read_only狀態,read_only是要把read_only設定成的值。

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.