MySQ 資料庫主從同步安裝與配置詳解(Master/Slave)

劉美利發表於2018-07-30

MySQL5.6 資料庫主從同步安裝與配置詳解(Master/Slave)
本篇文章主要介紹了MySQL5.6 資料庫主從同步安裝與配置詳解,具有一定的參考價值,有興趣的可以瞭解一下。
安裝環境

作業系統 :CentOS 6.5 

資料庫版本:MySQL 5.6.27

主機A:192.168.1.1 (Master)

主機B:192.168.1.2 (Slave)

這裡強調的資料庫的版本,是因為MySQL在5.6之前和之後的安裝方式是不一樣的。 

本人在進行配置的時候,也遇到了這個坑,這裡提前說明,希望大家不要採坑。

注:這裡有一篇CentOS安裝MySQL的文章,在這裡引出,過程親測,希望對大家有幫助:http://www.jb51.net/article/101778.htm

基本環境配置

首先,要保證防火牆對3306埠的開啟,如果只是為了學習資料庫的主從配置,可以使用service iptables stop 命令直接關閉防火牆。

然後可以在兩臺機子之間進行 ping操作,確保兩臺機器之間能夠相同。

Master的配置

在Linux環境下MySQL的配置檔案的位置是在 /etc/my.cnf ,在該檔案下指定Master的配置如下:


  1. log-bin=mysql-bin

  2. server-id=2

  3. binlog-ignore-db=information_schema

  4. binlog-ignore-db=cluster

  5. binlog-ignore-db=mysql

  6. binlog-do-db=ufind_db


這裡的server-id用於標識唯一的資料庫,這裡設定為2,在設定從庫的時候就需要設定為其他值。

binlog-ignore-db:表示同步的時候ignore的資料庫 

binlog-do-db:指定需要同步的資料庫

完整配置截圖如下:

1、然後重啟mysql:


  1. service mysqld restart


2、進入mysql:[root@VM_221_4_centos ~]# mysql -u root -p 回車,輸入mysql密碼進入。

3、 賦予從庫許可權帳號,允許使用者在主庫上讀取日誌,賦予192.168.1.2也就是Slave機器有File許可權,只賦予Slave機器有File許可權還不行,還要給它REPLICATION SLAVE的許可權才可以。

在Master資料庫命令列中輸入:


  1. >GRANT FILE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';


  2. >GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';

  3. >FLUSH PRIVILEGES



這裡使用的仍是 root 使用者作為同步的時候使用到的使用者,可以自己設定。

4、重啟mysql,登入mysql,顯示主庫資訊


  1. mysql> show master status;



  1. mysql> show master status;

  2. +------------------+----------+--------------+----------------------------------+-------------------+

  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

  4. +------------------+----------+--------------+----------------------------------+-------------------+

  5. | mysql-bin.000004 | 28125 | ufind_db | information_schema,cluster,mysql | |

  6. +------------------+----------+--------------+----------------------------------+-------------------+

  7. 1 row in set (0.00 sec)


  8. mysql>



這裡的 File 、Position 是在配置Salve的時候要使用到的,Binlog_Do_DB表示要同步的資料庫,Binlog_Ignore_DB 表示Ignore的資料庫,這些都是在配置的時候進行指定的。

另外:如果執行這個步驟始終為Empty set(0.00 sec),那說明前面的my.cnf沒配置對。

Slave的配置

1、從庫的配置,首先也是修改配置檔案:/etc/my.cnf 如下:


  1. log-bin=mysql-bin

  2. server-id=3

  3. binlog-ignore-db=information_schema

  4. binlog-ignore-db=cluster

  5. binlog-ignore-db=mysql

  6. replicate-do-db=ufind_db

  7. replicate-ignore-db=mysql

  8. log-slave-updates

  9. slave-skip-errors=all

  10. slave-net-timeout=60


