實現mysql級聯複製

來源:互聯網
上載者:User

標籤:mysql級聯複製

所謂級聯複製就是master伺服器,只給一台slave伺服器同步資料,然後slave伺服器在向後端的所有slave伺服器同步資料,降低master伺服器的寫壓力,和複製資料的網路IO。

一,配置master伺服器1,修改主設定檔
    vim /etc/my.cnf      在[mysql]配置塊下添加如下兩行配置       [mysql]       log_bin          #開啟二進位日誌功能       server_id=1      #為當前節點設定一個全域惟一的ID號 
2,重啟mysql服務,使配置生效
    systemctl restart mairadb
3,建立有複製許可權的使用者帳號
GRANT REPLICATION SLAVE  ON *.* TO ‘repluser‘@‘HOST‘ IDENTIFIED BY ‘replpass‘;     命令解析:        ‘repluser‘@‘HOST‘ :設定使用者名稱即主機ip或網段,網段用%表示 例如10.0.0.%        IDENTIFIED BY:設定密碼        *.* :表示所有資料庫,所有表        GRANT REPLCATION SLAVE:就是允許該使用者複製資料    該命令作用就是授權repluser能拷貝資料庫的所有內容
二,中繼slave伺服器配置1,修改主設定檔
    vim /etc/my.cnf   在[mysql]配置塊中添加如下兩行配置        [mysqld]           log_bin        server_id=2      #為當前節點設定一個全域惟一的ID號                 read_only=ON #限制從伺服器為唯讀."注意:此限制對擁有SUPER許可權的使用者均無效"        log_slave_updates #該項的作用是把master伺服器的二進位日誌計入到本機,然後再把二進位日誌複製給後端的其他slave伺服器
2,重啟mysql服務,使配置生效
    systemctl  restart mariadb
3,使用有複製許可權的使用者帳號串連至主伺服器,並啟動複製線程
     CHANGE MASTER TO      MASTER_HOST=‘host‘,        #指定master主機IP     MASTER_USER=‘repluser‘,    #指定master被授權的使用者名稱     MASTER_PASSWORD=‘replpass‘,#指定被授權的使用者密碼 MASTER_LOG_FILE=‘mysql-bin.xxxxx‘, #指定從master伺服器的那個二進位日誌開始複製     MASTER_LOG_POS=#;          #二進位日誌位置,可以在master伺服器上執行該命令查看,show master logs;     啟動複製線程IO_THREAD和SQL_THREAD     START SLAVE; 
4,查看中繼slave伺服器狀態
    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  "重點關注如果是NO表示線程沒起來"                Slave_SQL_Running: Yes "重點關注 如果是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 "該項表示同步時間 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     #為當前節點設定一個全域惟一的ID號            read_only=ON #限制從伺服器為唯讀."注意:此限制對擁有SUPER許可權的使用者均無效"
2,重啟mysql服務,使配置生效
    systemctl restart mariadb
3,使用有複製許可權的使用者帳號串連至主伺服器,並啟動複製線程
CHANGE MASTER TO      MASTER_HOST=‘中繼host‘,        #指定中繼slave主機IP     MASTER_USER=‘repluser‘,    #指定master被授權的使用者名稱     MASTER_PASSWORD=‘replpass‘,#指定被授權的使用者密碼 MASTER_LOG_FILE=‘mysql-bin.xxxxx‘, #指定從中繼slave伺服器的那個二進位日誌開始複製     MASTER_LOG_POS=#;          #二進位日誌位置,可以在slave伺服器上執行該命令查看,show master logs;     啟動複製線程IO_THREAD和SQL_THREAD     START SLAVE; 
4,查看slave伺服器狀態
    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  "重點關注如果是NO表示線程沒起來"                Slave_SQL_Running: Yes "重點關注 如果是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 "該項表示同步時間 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伺服器上建立資料庫測試即可查看是否同步級聯複製特點
  • 降低master伺服器的壓力,網路io壓力
  • 但是會產生資料不一致的問題
總結
  • 中繼slave需要開啟二進位日誌,必須加上log_slave_updates配置項
  • 注意read_only=ON作用,限制從伺服器為唯讀."注意:此限制對擁有SUPER許可權的使用者均無效"

實現mysql級聯複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.