1. 程式人生 > 實用技巧 >CentOS 7下的MariaDB Master-Slave Replication配置

CentOS 7下的MariaDB Master-Slave Replication配置

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

Master-Slave Replication原理圖:

223236_dTxj_1437015.jpeg

主從複製的原理:

分為同步複製和非同步複製,實際複製架構中大部分為非同步複製。 複製的基本過程如下:

1).Slave上面的IO程序連線上Master,並請求從指定日誌檔案的指定位置(或者從最開始的日誌)之後的日誌內容;

2).Master接收到來自Slave的IO程序的請求後,通過負責複製的IO程序根據請求資訊讀取制定日誌指定位置之後的日誌資訊,返回給Slave 的IO程序。返回資訊中除了日誌所包含的資訊之外,還包括本次返回的資訊已經到Master端的bin-log檔案的名稱以及bin-log的位置;

3).Slave的IO程序接收到資訊後,將接收到的日誌內容依次新增到Slave端的relay-log檔案的最末端,並將讀取到的Master端的 bin-log的檔名和位置記錄到master-info檔案中,以便在下一次讀取的時候能夠清楚的告訴Master“我需要從某個bin-log的哪個位置開始往後的日誌內容,請發給我”;

4).Slave的Sql程序檢測到relay-log中新增加了內容後,會馬上解析relay-log的內容成為在Master端真實執行時候的那些可執行的內容,並在自身執行。


為什麼是MariaDB Replication

As you may know,MariaDBis a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.

MariaDB replicationis a method to store multiple copies of data on many systems, and the data will automatically be copied from one database (Master) to another database (Slave). If one server goes down, the clients still can access the data from another (Slave) server database.

In this article, let us see how to configure MariaDB Master-Slave replication in CentOS 7. This method will work on all linux distributions, including

RHEL, CentOS, Ubuntu, and openSUSE etc.All you need to know is how to install MariaDB in the specific distribution you use.


環境:

  • MariaDB Master:CentOS 7 64bit Minimal Server

  • Master IP Address:192.168.15.134/24

  • MariaDB Slave:CentOS 7 64bit Minimal Server

  • Slave IP Address:192.168.15.138/24

  • 不同Linux版本,MariaDB安裝略有不同(Installing LAMP Stack in Linux


Master與Slave均操作

安裝MariaDB:

yum install mariadb-server mariadb

啟動MariaDB並開機啟動:

systemctl start mariadb

systemctl enable mariadb

設定MariaDB root密碼:

mysql_secure_installation


配置Master MariaDB:

firewall-cmd --permanent --add-port=3306/tcp #放行3306埠

firewall-cmd --reload

vi /etc/my.cnf #新增如下至[mysqld]部分

[mysqld]

server-id=1

log-basename=master

log-bin

binlog-format=row

binlog-do-db=games

#binlog-ignore-db=test

[...]

#binlog-format binlog三種格式:row/statement/mixed

#binlog-do-db 要同步的資料庫,同步多個,重複設定即可

#games 為要複製到Slave的資料庫名稱

systemctl restart mariadb #重啟MariaDB

mysql -u root -p #登入Master MariaDB

建立Slave使用者並設定密碼,獲取相應的Binlog及Pos資訊供後續使用:

MariaDB [(none)]> STOP SLAVE;

Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'sk'@'%' IDENTIFIED BY 'centos';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW MASTER STATUS;

+--------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+--------------------+----------+--------------+------------------+

| mariadb-bin.000001 | 460 | games | |

+--------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

MariaDB [(none)]> exit

Bye


備份Master MariaDB,關閉鎖表,並上傳備份masterdatabase.sql到Slave

mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql

mysql -u root -p #登入Master MariaDB

MariaDB [(none)]> UNLOCK TABLES;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit

Bye

拷貝masterdatabase.sql至Slave的/home目錄。

scp masterdatabase.sql [email protected]:/home


配置Slave MariaDB:

firewall-cmd --permanent --add-port=3306/tcp #放行3306埠

firewall-cmd --reload

vi /etc/my.cnf #新增如下至[mysqld]部分

[mysqld]

server-id = 2

replicate-do-db=games

#replicate-ignore-db=games

[...]

匯入Master資料:

mysql -u root -p < /home/masterdatabase.sql

systemctl restart mariadb #重啟MariaDB

mysql -u root -p #登入Slave MariaDB

MariaDB [(none)]> STOP SLAVE;

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.15.134', MASTER_USER='sk', MASTER_PASSWORD='centos', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=460;

Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> SLAVE START;

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show slave status\G;

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

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.15.134

Master_User: sk

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mariadb-bin.000001

Read_Master_Log_Pos: 460

Relay_Log_File: mariadb-relay-bin.000002

Relay_Log_Pos: 531

Relay_Master_Log_File: mariadb-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: games

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: 460

Relay_Log_Space: 827

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: 1

1 row in set (0.00 sec)

注:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

保證以上兩個引數為Yes,否則檢查master/slave配置步驟


測試MariaDB複製:

mysql -u root -p

MariaDB [(none)]> create database games;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use games;

Database changed

MariaDB [games]> create table sample(c int);

Query OK, 0 rows affected (0.01 sec)

MariaDB [games]> insert into sample (c) values (1);

Query OK, 1 row affected (0.01 sec)

MariaDB [games]> select * from sample;

+------+

| c |

+------+

| 1 |

+------+

1 row in set (0.00 sec)

MariaDB [games]>


Slave MariaDB端檢測

mysql -u root -p

Then, run the following commands to verify whether the entries have been replicated correctly.

MariaDB [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| games |

| mysql |

| performance_schema |

+--------------------+

MariaDB [(none)]> use games;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [games]> select * from sample;

+------+

| c |

+------+

| 1 |

+------+

1 row in set (0.00 sec)

MariaDB [games]>


類似article

Setup Master-Slave Replication in MySQL Server

轉載於:https://my.oschina.net/HeAlvin/blog/663471