1. 程式人生 > >Mysql——5、MySQL主從複製

Mysql——5、MySQL主從複製

一、MySQL複製

1.1 系統擴充套件的方式:
scale up:向上擴充套件,垂直擴充套件
scale out:向外擴充套件,水平擴充套件
1.2 MySQL的擴充套件:複製:每個節點都有相同的資料集;向外擴充套件;二進位制日誌;單向;
1.3 複製的功用:
負載均衡讀;資料分佈;備份;高可用和故障切換;MySQL升級測試
1.4 主從複製:
(1)從節點:
(中繼日誌)
I/O Thread:從Master請求二進位制日誌時間,並保存於中繼日誌中;
SQL Thread:從中繼日誌中讀取日誌事件,在本地完成重放;
(2)主節點:
(二進位制日誌)
dump Thread:為每個Slave的I/O Thread啟動一個dump執行緒,用於向其傳送binary log events;
1.5 主從複製特點:


(1)非同步複製
(2)主從資料不一致比較常見
1.6 複製架構:M/S, M/M, 環狀複製
一主多從:(從伺服器可以再有從伺服器)
一從多主:
1、主節點是單點
2、寫操作無法有效均衡
1.7 二進位制日誌事件記錄格式:
語句:STATEMENT
行:ROW 行儲存量最大
混合:MIXED 使用比較多

**1.8 複製型別:**主從、主主、半同步複製、複製過濾器

二、主從複製

2.1 主從配置過程:
主節點:
(1)啟動二進位制日誌;

[mysqld]
log_bin=mysql-bin

(2)為當前節點設定一個全域性唯一的ID號;

[mysqld]
server_id=1

(3)建立有複製許可權的使用者賬號:

 REPLICATION SLAVE, REPLICATION CLIENT
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.%.%' IDENTIFIED BY 'replpass';

從節點:
(1)啟動中繼日誌(並閉二進位制日誌建議);

[mysqld]
relay_log=relay-log
relay_log_index=relay-log.index

(2)為當前節點設定一個全域性唯一的ID號;

[mysqld]
server-id=7

(3)使用有複製許可權的使用者賬號連線至主伺服器,並啟動複製執行緒;

mysql > CHANGE MASTER TO MASTER_HOST='172.16.92.32',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_log_file='master-bin.000003',MASTER_LOG_POS=245;
mysql > START SLAVE [IO_THREAD|SQL_THREAD];

複製三個步驟:
(1)在主庫上啟用二進位制日誌;
(2)備庫從主庫複製二進位制日誌,並儲存至本地的中繼日誌中;
(3)備庫從中繼日誌中讀取事件並於本地執行一次;
2.2 思考:如果主節點已經運行了一段時間,且有大量資料時,如何配置並啟動slave節點?

  • 通過備份恢復資料至從伺服器;
  • 複製起始位置為備份時,二進位制日誌檔案及其POS

2.3 主從複製示例:

主節點172.16.92.32:

vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log_bin=master-bin
server-id=1
innodb_file_per_table=ON
skip_name_resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

ls /etc/my.cnf.d/
client.cnf  mysql-clients.cnf  server.cnf
systemctl start mariadb.service
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%log%';
MariaDB [(none)]> SHOW MASTER LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |     30379 |
| master-bin.000002 |   1038814 |
| master-bin.000003 |       245 |
+-------------------+-----------+
MariaDB [(none)]>  SHOW GLOBAL VARIABLES LIKE '%SERVER%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| character_set_server | latin1            |
| collation_server     | latin1_swedish_ci |
| server_id            | 1                 |
+----------------------+-------------------+
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.%.%' IDENTIFIED BY 'replpass';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%master%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| master_verify_checksum | OFF   |
| sync_master_info       | 0     |
+------------------------+-------+

從節點172.16.92.33:

vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
relay-log=relay-log
relay-log-index=relay-log.index
server-id=7
innodb_file_per_table=ON
skip_name_resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

systemctl start mariadb.service
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%log%';
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%SERVER%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| character_set_server | latin1            |
| collation_server     | latin1_swedish_ci |
| server_id            | 7                 |
+----------------------+-------------------+
MariaDB [(none)]> HELP CHANGE MASTER TO
Name: 'CHANGE MASTER TO'
Description:
Syntax:
	CHANGE MASTER TO option [, option] ...

option:
			  | MASTER_BIND = 'interface_name'
			  | MASTER_HOST = 'host_name'    # 指明要連線的主節點,值型別字串
			  | MASTER_USER = 'user_name'    # 具有複製許可權的賬號,值型別為字串 
			  | MASTER_PASSWORD = 'password' # 上述使用者的密碼,值型別為字串 
			  | MASTER_PORT = port_num
			  | MASTER_CONNECT_RETRY = interval
			  | MASTER_HEARTBEAT_PERIOD = interval
			  | MASTER_LOG_FILE = 'master_log_name'  # 複製起點,主節點上二進位制日誌,值型別為字串
			  | MASTER_LOG_POS = master_log_pos      # 複製起點,主節點上二進位制日誌中起始事件的位置,值型別為數值
			  | RELAY_LOG_FILE = 'relay_log_name'
			  | RELAY_LOG_POS = relay_log_pos
			  | MASTER_SSL = {0|1}
			  | MASTER_SSL_CA = 'ca_file_name'
			  | MASTER_SSL_CAPATH = 'ca_directory_name'
			  | MASTER_SSL_CERT = 'cert_file_name'
			  | MASTER_SSL_KEY = 'key_file_name'
			  | MASTER_SSL_CIPHER = 'cipher_list'
			  | MASTER_SSL_VERIFY_SERVER_CERT = {0|1}
			  | IGNORE_SERVER_IDS = (server_id_list)
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.92.32',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_log_file='master-bin.000003',MASTER_LOG_POS=245;
MariaDB [(none)]> SHOW SLAVE STATUS\G
MariaDB [(none)]> HELP START SLAVE
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G

測試:
在主節點:

MariaDB [(none)]> CREATE DATABASE mydb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      579 |              |                  |
+-------------------+----------+--------------+------------------+

在從節點可以檢視到資料同步過來了:

MariaDB [(none)]> SHOW SLAVE STATUS\G     #可以看到Exec_Master_Log_Pos: 579
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%relay_log%';
+----------------------------------+----------------+
| Variable_name                    | Value          |
+----------------------------------+----------------+
| innodb_recovery_update_relay_log | OFF            |
| max_relay_log_size               | 0              |
| relay_log                        | relay-log      |
| relay_log_index                  |                |
| relay_log_info_file              | relay-log.info |
| relay_log_purge                  | ON             |
| relay_log_recovery               | OFF            |
| relay_log_space_limit            | 0              |
| sync_relay_log                   | 0              |
| sync_relay_log_info              | 0              |
+----------------------------------+----------------+
[[email protected] ~]# cd /var/lib/mysql/
[[email protected] mysql]# file /var/lib/mysql/master.info
/var/lib/mysql/master.info: ASCII text
[[email protected] mysql]# cat /var/lib/mysql/master.info
18
master-bin.000003
579
172.16.92.32
repluser
replpass
3306
60
0





0
1800.000

0
[[email protected] mysql]# file relay-log.info
relay-log.info: ASCII text
[[email protected] mysql]# cat relay-log.info 
./relay-log.000002
864
master-bin.000003
579

2.4 複製架構中應該注意的問題:
(1)限制從伺服器為只讀:
在從伺服器上設定 read_only=ON;此限制對擁有SUPER許可權的使用者均無效;
阻止所有使用者的方法:mysql> FLUSH TABLES WITH READ LOCK;(寫操作都會被阻塞)
(2)如何保證主從複製的事務安全?
前提:mysql對二進位制日誌事件資料會有緩衝;
在master節點啟用引數:sync_binlog=ON
如果用到的為InnoDB儲存引擎:
innodb_flush_logs_at_trx_commit=ON
innodb_support_xa=ON
在slave節點:skip_slave_start=ON

master節點: sync_master_log
slave節點: sync_relay_log sync_relay_log_info
(3)雙版本一致,從節點版本低於主節點可能會引起錯誤
(4)半同步複製?

半同步:master要等待一個從節點把資料完整複製過去;

三、主主複製

(1) 雙方節點都得建立具有複製許可權使用者;
(2) 雙節點都得啟用中繼日誌和二進位制日誌;
(3) 為保證具有自動增長功能的欄位能正確生成ID,需要配置兩個節點分別使用偶數或奇數ID號;
(4) 都要把對方配置為自己的主節點;
3.1 互為主從:
(1)資料不一致:因此慎用;
(2)自動增長id:
配置一個節點使用奇數id
auto_increment_offset=1
auto_increment_increment=2
另一個節點使用偶數id
auto_increment_offset=2
auto_increment_increment=2
3.2 配置步驟:
(1)各節點使用一個唯一server_id;
(2)都啟動binary_log和relay log;
(3)建立擁有複製許可權的使用者賬號;
(4)定義自動增長id欄位的資料範圍為奇偶;
(5)均把對方指定為主節點,並啟動複製執行緒;

3.3 主主複製示例

(1)各節點使用一個唯一server_id;
(2)都啟動binary_log和relay log;
主節點1 172.16.92.32

vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log_bin=master-bin
relay_log=relay-log
server-id=1
innodb_file_per_table=ON
skip_name_resolve=ON
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

[[email protected] ~]# systemctl start mariadb   #確定都啟動binary_log和relay log
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%log%';
| log_bin                                   | ON
| relay_log                                 | relay-log  

主節點2 172.16.92.33

vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log_bin=master-bin
relay_log=relay-log
server-id=5
innodb_file_per_table=ON
skip_name_resolve=ON

auto_increment_offset=2
auto_increment_increment=2

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

[[email protected] ~]# systemctl start mariadb   #確定都啟動binary_log和relay log
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%log%';
| log_bin                                   | ON
| relay_log                                 | relay-log  

(3)建立擁有複製許可權的使用者賬號;
2個主節點都操作:

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.%.%' IDENTIFIED BY 'replpass';
#生產環境注意不要寫'172.16.%.%'的方式,應指定詳細IP
MariaDB [(none)]> FLUSH PRIVILEGES;

主節點2 172.16.92.33

MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      506 |              |                  |
+-------------------+----------+--------------+------------------+
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.92.32',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=506;
MariaDB [(none)]> SHOW SLAVE STATUS\G
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: 172.16.92.32
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 506
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000003
             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: 506
              Relay_Log_Space: 818
              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 172.16.92.32

MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      506 |              |                  |
+-------------------+----------+--------------+------------------+

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.92.33',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=506;
MariaDB [(none)]> SHOW SLAVE STATUS\G
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.92.33
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 506
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000003
             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: 506
              Relay_Log_Space: 818
              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: 5

測試:
主節點1 172.16.92.32建立資料庫mydb:

MariaDB [(none)]> CREATE DATABASE mydb;
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+

檢視主節點2 172.16.92.33資料庫也同步了,使用資料庫建立表測試:

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
MariaDB [(none)]> use mydb;
Database changed
MariaDB [mydb]> CREATE TABLE tb1 (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(30));
MariaDB [mydb]> DESC tb1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(30)         | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
MariaDB [mydb]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      659 |              |                  |
+-------------------+----------+--------------+------------------+

再檢視主節點1 172.16.92.32已同步資料, 往表裡面寫入資料:

MariaDB [mydb]> use mydb;
Database changed
MariaDB [mydb]> SHOW TABLES;
+----------------+
| Tables_in_mydb |
+----------------+
| tb1            |
+----------------+
MariaDB [mydb]> DESC tb1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(30)         | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES ('Yang Kang'),('Yang Guo'),('Xiao Ming');
MariaDB [mydb]> SELECT * FROM tb1;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | Yang Kang |
|  3 | Yang Guo  |
|  5 | Xiao Ming |
+----+-----------+

主節點2 172.16.92.33資料已同步,再插入資料:

MariaDB [mydb]> SELECT * FROM tb1;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | Yang Kang |
|  3 | Yang Guo  |
|  5 | Xiao Ming |
+----+-----------+
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES ('Chen Yi'),('Chen Er'),('Chen San');
MariaDB [mydb]> SELECT * FROM tb1;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | Yang Kang |
|  3 | Yang Guo  |
|  5 | Xiao Ming |
|  6 | Chen Yi   |
|  8 | Chen Er   |
| 10 | Chen San  |
+----+-----------+

四、半同步複製

