1. 程式人生 > 其它 >centos7 mysql5.6 主從複製

centos7 mysql5.6 主從複製

架構與思維:高併發下解決主從延時的一些思路 - Hello-Brand - 部落格園 (cnblogs.com)

如何基於生產環境mysql 5.6.25主從部署新的mysql從庫操作指南_ITPUB部落格

mysql的主從複製延遲問題--看這一篇就夠了 - ityml - 部落格園 (cnblogs.com)

一、安裝環境

作業系統:CentOS-7-x86_64-DVD-1611.iso

資料庫版本:mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

資料庫地址:
  192.168.2.1(主)
  192.168.2.2(從)


  MySQL在5.6之前和之後的安裝方式是不一樣的。

 

自己整理的mysql安裝,主從配置基於這篇安裝:http://www.cnblogs.com/cypress/p/8608496.html

首先保證3306埠的可用,或者關閉防火牆,兩臺機子可以互相ping

二、Master的配置

1.修改MySQL配置檔案

?
1 [root@localhost ~]# vim /etc/my.cnf

  檔案內容

?
1 2 3 4 5 6 7 8 9 10 11 [mysqld] #開啟二進位制日誌 log-bin=mysql-bin #標識唯一id(必須),一般使用ip最後位 server-id=2 #不同步的資料庫,可設定多個
binlog-ignore-db=information_schema binlog-ignore-db=cluster binlog-ignore-db=mysql #指定需要同步的資料庫(和slave是相互匹配的),可以設定多個 binlog-do-db=test

 新增日誌儲存方式和規則(選填)

?
1 2 3 4 5 6 7 8 9 10 #設定儲存模式不設定預設 binlog_format=MIXED #日誌清理時間 expire_logs_days=7 #日誌大小 max_binlog_size=100m #快取大小 binlog_cache_size=4m
#最大快取大小 max_binlog_cache_size=521m

  

2.重啟MySQL

?
1 service mysqld restart

3.進去mysql設定允許從庫獲得主庫日誌  注:這裡使用root使用者配置,不建議使用,正常使用新建立的使用者進行授權

?
1 [root@localhost ~]# mysql -u root -p
?
1 2 3 4 5 #給從庫放許可權 mysql>GRANT FILE ON *.* TO 'root'@'192.168.2.2' IDENTIFIED BY 'root password';   #建立使用者 mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.2.2' IDENTIFIED BY 'root password';  #修改使用者許可權 mysql>select host ,user ,password from mysql.user;  #檢視是否修改成功 mysql>FLUSH PRIVILEGES;   #重新整理

  

注:如果資料庫有資料需要進行資料遷移保證資料的一致性  資料遷移

建立資料庫: 在從庫中建立一個和主庫相同的資料庫,不然兩個資料庫不能同步(進行過資料遷移就跳過)

?
1 CREATE DATABASE test CHARACTER SET utf8 COLLATE utf8_general_ci;

4.重啟MySQL,登入MySQL,檢視主庫資訊

?
1 mysql> show master status;

 顯示內容

?
1 2 3 4 5 6 7 8 +------------------+----------+--------------+----------------------------------+-------------------+ | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                 | Executed_Gtid_Set | +------------------+----------+--------------+----------------------------------+-------------------+ | mysql-bin.000006 |    120 | ufind_db     | information_schema,cluster,mysql |                   | +------------------+----------+--------------+----------------------------------+-------------------+ 1 row in set (0.00 sec)   mysql>

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

三、Slave的配置

1.從庫配置

?
1 2 3 4 5 6 7 8 9 10 #開啟二進位制日誌(可以不配置)log-bin=mysql-bin server-id=3 binlog-ignore-db=information_schema binlog-ignore-db=cluster binlog-ignore-db=mysql#與主庫配置一直 replicate-do-db=test replicate-ignore-db=mysql log-slave-updates slave-skip-errors=all slave-net-timeout=60

4.重啟MySQL,登入MySQL

