mysqldump --master-data參數實現主從複製快速部署
mysqldump --help
--master-data[=#] This causes the binary log position and filename to be
appended to the output. If equal to 1, will print it as a
CHANGE MASTER command; if equal to 2, that command will
be prefixed with a comment symbol. This option will turn
--lock-all-tables on, unless --single-transaction is
specified too (in which case a global read lock is only
taken a short time at the beginning of the dump; don't
forget to read about --single-transaction below). In all
cases, any action on logs will happen at the exact moment
of the dump. Option automatically turns --lock-tables
off.
--master-data[=#] 在備份匯出的檔案裡追加二進位binlog檔案的位置和名稱
如果值等於1,就會添加一個CHANGE MASTER語句
如果值等於2,就會在CHANGE MASTER語句前添加註釋(不起作用了唄~)
這個參數會--lock-all-tables鎖表,除非你指定了--single-transaction
這種情況下,鎖表只會在dump開始的時候持續一小段時間,照理說
在dump的時候,任何動作都會影響到binlog檔案
dump結束之後,選項會自動關閉鎖表功能
不知道翻譯的對不對,湊合看吧~~
簡單的說,就是主從複製在做全量備份的時候,這個選項可以自動幫我們鎖表和識別binlog臨界檔案,就不需要我們鎖表,再看臨界檔案編號,再執行CHANGE MASTER填寫binglong位置資訊到從庫master.info檔案中了,提高了從庫部署效率吧。
執行個體測試一下
備份當前資料庫
#注意 在做主從複本備份資料庫的時候,最好不要帶mysql內建的幾個庫,如mysql、information_schema否則開啟slave開關進行複製的時候會出現“Last_SQL_Error: Error 'Can't create database”錯誤,所以備份的時候要排除這幾個庫,又由於mysqldump只有ignore-table參數,並沒有ignore-database可以用一下命令實現
[root@db02 3309]# mysql -uroot -poldboy1234 -S /data/3306/mysql.sock -e "show databases;"| grep -Ev "Database|information_schema|performance_schema|mysql"|xargs mysqldump -uroot -poldboy1234 -S /data/3306/mysql.sock -B -F -R --master-data=1 --events|gzip > /server/backup/mysql_$(date +%F).sql.gz
--maste-data參數自動在備份檔案中添加了CHANGE MASTES TO...
我們將全量備份恢複到從庫
[root@db02 3309]# mysql -uroot -S /data/3309/mysql.sock < /server/backup/mysql_2016-07-07.sql
配置CHANGE MASTER TO..命令
mysql> CHANGE MASTER TO MASTER_HOST='172.16.2.10', MASTER_PORT=3306, MASTER_USER='rep', MASTER_PASSWORD='oldboy123';
# 注意此時我沒有配置MASTER_LOG_FILE和MASTER_LOG_POS
開啟salve 開關
mysql> start slave;
mysql> show slave status;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.2.10
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000023
Read_Master_Log_Pos: 279
Relay_Log_File: relay-bin.000037
Relay_Log_Pos: 344
Relay_Master_Log_File: mysql-bin.000023
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 279
Relay_Log_Space: 640
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
我們在主庫建立一個lilongzi資料庫,來驗證主庫和從庫是否串連成功
[root@db02 3309]# mysql -uroot -poldboy1234 -S /data/3306/mysql.sock
mysql> cr
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lilongzi |
| lilongzi_gbk |
| mysql |
| performance_schema |
| test |
| www |
| zzz |
+--------------------+
8 rows in set (0.00 sec)
eate database lilongzi;
從庫這邊
[root@db02 3309]# mysql -uroot -S /data/3309/mysql.sock
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lilongzi |
| lilongzi_gbk |
| mysql |
| performance_schema |
| test |
| www |
| zzz |
+--------------------+
8 rows in set (0.00 sec)
驗證成功!
本文永久更新連結地址: