CentOS上配置MariaDB主從複製配置教程

來源:互聯網
上載者:User

環境說明

主程式庫伺服器: 192.168.71.151,CentOS 7,MariaDB 10已安裝,無應用資料。
從程式庫伺服器1: 192.168.71.152,CentOS 7,MariaDB 10已安裝,無應用資料。
從程式庫伺服器2: 192.168.71.153,CentOS 7,MariaDB 10已安裝,無應用資料。
3個伺服器的MariaDB都正常運行。
MariaDB 10伺服器的常規安裝配置可參照《MariaDB/MySQL在 CentOS 6.6上的編譯安裝/二進位源碼包+授權詳解》。
各個伺服器的共同配置

以下操作在各個資料庫伺服器設定檔的[mysqld]部分下執行,資料庫設定檔路徑假定為 /etc/my.cnf 。/etc/my.cnf 中有關於主從配置的一些說明,見my.cnf中# Replication Master Server (default)和# Replication Slave (comment out master section to use this)部分。
開啟各個資料庫伺服器的設定檔 my.cnf
檢查確保各個伺服器的skip-networking這行是注釋掉的。主從複製需要資料庫伺服器使用IP監聽的方式,不然使用UNIX socket方式監聽,其他伺服器訪問不到。
把bind-address指定為各個伺服器網卡的綁定IP上。
即在設定檔的 #skip-networking行後面添加bind-address=192.168.71.x,在192.168.71.151上配置為bind-address=192.168.71.151,在192.168.71.152上配置為bind-address=192.168.71.152,在192.168.71.153上配置為bind-address=192.168.71.153。
配置server_id。server_id值為1到2的32次方-1的整數,每個伺服器都需要添加server_id配置,各個伺服器的server_id需要保證唯一性互不相同,實踐中通常設定為伺服器IP地址的最後一位,即分別設定為server_id=151,server_id=152,server_id=153。
上述配置完後調用service mysql reload重新載入設定檔。
配置 主伺服器

以下操作在主伺服器192.168.71.151的/etc/my.cnf上進行。
確保log-bin是啟用的,即log-bin=mysql-bin是非注釋狀態的,log-bin沒指定儲存目錄,則是預設datadir指向的目錄,可登入MariaDB shell通過如下命令查看:
MariaDB [(none)]> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| datadir       | /var/lib/mysql/ |
+---------------+-----------------+
建立帳號並賦予replication的許可權
從庫 從主庫複製資料時需要使用這個帳號進行
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'192.168.71.%' IDENTIFIED BY 'bigs3cret';
Query OK, 0 rows affected (0.00 sec)
查看主庫binary log的檔案位置
主庫鎖表操作,不讓資料庫進行寫入操作
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
記錄主庫log檔案及其當前位置
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      326 |              |                  |
+------------------+----------+--------------+------------------+
記住File和Position的部分,後面會用到
保持當前MariaDB shell終端處於開啟狀態,即保持主庫處於鎖定狀態,如果關閉MariaDB shell會導致主庫恢複非鎖定狀態
備份主庫已有資料並匯入從庫,如果主庫中有資料需要先備份並匯入到從庫中。使用新的終端視窗或終端模擬器Tab ssh登入192.168.71.151伺服器,執行如下語句進行Database Backup操作
mysqldump -uroot -p --all-databases > databases.sql
解鎖 主庫
資料備份完成後,就可以釋放主庫上的鎖:
MariaDB [(none)]> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
在 從伺服器 上的操作

以下操作需要在從庫192.168.71.152和192.168.71.153上都執行。
匯入備份的主庫資料
mysql -uroot -p < databases.sql
設定relay-log
my.cnf檔案中添加一行relay_log=relay-bin,如果不設定,預設是按主機名稱 + “-relay-bin”產生relay log。
設定主從複製
關於MySQL5.5以後和MariaDB不能在my.cnf檔案中配置主庫資訊的說明
傳送門----點我
其他的一些配置資訊可以參考
cat `whereis mariadb|awk '{print $2"/support-files/my-large.cnf"}'`
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.71.151',MASTER_PORT=3306,\
MASTER_USER='slave_user',MASTER_PASSWORD='bigs3cret',\
MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS= 326;
Query OK, 0 rows affected (0.24 sec)
這個命令完成以下幾個任務:
設定當前伺服器為192.168.71.151的從庫
提供當前資料庫(從庫)從主庫複製資料時所需的使用者名稱和密碼,即上面的
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'192.168.71.%' IDENTIFIED BY 'bigs3cret';設定的指定從庫開始複製主庫時需要使用的記錄檔和檔案位置,即上面主庫執行SHOW MASTER STATUS;顯示結果中的File和Position
開啟主從複製
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.71.151
                  Master_User: slave_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 326
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 306
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              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: 565
              Relay_Log_Space: 826
              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:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 151
               Master_SSL_Crl:
           Master_SSL_Crlpath:
                   Using_Gtid: No
                  Gtid_IO_Pos:
1 row in set (0.00 sec)
結果中Slave_IO_Running和Slave_SQL_Running必須為Yes,如果不是,需要根據提示的錯誤修改。
測試主從複製是否正常

在主庫192.168.71.151的MariaDB shell上建立表或修改資料,看是否從庫也跟著更新,如果跟著更新則說明正常。
例如,假定主庫上有資料庫 newdatabase,在主庫上執行
MariaDB [(none)]> use newdatabase;
Database changed
MariaDB [newdatabase]> create table test (id int unsigned auto_increment primary key);
Query OK, 0 rows affected (1.07 sec)
在每個從庫上執行
MariaDB [(none)]> use newdatabase;
Database changed
MariaDB [newdatabase]> show tables;
+-----------------------+
| Tables_in_newdatabase |
+-----------------------+
| test |
+-----------------------+
1 rows in set (0.00 sec)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.