參考:
如何在 MySQL 5.5 設定單向資料庫複寫機制 (Replication)
Setting up MySQL replication without the downtime
-
定義
Master => 被複製抄寫的伺服器
Slave => 執行抄寫的伺服器 -
修改 Master 端的 mysql 設定
vim /etc/my.cnf (或 vim /etc/ysql/my.cnf)
1 2 3 4 5 6 7 |
#在[mysqld]區段中加入 server-id=1 log-bin=mysql-bin #需要同步的數據庫名 binlog-do-db= radius #避免同步的數據庫名 binlog-ignore-db= mysql |
- 重新啟動 mysql server
1 |
service mysqld reboot |
- 檢查Master設定是否正常
1 2 3 4 5 6 7 8 |
mysql -uroot -p mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 11530633 | radius | | +------------------+----------+--------------+------------------+ 1 row in set (0.06 sec) |
- 於Master創建抄寫用的使用者
1 |
mysql> CREATE USER '使用者'@'來源位置' IDENTIFIED BY '密碼'; |
- 於Master給予抄寫使用者權限 (錯誤版)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
mysql> GRANT REPLICATION SLAVE ON radius.* TO '使用者'@'來源位置'; #==> ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES #因為無法單獨給予某個資料庫REPLICATION的權限, 所以會在master及slave的my.cnf中設定限制 #ex. # <in master> # server-id=1 # log-bin=mysql-bin # binlog-do-db= radius #需要同步的數據庫名 # binlog-ignore-db= mysql #避免同步的數據庫名 # # <in slave> # server-id=2 # log-bin=mysql-bin # binlog-do-db= radius #需要同步的數據庫名 # 於Master給予抄寫使用者權限 (正確版) mysql> GRANT REPLICATION SLAVE ON *.* TO '使用者'@'來源位置'; |
- 套用設定
1 |
mysql> FLUSH PRIVILEGES; |
- Master匯出要同步的資料庫
1 2 3 |
mysqldump -u root -p --skip-lock-tables --single-transaction --master-data [選擇的資料庫名稱] > [輸出的檔名] # 如果要輸出所有資料庫 # mysqldump -u root -p --skip-lock-tables --single-transaction --master-data --all-databases > [輸出的檔名] |
- 傳送到抄寫伺服器
1 |
scp [輸出的檔名] [使用者]@[伺服器IP]:[存放位置] |
- 於抄寫伺服器匯入資料庫
1 2 3 |
mysql -u root -p [指定的資料庫名稱] --default-character-set=utf8 < [輸出的檔名] # 如果是之前是匯出所有資料庫,這邊就倒入所有資料庫 # mysql -u root -p --default-character-set=utf8 < [輸出的檔名] |
- 修改 Slave 端的 mysql 設定
1 2 3 4 5 |
vim /etc/my.cnf (或 vim /etc/ysql/my.cnf) #在[mysqld]區段中加入 server-id=2 #需要同步的數據庫名 binlog-do-db= radius |
- 重新啟動 mysql server
1 |
service mysqld reboot |
- 設定 Master 的登入資訊,並啟動slave
1 2 |
mysql -uroot -p mysql> CHANGE MASTER TO MASTER_HOST='[Master IP]', MASTER_PORT=3306, MASTER_USER='[使用者]', MASTER_PASSWORD='[密碼]'; |
- 啟動slave抄寫
1 |
mysql> START SLAVE; |
- 檢查Slave設定是否正常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
mysql> SHOW SLAVE STATUS G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.55.142 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 2381281 Relay_Log_File: mysqld-relay-bin.000011 Relay_Log_Pos: 31802 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: 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: 2381281 Relay_Log_Space: 31802 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 1 row in set (0.00 sec) |
- 如果發生錯誤
查看上面出現的Last_Errno: [編號]
1 |
mysql> stop slave; SET GLOBAL sql_slave_skip_counter=[編號]; start slave; |