?
1 2 3 4 5 #關閉Slave mysql> stop slave;  #設定連線主庫資訊 mysql> change master to master_host='192.168.2.1',master_user='root',master_password='root password',master_log_file='mysql-bin.000006', master_log_pos=120; #開啟Slave mysql> start slave;

  注:上面的master_log_file是在配置Master的時候的File欄位, master_log_pos是在配置Master的Position 欄位。一定要一一對應

5.檢視資訊

?
1 mysql> show slave status \G;

  

?
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 *************************** 1. row ***************************                Slave_IO_State: Waiting for master to send event                   Master_Host: 192.168.2.1                   Master_User: root                   Master_Port: 3306                 Connect_Retry: 60               Master_Log_File: mysql-bin.000006           Read_Master_Log_Pos: 120                Relay_Log_File: localhost-relay-bin.000006                 Relay_Log_Pos: 520         Relay_Master_Log_File: mysql-bin.000006              Slave_IO_Running: Yes  //顯示yes為成功             Slave_SQL_Running: Yes  //顯示yes為成功,如果為no,一般為沒有啟動master               Replicate_Do_DB: test           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: 357               Relay_Log_Space: 697               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:  //如果為no,此處會顯示錯誤資訊                Last_SQL_Errno: 0                Last_SQL_Error:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 2                   Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a              Master_Info_File: /usr/local/mysql/data/master.info                     SQL_Delay: 0           SQL_Remaining_Delay: NULL       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it            Master_Retry_Count: 86400                   Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set:             Executed_Gtid_Set:                 Auto_Position: 0 1 row in set (0.00 sec)   ERROR: No query specified

  注:如果Slave_IO_Running: No  出現下面的錯誤

?
1 Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

  說明主伺服器的UUID和從伺服器的UUID重複,更改方式

?
1 [root@localhost ~]# vim /usr/local/mysql/data/auto.cnf  #這是我的安裝路徑修改auto.cnf的server-uuid

  注:如果Slave_IO_Running: Connecting 並出現下面錯誤  

?
1 error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1

  解決方法,檢視主庫是否授權,檢視change master to... 是否有使用者密碼ip填寫錯誤

   注:如果Slave_IO_Running: No 出現下面錯誤

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

  解決方法:復位

mysql>stop slave;  //停止
mysql>reset slave;  //清空
mysql>start slave;  //開啟

以上主從MySQL已經可以使用了,歡迎各位多提bug

小擴充套件

當只針對某些庫的某張表進行同步時,如下,只同步huanqiu庫的haha表和huanpc庫的heihei表:
replicate-do-db = huanqiu
replicate-wild-do-table = huanqiu.haha       //當只同步幾個或少數表時,可以這樣設定。注意這要跟上面的庫指定配合使用;
replicate-do-db = huanpc
replicate-wild-do-table = huanpc.heihei      //如果同步的庫的表比較多時,就不能這樣一一指定了,就把這個選項配置去掉,直接根據指定的庫進行同步。

 關於增刪改查,主從資料不一致問題:

?
1 2 3 4 5 6 7 8 9 #select 語句,暫時沒有發現問題   #insert 語句,暫時沒有發現問題   #update 語句,暫時沒有發現問題   #delete 語句,主庫刪除多條資料,發現數據不一致 原因:在主庫的logbin中的確有執行刪除語句,但是在從庫的logbin中卻沒有刪除語句 解決:使用 use database 選取當前資料庫架構中的需要操作的資料庫,然後在執行刪除,OK同步成功

  

查詢binlog主從日誌的方法

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #檢視binlog全部檔案 mysql>show binary logs;   #檢視binlog是否開啟NO為開啟 mysql> show variables like 'log_bin%';   #詳細資訊 mysql>  show variables like 'binlog%';   #檢視binlog日誌 mysql> show binlog events in'mysql-bin.000019';   #或者使用mysqlbinlog,如果報錯使用--no-defaults(使用全路徑) [root@localhost ~]# /usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000019

  

手動清理master日誌,最好關閉日誌,在/etc/my.cnf

?
1 2 3 #手動重新整理日誌 mysql> show master status; #刪除全部mysql> reset slave;或 rest master;#刪除MySQL-bin.010mysql> PURGE MASTER LOGS TO 'MySQL-bin.010';