測試環境重置:
systemctl stop mariadb
rm -rf /var/lib/mysql/*
主節點 172.16.92.32:

vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log_bin=master-bin
server-id=1
innodb_file_per_table=ON
skip_name_resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

從節點 172.16.92.33:

vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
relay_log=relay-log
server-id=5
innodb_file_per_table=ON
skip_name_resolve=ON

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

主節點 172.16.92.32 建立擁有複製許可權的使用者賬號:

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.%.%' IDENTIFIED BY 'replpass';
#生產環境注意不要寫'172.16.%.%'的方式,應指定詳細IP
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      496 |              |                  |
+-------------------+----------+--------------+------------------+

從節點 172.16.92.33:

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.92.32',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=496;
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.92.32
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 496
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000003
             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: 496
              Relay_Log_Space: 818
              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
MariaDB [(none)]> STOP SLAVE;

安裝外掛:
主節點 172.16.92.32:

MariaDB [(none)]> HELP INSTALL
Name: 'INSTALL PLUGIN'
Description:
Syntax:
INSTALL PLUGIN plugin_name SONAME 'shared_library_name'
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%semi%';
+------------------------------------+-------+
| Variable_name                      | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled       | OFF   |
| rpl_semi_sync_master_timeout       | 10000 |
| rpl_semi_sync_master_trace_level   | 32    |
| rpl_semi_sync_master_wait_no_slave | ON    |
+------------------------------------+-------+
MariaDB [(none)]>  SET GLOBAL rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GLOBAL STATUS LIKE '%semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 0     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
| Rpl_semi_sync_master_tx_wait_time          | 0     |
| Rpl_semi_sync_master_tx_waits              | 0     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 0     |
+--------------------------------------------+-------+

從節點 172.16.92.33:

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'rpl_semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | OFF   |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
MariaDB [(none)]>  SET GLOBAL rpl_semi_sync_slave_enabled = 1;

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'rpl_semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
MariaDB [(none)]> START SLAVE;

在主節點172.16.92.32驗證是否已經工作於半同步模式:

MariaDB [(none)]> CREATE DATABASE mydb;
MariaDB [(none)]> USE mydb;
Database changed
MariaDB [mydb]> SHOW GLOBAL STATUS LIKE '%semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 546   |
| Rpl_semi_sync_master_net_wait_time         | 546   |
| Rpl_semi_sync_master_net_waits             | 1     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 671   |
| Rpl_semi_sync_master_tx_wait_time          | 671   |
| Rpl_semi_sync_master_tx_waits              | 1     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 1     |
+--------------------------------------------+-------+

master:

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql> SET GLOBAL rpl_semi_sync_master_enabled=1;
mysql> SHOW GLOBAL VARIABLES LIKE '%semi%';
mysql> SHOW GLOBAL STATUS LIKE '%semi%';

slave:

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;

主節點在聯絡從節點超時後,會自動降紙為非同步模式;

五、複製過濾器

讓從節點僅複製指定的資料庫,或指定資料庫的指定表;
有兩種實現方式:
(1)在主節點上過濾
主伺服器僅向二進位制日誌記錄與指定資料庫(特定表)相關的事件;
問題:時間還原無法實現,不建議使用:
binlog_do_db = # 資料庫白名單列表
binlog_ignore_db = # 資料庫黑名單列表

(2)在從節點上過濾
從伺服器SQL_THREAD在replay中繼日誌中的事件時,僅讀取與特定的資料庫(特定表)相關的事件並應用於本地;
問題:會造成網路及磁碟IO浪費;

replicate_do_db =  #資料庫複製白名單
replicate_ignore_db =  #資料庫複製黑名單
replicate_db_table = DB_NAME.TB_NAME #複製表的白名單
replicate_ignore_table =  #複製表的黑名單
replicate_wild_do_table = #支援萬用字元的複製表的白名單
replicate_wild_ignore_table = 

在從節點上測試,調整隻複製mydb資料庫的資料:

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'replicate%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| replicate_annotate_row_events    | OFF       |
| replicate_do_db                  |           |
| replicate_do_table               |           |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db              |           |
| replicate_ignore_table           |           |
| replicate_wild_do_table          |           |
| replicate_wild_ignore_table      |           |
+----------------------------------+-----------+
MariaDB [(none)]> STOP SLAVE;
MariaDB [(none)]> SET GLOBAL replicate_do_db=mydb;
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'replicate%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| replicate_annotate_row_events    | OFF       |
| replicate_do_db                  | mydb      |
| replicate_do_table               |           |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db              |           |
| replicate_ignore_table           |           |
| replicate_wild_do_table          |           |
| replicate_wild_ignore_table      |           |
+----------------------------------+-----------+
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.92.32
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 579
               Relay_Log_File: relay-log.000004
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: mydb   #說明只複製mydb的資料
          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: 579
              Relay_Log_Space: 1186
              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

#此時在主節點上面新建一個其他的資料庫,發現並不會同步到從節點上。
#在主節點的mydb資料庫上面插入資料,發現可以同步到從節點。

六、基於SSL複製

6.1 前提:支援SSL
(1)master配置證書和私鑰:並且建立一個要求必須使用SSL連線的複製賬號;
(2)slave端使用CHANGE MASTER TO命令時指明ssl相關選項;
6.2 跟複製功能相關的檔案:
master.info : 用於儲存slave連線至master時的相關資訊,例如賬號、密碼、伺服器地址等等;
relay-log.info : 儲存在當前slave節點上已經複製的當前二進位制日誌和本地replay log日誌的對應關係;
6.3 複製的監控和維護:
(1)清理日誌:
PURGE
(2)複製監控:
SHOW MASTER STATUS
SHOW BINLOG EVENTS
SHOW BINARY LOGS

SHOW SLAVE STATUS
SHOW PROCESSLIST;
(3)從伺服器是否落後於主服務:
Seconds_Behind_Master: 0
(4)如何確定主從節點資料是否一致:
percona-tools
(5)資料不一致如何修復?
重新複製