歡迎您光臨本站 註冊首頁

mysql 添加第N個從庫

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0

某項目mysql伺服器 1主 1從 ,現在要添加一個mysql從伺服器,要求主庫不能停止服務,以前由於不是線上的伺服器,可以在主伺服器上 執行 flush tables with read lock 語句(鎖表,只讀),所有的表只能讀不能寫,然後再拷貝主庫數據到新的從庫伺服器上,並保持數據一致性,現在只能換一種方法了,思路 新從庫2拷貝老的從庫1的數據!


老從庫1 相關操作:

#1 停止 mysql從庫,鎖表,
記住 Read_Master_Log_Pos: 與 Master_Log_File: (紅色字)

  1. mysql> stop slave;
  2. mysql> flush tables with read lock;
  3. mysql> show slave status\G;
  4. *************************** 1. row ***************************
  5. Slave_IO_State:
  6. Master_Host: 192.168.6.53
  7. Master_User: dongnan
  8. Master_Port: 3306
  9. Connect_Retry: 60
  10. Master_Log_File: mysql-bin.000097
  11. Read_Master_Log_Pos: 19157395
  12. Relay_Log_File: zabbix-slave-relay-bin.000185
  13. Relay_Log_Pos: 11573578
  14. Relay_Master_Log_File: mysql-bin.000097
  15. Slave_IO_Running: No
  16. Slave_SQL_Running: No
  17. Empire CMS,phome.net
    Replicate_Do_DB:
  18. Replicate_Ignore_DB:
  19. Replicate_Do_Table:
  20. Replicate_Ignore_Table:
  21. Replicate_Wild_Do_Table:
  22. Replicate_Wild_Ignore_Table:
  23. Last_Errno: 0
  24. Last_Error:
  25. Skip_Counter: 0
  26. Exec_Master_Log_Pos: 19157395
  27. Relay_Log_Space: 19142103
  28. Until_Condition: None
  29. Until_Log_File:
  30. Until_Log_Pos: 0
  31. Master_SSL_Allowed: No
  32. Master_SSL_CA_File:
  33. Master_SSL_CA_Path:
  34. Master_SSL_Cert:
  35. Master_SSL_Cipher:
  36. Master_SSL_Key:
  37. Seconds_Behind_Master: NULL
  38. Master_SSL_Verify_Server_Cert: No
  39. Last_IO_Errno: 0
  40. Last_IO_Error:
  41. Last_SQL_Errno: 0
  42. Last_SQL_Error:
  43. 1 row in set (0.00 sec)
  44. ERROR:
  45. No query specified


#2 打包數據併發送到新從庫2伺服器上

  1. cd /usr/local/mysql/var/
  2. tar czvf zabbix_20110805.tar.gz zabbix/
  3. Empire CMS,phome.net
    scp zabbix_20110805.tar.gz root@192.168.6.54:/root



新從庫2相關操作:

#1 更改 server-id 值不能為1,master 的 server-id=1

  1. vim /etc/my.cnf
  2. server-id = 3

#2 停止mysql資料庫並解壓縮到/usr/local/mysql/var/

tar zxvf zabbix_20110805.tar.gz -C /usr/local/mysql/var/

#3 啟動mysql資料庫並change master

Read_Master_Log_Pos 值 19157395

Master_Log_File 值 mysql-bin.000097

  1. mysql> change master to master_host='192.168.6.53',master_user='dongnan',master_password='password',master_log_file='mysql-bin.000097',master_log_pos=19157395;
  2. mysql> slave start; #啟動slave
  3. mysql> show slave status\G; #顯示slave 狀態
  4. *************************** 1. row ***************************
    Empire CMS,phome.net
  5. Slave_IO_State: Waiting for master to send event
  6. Master_Host: 192.168.6.53
  7. Master_User: dongnan
  8. Master_Port: 3306
  9. Connect_Retry: 60
  10. Master_Log_File: mysql-bin.000097
  11. Read_Master_Log_Pos: 21194359
  12. Relay_Log_File: db1-relay-bin.000002
  13. Relay_Log_Pos: 2037215
  14. Relay_Master_Log_File: mysql-bin.000097
  15. Slave_IO_Running: Yes
  16. Slave_SQL_Running: Yes
  17. Replicate_Do_DB:
  18. Replicate_Ignore_DB:
  19. Replicate_Do_Table:
  20. Replicate_Ignore_Table:
  21. Replicate_Wild_Do_Table:
  22. Replicate_Wild_Ignore_Table:
  23. Last_Errno: 0
  24. Last_Error:
  25. Skip_Counter: 0
  26. Exec_Master_Log_Pos: 21194359
  27. Relay_Log_Space: 2037368
  28. Until_Condition: None
  29. Until_Log_File:
  30. Until_Log_Pos: 0
  31. Master_SSL_Allowed: No
  32. Master_SSL_CA_File:
  33. Master_SSL_CA_Path:
  34. Empire CMS,phome.net
    Master_SSL_Cert:
  35. Master_SSL_Cipher:
  36. Master_SSL_Key:
  37. Seconds_Behind_Master: 0
  38. Master_SSL_Verify_Server_Cert: No
  39. Last_IO_Errno: 0
  40. Last_IO_Error:
  41. Last_SQL_Errno: 0
  42. Last_SQL_Error:
  43. 1 row in set (0.00 sec)
  44. ERROR:
  45. No query specified



驗證重庫是否同步:

老從庫1

  1. mysql> slave start;
  2. mysql> show slave status\G;
  3. *************************** 1. row ***************************
  4. Slave_IO_State: Waiting for master to send event
  5. Master_Host: 192.168.6.53
  6. Master_User: dongnan
  7. Master_Port: 3306
  8. Connect_Retry: 60
  9. Master_Log_File: mysql-bin.000097
  10. Read_Master_Log_Pos: 21194359
  11. Relay_Log_File: db1-relay-bin.000002
  12. Relay_Log_Pos: 2037215
  13. Relay_Master_Log_File: mysql-bin.000097
  14. Slave_IO_Running: Yes
  15. Empire CMS,phome.net
  16. Slave_SQL_Running: Yes



新從庫2

  1. mysql> show slave status\G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.6.53
  5. Master_User: dongnan
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000097
  9. Read_Master_Log_Pos: 21194359
  10. Relay_Log_File: db1-relay-bin.000002
  11. Relay_Log_Pos: 2037215
  12. Relay_Master_Log_File: mysql-bin.000097
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes

結束

既然主庫不能動,那就去操作從庫吧,新從庫2拷貝老的從庫1的數據!



[火星人 ] mysql 添加第N個從庫已經有248次圍觀

http://coctec.com/docs/linux/show-post-45970.html