溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

實(shí)現(xiàn)mysql級(jí)聯(lián)復(fù)制的方法示例

發(fā)布時(shí)間:2020-10-09 07:38:08 來源:腳本之家 閱讀:177 作者:小李子博客 欄目:MySQL數(shù)據(jù)庫

所謂級(jí)聯(lián)復(fù)制就是master服務(wù)器,只給一臺(tái)slave服務(wù)器同步數(shù)據(jù),然后slave服務(wù)器在向后端的所有slave服務(wù)器同步數(shù)據(jù),降低master服務(wù)器的寫壓力,和復(fù)制數(shù)據(jù)的網(wǎng)絡(luò)IO。

一,配置master服務(wù)器

1,修改主配置文件

vim /etc/my.cnf

在[mysql]配置塊下添加如下兩行配置

[mysql]
log_bin     #開啟二進(jìn)制日志功能
server_id=1   #為當(dāng)前節(jié)點(diǎn)設(shè)置一個(gè)全局惟一的ID號(hào) 

2,重啟mysql服務(wù),使配置生效

systemctl restart mairadb

3,創(chuàng)建有復(fù)制權(quán)限的用戶賬號(hào)

GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'HOST' IDENTIFIED BY 'replpass'; 

命令解析:

  • 'repluser'@'HOST' :設(shè)置用戶名即主機(jī)ip或網(wǎng)段,網(wǎng)段用%表示 例如10.0.0.%
  •  IDENTIFIED BY:設(shè)置密碼
  • *.* :表示所有數(shù)據(jù)庫,所有表
  • GRANT REPLCATION SLAVE:就是允許該用戶復(fù)制數(shù)據(jù)

該命令作用就是授權(quán)repluser能拷貝數(shù)據(jù)庫的所有內(nèi)容

二,中繼slave服務(wù)器配置

1,修改主配置文件

vim /etc/my.cnf

在[mysql]配置塊中添加如下兩行配置

[mysqld]  
 log_bin
server_id=2   #為當(dāng)前節(jié)點(diǎn)設(shè)置一個(gè)全局惟一的ID號(hào) 
read_only=ON #限制從服務(wù)器為只讀."注意:此限制對(duì)擁有SUPER權(quán)限的用戶均無效"
log_slave_updates #該項(xiàng)的作用是把master服務(wù)器的二進(jìn)制日志計(jì)入到本機(jī),然后再把二進(jìn)制日志復(fù)制給后端的其他slave服務(wù)器

2,重啟mysql服務(wù),使配置生效

systemctl restart mariadb

3,使用有復(fù)制權(quán)限的用戶賬號(hào)連接至主服務(wù)器,并啟動(dòng)復(fù)制線程

   CHANGE MASTER TO 
   MASTER_HOST='host',    #指定master主機(jī)IP
   MASTER_USER='repluser',  #指定master被授權(quán)的用戶名
   MASTER_PASSWORD='replpass',#指定被授權(quán)的用戶密碼 MASTER_LOG_FILE='mysql-bin.xxxxx', #指定從master服務(wù)器的那個(gè)二進(jìn)制日志開始復(fù)制
   MASTER_LOG_POS=#;     #二進(jìn)制日志位置,可以在master服務(wù)器上執(zhí)行該命令查看,show master logs;

   啟動(dòng)復(fù)制線程IO_THREAD和SQL_THREAD
   START SLAVE; 

4,查看中繼slave服務(wù)器狀態(tài)

  MariaDB [(none)]> start slave;
  Query OK, 0 rows affected (0.00 sec)

  MariaDB [(none)]> show slave status\G
  *************************** 1. row ***************************
          Slave_IO_State: Waiting for master to send event
           Master_Host: 192.168.68.7
           Master_User: repluser
           Master_Port: 3306
          Connect_Retry: 60
         Master_Log_File: mariadb-bin.000001
       Read_Master_Log_Pos: 557
          Relay_Log_File: mariadb-relay-bin.000002
          Relay_Log_Pos: 843
      Relay_Master_Log_File: mariadb-bin.000001
         Slave_IO_Running: Yes "重點(diǎn)關(guān)注如果是NO表示線程沒起來"
        Slave_SQL_Running: Yes "重點(diǎn)關(guān)注 如果是NO表示該線程沒起來"
         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: 557
         Relay_Log_Space: 1139
         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 "該項(xiàng)表示同步時(shí)間 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

三,后端slave配置

1,修改配置文件

vim /etc/my.cnf

在[mysql]配置塊中添加如下兩行配置

[mysqld]  
server_id=3   #為當(dāng)前節(jié)點(diǎn)設(shè)置一個(gè)全局惟一的ID號(hào)
read_only=ON #限制從服務(wù)器為只讀."注意:此限制對(duì)擁有SUPER權(quán)限的用戶均無效"

2,重啟mysql服務(wù),使配置生效

systemctl restart mariadb

3,使用有復(fù)制權(quán)限的用戶賬號(hào)連接至主服務(wù)器,并啟動(dòng)復(fù)制線程

CHANGE MASTER TO 
   MASTER_HOST='中繼host',    #指定中繼slave主機(jī)IP
   MASTER_USER='repluser',  #指定master被授權(quán)的用戶名
   MASTER_PASSWORD='replpass',#指定被授權(quán)的用戶密碼 MASTER_LOG_FILE='mysql-bin.xxxxx', #指定從中繼slave服務(wù)器的那個(gè)二進(jìn)制日志開始復(fù)制
   MASTER_LOG_POS=#;     #二進(jìn)制日志位置,可以在slave服務(wù)器上執(zhí)行該命令查看,show master logs;

   啟動(dòng)復(fù)制線程IO_THREAD和SQL_THREAD
   START SLAVE; 

4,查看slave服務(wù)器狀態(tài)

  MariaDB [(none)]> start slave;
  Query OK, 0 rows affected (0.00 sec)

  MariaDB [(none)]> show slave status\G
  *************************** 1. row ***************************
          Slave_IO_State: Waiting for master to send event
           Master_Host: 192.168.68.17
           Master_User: repluser
           Master_Port: 3306
          Connect_Retry: 60
         Master_Log_File: mariadb-bin.000001
       Read_Master_Log_Pos: 557
          Relay_Log_File: mariadb-relay-bin.000002
          Relay_Log_Pos: 843
      Relay_Master_Log_File: mariadb-bin.000001
         Slave_IO_Running: Yes "重點(diǎn)關(guān)注如果是NO表示線程沒起來"
        Slave_SQL_Running: Yes "重點(diǎn)關(guān)注 如果是NO表示該線程沒起來"
         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: 557
         Relay_Log_Space: 1139
         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 "該項(xiàng)表示同步時(shí)間 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

5,最后在master服務(wù)器上創(chuàng)建數(shù)據(jù)庫測試即可查看是否同步

級(jí)聯(lián)復(fù)制特點(diǎn)

  • 降低master服務(wù)器的壓力,網(wǎng)絡(luò)io壓力
  • 但是會(huì)產(chǎn)生數(shù)據(jù)不一致的問題

總結(jié)

  • 中繼slave需要打開二進(jìn)制日志,必須加上log_slave_updates配置項(xiàng)
  • 注意read_only=ON作用,限制從服務(wù)器為只讀."注意:此限制對(duì)擁有SUPER權(quán)限的用戶均無效"

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI