Master-Slave Replication原理圖:mysql
主從複製的原理:linux
分爲同步複製和異步複製,實際複製架構中大部分爲異步複製。 複製的基本過程以下:web
1).Slave上面的IO進程鏈接上Master,並請求從指定日誌文件的指定位置(或者從最開始的日誌)以後的日誌內容;sql
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的哪一個位置開始日後的日誌內容,請發給我」;centos
4).Slave的Sql進程檢測到relay-log中新增長了內容後,會立刻解析relay-log的內容成爲在Master端真實執行時候的那些可執行的內容,並在自身執行。架構
爲何是MariaDB Replication
異步
As you may know, MariaDB is a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.tcp
MariaDB replication is 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 root@192.168.14.138:/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