2、這裡可以看到,在MySQL5.6之後的版本中沒有指定:


  1. master-host=192.168.1.1 #Master的主機IP

  2. master-user=root

  3. master-password=mysql password #Master的MySQL密碼


3、這也是在網上很多搜尋的配置過程,他們也都指定了資料庫的版本,但是並沒有說出來新版本的配置這種方式是不適用的。

4、如果,你在MySQL5.6和之後的版本中配置從庫的時候,設定到了上邊的內容,即指定了master-host、master-user等資訊的話,重啟MySQL的時候就回報錯,錯誤資訊如下:


  1. [root@VM_128_194_centos bin]# service mysqld restart

  2. Shutting down MySQL... SUCCESS!

  3. Starting MySQL... ERROR! The server quit without updating PID file (/data/mysqldb/VM_128_194_centos.pid).

  4. [root@VM_128_194_centos bin]#


此時,檢視資料庫的報錯資訊(資料庫的目錄, /data/mysqldb/VM_128_194_centos.err ),可以看到:


  1. 2016-05-06 13:12:04 13345 [Note] InnoDB: Waiting for purge to start

  2. 2016-05-06 13:12:04 13345 [Note] InnoDB: 5.6.27 started; log sequence number 2850211

  3. 2016-05-06 13:12:04 13345 [ERROR] /data/home/server/mysql-5.6.27/bin/mysqld: unknown variable 'master-host=192.168.1.1'

  4. 2016-05-06 13:12:04 13345 [ERROR] Aborting


可以看出master-host 被檢測數是一個未知的變數,因此會出現錯誤。

5、在5.6以及後續版本的配置如下:

修改完/etc/my.cnf 檔案之後,重啟一下MySQL(service mysqld restart)

進入Slave mysql控制檯,執行:


  1. mysql> stop slave; #關閉Slave

  2. mysql> change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000004', master_log_pos=28125;


  3. mysql> start slave; #開啟Slave



在這裡指定Master的資訊,master_log_file是在配置Master的時候的File選項, master_log_pos是在配置Master的Position 選項,這裡要進行對應。

然後可以通過mysql> show slave status; 檢視配置的資訊:


  1. mysql> show slave status \G;

  2. *************************** 1. row ***************************

  3. Slave_IO_State: Waiting for master to send event

  4. Master_Host: 192.167.1.1

  5. Master_User: root

  6. Master_Port: 3306

  7. Connect_Retry: 60

  8. Master_Log_File: mysql-bin.000004

  9. Read_Master_Log_Pos: 28125

  10. Relay_Log_File: VM_128_194_centos-relay-bin.000004

  11. Relay_Log_Pos: 26111

  12. Relay_Master_Log_File: mysql-bin.000004

  13. Slave_IO_Running: Yes

  14. Slave_SQL_Running: Yes

  15. Replicate_Do_DB: ufind_db

  16. Replicate_Ignore_DB: mysql

  17. Replicate_Do_Table:

  18. Replicate_Ignore_Table:

  19. Replicate_Wild_Do_Table:

  20. Replicate_Wild_Ignore_Table:

  21. Last_Errno: 0

  22. Last_Error:

  23. Skip_Counter: 0

  24. Exec_Master_Log_Pos: 28125

  25. Relay_Log_Space: 26296

  26. Until_Condition: None

  27. Until_Log_File:

  28. Until_Log_Pos: 0

  29. Master_SSL_Allowed: No

  30. Master_SSL_CA_File:

  31. Master_SSL_CA_Path:

  32. Master_SSL_Cert:

  33. Master_SSL_Cipher:

  34. Master_SSL_Key:

  35. Seconds_Behind_Master: 0

  36. Master_SSL_Verify_Server_Cert: No

  37. Last_IO_Errno: 0

  38. Last_IO_Error:

  39. Last_SQL_Errno: 0

  40. Last_SQL_Error:

  41. Replicate_Ignore_Server_Ids:

  42. Master_Server_Id: 2

  43. Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd

  44. Master_Info_File: /data/mysqldb/master.info

  45. SQL_Delay: 0

  46. SQL_Remaining_Delay: NULL

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

  48. Master_Retry_Count: 86400

  49. Master_Bind:

  50. Last_IO_Error_Timestamp:

  51. Last_SQL_Error_Timestamp:

  52. Master_SSL_Crl:

  53. Master_SSL_Crlpath:

  54. Retrieved_Gtid_Set:

  55. Executed_Gtid_Set:

  56. Auto_Position: 0

  57. 1 row in set (0.00 sec)


  58. ERROR:

  59. No query specified


  60. mysql>




