1. 程式人生 > >[mysqldump]不停止MySQL服務的情況下增加從庫

[mysqldump]不停止MySQL服務的情況下增加從庫

MySQL主從是資料庫高可用的最簡單做法之一,結合每天的定時冷備份,基本上能滿足小公司的資料備份要求。新增從庫是經常要做的的一個操作,下面是一種常見的不停止主庫實現從庫的方法(很多時候我們不能直接開啟讀鎖,那樣會對業務影響比較大),主要的工具是 mysqldump.

邏輯原理

很多情況下是給一個有資料,或者說運行了一段時間的資料庫來新增從庫,從邏輯上來說需要把主庫在某個時間的 日誌位置記錄下來, 然後把這個時間之前的資料匯入到從庫中, 從庫從這個日誌位置開始同步主庫的資料。

主庫中檢視日誌的位置

mysql> show master status \G
*****
********************** 1. row *************************** File: mysql-bin.000002 Position: 258814 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: a4e5dad2-7e20-11e6-8951-1df67d8f54f5:1-847 1 row in set (0.00 sec)

mysqldump 是mysql中自帶的一個備份工具,它可以在匯出資料檔案的同時記錄下資料庫當時的 position,資料dump的過程中資料庫鎖定,匯出的資料正好是這個 postiion之前的資料。 只要把匯出的資料匯入到從庫,然後從匯出的檔案中找到主庫的 position 並開始同步就行了。

操作案例

環境 Centos6.x ,MySQL 5.6.x

檢查配置

主庫配置 一般是 /etc/my.cnf 或者 /etc/mysql/my.cnf

server-id=1
binlog-format=mixed
log-bin=mysql-bin
datadir=/var/lib/mysql
innodb_flush_log_at_trx_commit=1
sync_binlog=1

注意從庫配置中如果有 server-id 不要和主庫相同

建立從庫使用者

主庫中建立一個使用者給從庫複製用

CREATE USER [email protected]<<slave-server-ip>>;
GRANT REPLICATION SLAVE ON *.* TO [email protected]<<slave-server-ip>> IDENTIFIED BY '<<choose-a-good-password>>'; FLUSH PRIVILEGES;

主庫資料匯出

mysqldump --skip-lock-tables --single-transaction --flush-logs --hex-blob --master-data=2 -A  > ~/dump.sql

能匯出 position的選項就是 --master-data, 下面是這個選項的解釋

Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. It causes the dump output to include a CHANGE MASTER TO statement that indicates the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates from which the slave should start replicating after you load the dump file into the slave. If the option value is 2, the CHANGE MASTER TO statement is written as an SQL comment, and thus is informative only; it has no effect when the dump file is reloaded. If the option value is 1, the statement is not written as a comment and takes effect when the dump file is reloaded. If no option value is specified, the default value is 1.

-A 匯出所有的 databases, 匯出某些 databases 可以用 -B, 具體看 mysqldump 的 help文件就好了。

檢視下主庫的 position, 通過查詢 MASTER_LOG_FILE and MASTER_LOG_POS

head dump.sql -n80 | grep "MASTER_LOG_POS"

壓縮資料

gzip ~/dump.sql

資料傳到從庫伺服器

方式多種多樣了,這裡用scp,用ftp啊都行

scp ~/dump.sql.gz mysql-user@<<slave-server-ip>>:~/

從庫配置

server-id= 101
binlog-format=mixed
log_bin=mysql-bin
relay-log=mysql-relay-bin
log-slave-updates=1
read-only=1

注意read-only 哈,配置之後就是個只讀庫了。

replicate-do-db=需要複製的資料庫名,如果複製多個數據庫,重複設定這個選項即可
replicate-ignore-db=需要複製的資料庫名,如果複製多個數據庫,重複設定這個選項即可

資料匯入從庫

gunzip ~/dump.sql.gz
mysql -u root -p < ~/dump.sql

建立主從關係

mysql> CHANGE MASTER TO MASTER_HOST='<<master-server-ip>>',MASTER_USER='replicant',MASTER_PASSWORD='<<slave-server-password>>', MASTER_LOG_FILE='<<value from above>>', MASTER_LOG_POS=<<value from above>>;
START SLAVE;

檢查主從情況

SHOW SLAVE STATUS \G

如果沒什麼問題的話,Last_Error 應該是空的,Slave_IO_State 應該會顯示 “Waiting for master to send event”。

還會看到

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

有時候可能會出錯,下面是如何重新建立主從關係的方法

STOP SLAVE;SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;START SLAVE;

Setting up MySQL replication using mysqldump without the downtime