架構與思維:高併發下解決主從延時的一些思路 - Hello-Brand - 部落格園 (cnblogs.com)

如何基於生產環境mysql 5.6.25主從部署新的mysql從庫操作指南_ITPUB部落格

mysql的主從複製延遲問題--看這一篇就夠了 - ityml - 部落格園 (cnblogs.com)

一、安裝環境

作業系統:CentOS-7-x86_64-DVD-1611.iso

資料庫版本:mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

資料庫地址:
  192.168.2.1(主)
  192.168.2.2(從)


  MySQL在5.6之前和之後的安裝方式是不一樣的。 

自己整理的mysql安裝,主從配置基於這篇安裝:http://www.cnblogs.com/cypress/p/8608496.html

首先保證3306埠的可用,或者關閉防火牆,兩臺機子可以互相ping

二、Master的配置

1.修改MySQL配置檔案

?
1 [root@localhost ~]# vim /etc/my.cnf

  檔案內容

?
1 2 3 4 5 6 7 8 9 10 11 [mysqld] #開啟二進位制日誌 log-bin=mysql-bin #標識唯一id(必須),一般使用ip最後位 server-id=2 #不同步的資料庫,可設定多個 binlog-ignore-db=information_schema binlog-ignore-db=cluster binlog-ignore-db=mysql #指定需要同步的資料庫(和slave是相互匹配的),可以設定多個 binlog-do-db=test

 新增日誌儲存方式和規則(選填)

?
1 2 3 4 5 6 7 8 9 10 #設定儲存模式不設定預設 binlog_format=MIXED #日誌清理時間 expire_logs_days=7 #日誌大小 max_binlog_size=100m #快取大小 binlog_cache_size=4m #最大快取大小 max_binlog_cache_size=521m

  

2.重啟MySQL

?
1 service mysqld restart

3.進去mysql設定允許從庫獲得主庫日誌  注:這裡使用root使用者配置,不建議使用,正常使用新建立的使用者進行授權

?
1 [root@localhost ~]# mysql -u root -p
?
1 2 3 4 5 #給從庫放許可權 mysql>GRANT FILE ON *.* TO 'root'@'192.168.2.2' IDENTIFIED BY 'root password';   #建立使用者 mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.2.2' IDENTIFIED BY 'root password';  #修改使用者許可權 mysql>select host ,user ,password from mysql.user;  #檢視是否修改成功 mysql>FLUSH PRIVILEGES;   #重新整理

  

注:如果資料庫有資料需要進行資料遷移保證資料的一致性  資料遷移

建立資料庫: 在從庫中建立一個和主庫相同的資料庫,不然兩個資料庫不能同步(進行過資料遷移就跳過)

?
1 CREATE DATABASE test CHARACTER SET utf8 COLLATE utf8_general_ci;

4.重啟MySQL,登入MySQL,檢視主庫資訊

?
1 mysql> show master status;

 顯示內容

?
1 2 3 4 5 6 7 8 +------------------+----------+--------------+----------------------------------+-------------------+ | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                 | Executed_Gtid_Set | +------------------+----------+--------------+----------------------------------+-------------------+ | mysql-bin.000006 |    120 | ufind_db     | information_schema,cluster,mysql |                   | +------------------+----------+--------------+----------------------------------+-------------------+ 1 row in set (0.00 sec)   mysql>

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

三、Slave的配置

1.從庫配置

?
1 2 3 4 5 6 7 8 9 10 #開啟二進位制日誌(可以不配置)log-bin=mysql-bin server-id=3 binlog-ignore-db=information_schema binlog-ignore-db=cluster binlog-ignore-db=mysql#與主庫配置一直 replicate-do-db=test replicate-ignore-db=mysql log-slave-updates slave-skip-errors=all slave-net-timeout=60

4.重啟MySQL,登入MySQL

?
1 2 3 4 5 #關閉Slave mysql> stop slave;  #設定連線主庫資訊 mysql> change master to master_host='192.168.2.1',master_user='root',master_password='root password',master_log_file='mysql-bin.000006', master_log_pos=120; #開啟Slave mysql> start slave;

  注:上面的master_log_file是在配置Master的時候的File欄位, master_log_pos是在配置Master的Position 欄位。一定要一一對應

5.檢視資訊

?
1 mysql> show slave status \G;

  

?
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 *************************** 1. row ***************************                Slave_IO_State: Waiting for master to send event                   Master_Host: 192.168.2.1                   Master_User: root                   Master_Port: 3306                 Connect_Retry: 60               Master_Log_File: mysql-bin.000006           Read_Master_Log_Pos: 120                Relay_Log_File: localhost-relay-bin.000006                 Relay_Log_Pos: 520         Relay_Master_Log_File: mysql-bin.000006              Slave_IO_Running: Yes  //顯示yes為成功             Slave_SQL_Running: Yes  //顯示yes為成功,如果為no,一般為沒有啟動master               Replicate_Do_DB: test           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: 357               Relay_Log_Space: 697               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:  //如果為no,此處會顯示錯誤資訊                Last_SQL_Errno: 0                Last_SQL_Error:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 2                   Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a              Master_Info_File: /usr/local/mysql/data/master.info                     SQL_Delay: 0           SQL_Remaining_Delay: NULL       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it            Master_Retry_Count: 86400                   Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set:             Executed_Gtid_Set:                 Auto_Position: 0 1 row in set (0.00 sec)   ERROR: No query specified

  注:如果Slave_IO_Running: No  出現下面的錯誤

?
1 Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

  說明主伺服器的UUID和從伺服器的UUID重複,更改方式

?
1 [root@localhost ~]# vim /usr/local/mysql/data/auto.cnf  #這是我的安裝路徑修改auto.cnf的server-uuid

  注:如果Slave_IO_Running: Connecting 並出現下面錯誤  

?
1 error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1

  解決方法,檢視主庫是否授權,檢視change master to... 是否有使用者密碼ip填寫錯誤

   注:如果Slave_IO_Running: No 出現下面錯誤

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

  解決方法:復位

mysql>stop slave;  //停止
mysql>reset slave;  //清空
mysql>start slave;  //開啟

以上主從MySQL已經可以使用了,歡迎各位多提bug

小擴充套件

當只針對某些庫的某張表進行同步時,如下,只同步huanqiu庫的haha表和huanpc庫的heihei表:
replicate-do-db = huanqiu
replicate-wild-do-table = huanqiu.haha       //當只同步幾個或少數表時,可以這樣設定。注意這要跟上面的庫指定配合使用;
replicate-do-db = huanpc
replicate-wild-do-table = huanpc.heihei      //如果同步的庫的表比較多時,就不能這樣一一指定了,就把這個選項配置去掉,直接根據指定的庫進行同步。

 關於增刪改查,主從資料不一致問題:

?
1 2 3 4 5 6 7 8 9 #select 語句,暫時沒有發現問題   #insert 語句,暫時沒有發現問題   #update 語句,暫時沒有發現問題   #delete 語句,主庫刪除多條資料,發現數據不一致 原因:在主庫的logbin中的確有執行刪除語句,但是在從庫的logbin中卻沒有刪除語句 解決:使用 use database 選取當前資料庫架構中的需要操作的資料庫,然後在執行刪除,OK同步成功

  

查詢binlog主從日誌的方法

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #檢視binlog全部檔案 mysql>show binary logs;   #檢視binlog是否開啟NO為開啟 mysql> show variables like 'log_bin%';   #詳細資訊 mysql>  show variables like 'binlog%';   #檢視binlog日誌 mysql> show binlog events in'mysql-bin.000019';   #或者使用mysqlbinlog,如果報錯使用--no-defaults(使用全路徑) [root@localhost ~]# /usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000019

  

手動清理master日誌,最好關閉日誌,在/etc/my.cnf

?
1 2 3 #手動重新整理日誌 mysql> show master status; #刪除全部mysql> reset slave;或 rest master;#刪除MySQL-bin.010mysql> PURGE MASTER LOGS TO 'MySQL-bin.010';