可以看到,已經配置成功。

新增需要同步的從庫Slave

由於種種原因,測試的時候使用test庫,這裡我按照上述的方式,修改Master的my.cnf的配置檔案,新增同步的資料庫test,重啟MySQL,執行Master的:show master status如下:

相應的,要修改Slave從庫的資訊在my.cnf 增加 replicate-do-db=test,重啟Mysql,根據上述的show master status,在Slave從庫中執行下邊的內容:


  1. >stop slave

  2. >change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000005', master_log_pos=120;

  3. >start slave




然後使用:show slave status;


  1. mysql> show slave status\G;

  2. *************************** 1. row ***************************

  3. Slave_IO_State: Waiting for master to send event

  4. Master_Host: 192.168.1.1

  5. Master_User: root

  6. Master_Port: 3306

  7. Connect_Retry: 60

  8. Master_Log_File: mysql-bin.000005

  9. Read_Master_Log_Pos: 1422

  10. Relay_Log_File: VM_128_194_centos-relay-bin.000004

  11. Relay_Log_Pos: 283

  12. Relay_Master_Log_File: mysql-bin.000005

  13. Slave_IO_Running: Yes

  14. Slave_SQL_Running: Yes

  15. Replicate_Do_DB: ufind_db,test

  16. Replicate_Ignore_DB: mysql

  17. Replicate_Do_Table:

  18. Replicate_Ignore_Table:

  19. Replicate_Wild_Do_Table:

  20. Replicate_Wild_Ignore_Table:

  21. Last_Errno: 0

  22. Last_Error:

  23. Skip_Counter: 0

  24. Exec_Master_Log_Pos: 1422

  25. Relay_Log_Space: 468

  26. Until_Condition: None

  27. Until_Log_File:

  28. Until_Log_Pos: 0

  29. Master_SSL_Allowed: No

  30. Master_SSL_CA_File:

  31. Master_SSL_CA_Path:

  32. Master_SSL_Cert:

  33. Master_SSL_Cipher:

  34. Master_SSL_Key:

  35. Seconds_Behind_Master: 0

  36. Master_SSL_Verify_Server_Cert: No

  37. Last_IO_Errno: 0

  38. Last_IO_Error:

  39. Last_SQL_Errno: 0

  40. Last_SQL_Error:

  41. Replicate_Ignore_Server_Ids:

  42. Master_Server_Id: 2

  43. Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd

  44. Master_Info_File: /data/mysqldb/master.info

  45. SQL_Delay: 0

  46. SQL_Remaining_Delay: NULL

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

  48. Master_Retry_Count: 86400

  49. Master_Bind:

  50. Last_IO_Error_Timestamp:

  51. Last_SQL_Error_Timestamp:

  52. Master_SSL_Crl:

  53. Master_SSL_Crlpath:

  54. Retrieved_Gtid_Set:

  55. Executed_Gtid_Set:

  56. Auto_Position: 0

  57. 1 row in set (0.00 sec)


  58. ERROR:

  59. No query specified


  60. mysql>




已經新增加了test。

真正的測試

在主庫中新增資料庫表,user

【本文轉載自 Java高階架構師 ,原文連結:https://mp.weixin.qq.com/s/N7Q7nT5lgPwiT80huthyPw,轉載授權請聯絡原作者】

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31542119/viewspace-2168639/,如需轉載,請註明出處,否則將追究法律責任。

相關文章