MariaDB數據庫介紹之二、複製

上篇文章介紹了MariaDB數據庫的備份工具及實現的幾種方法,這次介紹MariaDB數據庫的複製,首選介紹什麼是mysql的複製?

一、複製概述

1.1、複製可以解決什麼問題?

數據複製技術有以下一些特點:
(1)    數據分佈
(2)    負載平衡(load balancing),只針對讀操作
(3)    數據庫的備份
(4)    高可用性(high availability)和容錯


1.2、複製如何進行

wKiom1Y9rNSglpGEAADs65FhHw4278.jpg

(1)    master將數據庫的改變記錄到二進制日誌(binary log)中(這些記錄叫做二進制日誌事件);

(2)    slave通過I/O線程將master的binary log events拷貝到它的中繼日誌(relay log);

(3)    slave再通過SQL線程replay中繼日誌中的事件,將改變保存至自己的數據文件中。


二、通過以下幾個模型進行介紹:

2.1、主從 

2.2、主主 

2.3、半同步複製

2.4、基於ssl的複製


測試環境: centos 7自帶的mariadb-server;

            master:172.16.116.231 ;slave:172.16.116.232


2.1、主從複製 

①主從配置過程:

master:

  (1) 啓動二進制日誌;

      [mysqld]

 log_bin=mysql-bin

  2) 爲當前節點設置一個全局唯一的server_id號;

      [mysqld]

 server_id=1

  (3) 創建有複製權限的用戶賬號;

 REPLICATION SLAVE, REPLICATION CLIENT

從節點:

  (1) 啓動中繼日誌;

       [mysqld]

   relay_log=relay-log

  (2) 爲當前節點設置一個全局惟的ID號;

      [mysqld]

   server_id=11

 (3) 使用有複製權限的用戶賬號連接至主服務器,並啓動複製線程;

      mysql> CHANGE MASTER TO ~~ 

      mysql> START SLAVE [IO_THREAD|SQL_THREAD];

②實現過程如下:

master:172.16.11.231 

# yum install -y mariadb-server   //master/slave節點安裝MySQL數據庫
[root@node1 ~]# vim /etc/my.cnf
log_bin=mysql-bin                  //開啓二進制
server_id=1
innodb_file_per_table=1
skip_name_resolve=1
ssl

MariaDB [(none)]> grant replication slave,replication client on *.* to 'copy'@'172.16.116.232' identified by 'passwd';              //用戶授權
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;    //查看使用二進制日誌文件的位置
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      494 |              |                  | 
+------------------+----------+--------------+------------------+ 

MariaDB [(none)]> create database testdb;     //測試從服務器是否可以複製
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show master status;         
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      581 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

slave:172.16.11.232

# yum install -y mariadb-server   //master/slave節點安裝MySQL數據庫
[root@node2 ~]# vim /etc/my.cnf
relay_log=relay-log              //開啓中繼日誌
server_id=11                     //id號
innodb_file_per_table=1          //innodb存儲引擎每表一個表空間
skip_name_resolve=1              //跳過名稱反解
ssl                              //啓用ssl

MariaDB [(none)]> change master to master_host='172.16.116.231',master_user='copy',master_password='passwd',master_log_file='mysql-bin.000003',master_log_pos=494;
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> start slave;        //啓動複製線程
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.16.116.231
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 494
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-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: 494
              Relay_Log_Space: 245
              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: NULL
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: 0
1 row in set (0.00 sec)


MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.116.231
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 581                    
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 616
        Relay_Master_Log_File: mysql-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: 581
              Relay_Log_Space: 904
              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)

MariaDB [(none)]> show databases;         //複製沒問題
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.01 sec)


2.2、主主複製;互爲主從

① 配置過程

         (1)各節點設置一個全局唯一的server_id號

         (2)各節點都啓用log_bin 和relay_log

         (3)創建有複製權限的用戶賬號

                        REPLICATION SLAVE ; REPLICATION CLIENT 

         (4)定義自動增長id字段的數值範圍爲奇偶      

                 配置一個節點使用奇數id

auto_increment_offset=1

auto_increment_increment=2

 另一個節點使用偶數id

auto_increment_offset=2

auto_increment_increment=2

         (5)均把對方指定爲主節點,並啓動複製線程 

② 實現過程如下

master:172.16.116.231

[root@node1 ~]# vim /etc/my.cnf
log_bin=mysql-bin                //開啓二進制日誌
relay_log=relay-log              //開啓中繼日誌
server_id=1                     //id號 
innodb_file_per_table=1          //innodb存儲引擎每表一個表空間
skip_name_resolve=1              //跳過名稱反解
ssl
auto_increment_offset=1         //自動增長id段使用奇數
auto_increment_increment=2

MariaDB [(none)]> grant replication slave,replication client on *.* to 'copy'@'172.16.116.232' identified by 'passwd';     
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      504 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>  change master to master_host='172.16.116.232',master_user='copy',master_password='passwd',master_log_file='mysql-bin.000003',master_log_pos=504;
Query OK, 0 rows affected (0.38 sec)

MariaDB [(none)]> start slave;             //啓動複製線程
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> create database testdb;     //測試,創建新的數據庫
Query OK, 1 row affected (0.00 sec)

//在172.16.116.231的MySQL數據庫中查看是否存在表及其表中數據
MariaDB [(none)]> select * from testdb.student;
+----+-------+
| id | name  |
+----+-------+
|  2 | tom   |
|  4 | jerry |
+----+-------+
2 rows in set (0.00 sec)
//存在,同樣新增2條數據
MariaDB [(none)]> insert into testdb.student(name) value ('blue'),('red');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

slave:172.16.116.232  

[root@node2 ~]# vim /etc/my.cnf
log_bin=mysql-bin
relay_log=relay-log
server_id=11
innodb_file_per_table=1
skip_name_resolve=1
ssl

auto_increment_offset=2
auto_increment_increment=2

MariaDB [(none)]> grant replication slave,replication client on *.* to 'copy'@'172.16.116.231' identified by 'passwd';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      504 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>  change master to master_host='172.16.116.231',master_user='copy',master_password='passwd',master_log_file='mysql-bin.000003',master_log_pos=504;
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]> start slave;            //啓動複製線程
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show databases;    //查看是否同步成功,可以看到有testdb數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)


//在172.16.116.232的MySQL數據庫中創建表student並插入2條數據
MariaDB [(none)]> use testdb;
Database changed
MariaDB [testdb]> create table student(id int unsigned not null auto_increment primary key,name char(20) not null);
Query OK, 0 rows affected (0.37 sec)
MariaDB [testdb]> insert into student(name) value ('tom'),('jerry');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

//可以看到下表中的數據,同步沒問題!!
MariaDB [testdb]> select * from testdb.student;
+----+-------+
| id | name  |
+----+-------+
|  2 | tom   |
|  4 | jerry |
|  5 | blue  |
|  7 | red   |
+----+-------+
4 rows in set (0.00 sec)

       

2.3、半同步複製

配置過程:

       實現半同步複製的功能很簡單,只需在mysql的主服務器和從服務器上安裝個google提供的插件即可實現,master上使用semisync_master.so,slave上使用semisync_slave.so插件即可實現,插件在mysql通用二進制的mysql/lib/plugin目錄內;rpm包安裝的 /usr/lib64/mysql/plugin/目錄內。

      注:在從服務器開啓半同步的時候,建立數據庫很快;關閉從服務器的I/O線程的時候,主服務器創建數據庫的時候會等待,當超過超時時間的時候纔會創建;一旦超時,會自動降級爲異步,再次執行操作不會等待

// 主服務器上需要安裝的插件 
MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.00 sec)

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)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show global variables like '%semi%';


②其配置步驟如下;主從複製模型測試~~

master:

[root@node1 ~]# vim /etc/my.cnf
log_bin=mysql-bin                //開啓二進制日誌
server_id=1
innodb_file_per_table=1
skip_name_resolve=1
rpl_semi_sync_master_enabled=1      //開啓半同步複製; 配置完成重啓mariadb服務
rpl_semi_sync_master_timeout=5000   
                 //設置複製的超時時間,單位爲毫秒,超過這個時長,會自動降級爲異步複製 
                 
MariaDB [(none)]> show global variables like '%semi%';
+------------------------------------+-------+
| Variable_name                      | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled       | ON    |
| rpl_semi_sync_master_timeout       | 5000  |
| rpl_semi_sync_master_trace_level   | 32    |
| rpl_semi_sync_master_wait_no_slave | ON    |
+------------------------------------+-------+
4 rows in set (0.00 sec)  

MariaDB [(none)]> grant replication slave,replication client on *.* to 'copy'@'172.16.116.232' identified by 'passwd';             //授權
Query OK, 0 rows affected (5.01 sec)

MariaDB [(none)]> show master status;        //查看二進制日誌的事件位置 
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      419 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

wKioL1Y9yCjS2ZgmAAJjYYBmqL8897.jpg

slave:

[root@node2 ~]# vim /etc/my.cnf
relay_log=relay-log
server_id=11
innodb_file_per_table=1
skip_name_resolve=1
rpl_semi_sync_slave_enabled=1       //開啓半同步複製 ; 配置完成重啓mariadb服務


MariaDB [(none)]> show global variables like '%semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
2 rows in set (0.00 sec)


MariaDB [(none)]> change master to master_host='172.16.116.231',master_user='copy',master_password='passwd',master_log_file='mysql-bin.000004',master_log_pos=419; 
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> start slave;//啓動複製線程,看到上圖顯示Rpl_semi_sync_master_clients=1  
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.116.231
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 419
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000004
             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: 419
              Relay_Log_Space: 817
              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)

測試過程如下~~~

master上創建新的數據庫;創建test1的時候很快的速度就完成了;在創建test2之前,關閉slave的io_thread,可以看到創建test2會有延遲,到達設定的值5m的時候(rpl_semi_sync_master_timeout=5000)纔會創建完成;但是再創建test3的時候會很快完成,說明確實降級爲異步複製了
MariaDB [(none)]> create database test1;
Query OK, 1 row affected (0.00 sec)
                                        //在slave上執行 stop slave io_thread;
MariaDB [(none)]> create database test2;           
Query OK, 1 row affected (5.00 sec)       //看到延遲爲5m

MariaDB [(none)]> create database test3;
Query OK, 1 row affected (0.00 sec)


MariaDB [(none)]> show global status like '%semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |     //客戶端爲0
| Rpl_semi_sync_master_net_avg_wait_time     | 17088 |
| Rpl_semi_sync_master_net_wait_time         | 85444 |
| Rpl_semi_sync_master_net_waits             | 5     |
| Rpl_semi_sync_master_no_times              | 3     |
| Rpl_semi_sync_master_no_tx                 | 4     |
| Rpl_semi_sync_master_status                | OFF   |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 697   |
| Rpl_semi_sync_master_tx_wait_time          | 1394  |
| Rpl_semi_sync_master_tx_waits              | 2     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 2     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)


2.4、基於ssl實現主從複製

 mariadb數據複製是明文的,爲了加強數據內容安全性,基於ssl實現對複製內容的加密。

實現過程:

2.4.1、創建CA,並自簽證書

#  echo 01 > /etc/pki/CA/serial
#  touch /etc/pki/CA/index.txt
# (umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:mariadb
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:CA    
Email Address []:[email protected] 

//爲主服務器(172.16.116.231)頒發證書
#openssl ca -in master.csr -out master.crt -days 365
//爲從服務器(172.16.116.232)頒發證書;
#openssl ca -in slave.csr -out slave.crt -days 365


2.4.2、主服務器(172.16.116.231)申請證書;

# hostname master
[root@master ~]# mkdir /etc/mysql/ssl
[root@master ~]# (umask 077;openssl genrsa -out /etc/mysql/ssl/master.key 2048)
Generating RSA private key, 2048 bit long modulus
.....................................................................+++
...................+++
e is 65537 (0x10001)
[root@master ~]# openssl req -new -key /etc/mysql/ssl/master.key -out /etc/mysql/ssl/master.csr
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:mariadb
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:master
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:


2.4.3、爲從服務器(172.16.116.232)頒發證書;

# hostname slave
[root@slave ~]# mkdir /etc/mysql/ssl
[root@slave ~]# (umask 077;openssl genrsa -out /etc/mysql/ssl/slave.key 2048)
Generating RSA private key, 2048 bit long modulus
.+++
...+++
e is 65537 (0x10001)
[root@slave ~]# openssl req -new -key /etc/mysql/ssl/slave.key -out /etc/mysql/ssl/slave.csr

Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Beijing 
Organization Name (eg, company) [Default Company Ltd]:mariadb
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:slave
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:


2.4.4、修改CA證書權限及mysql的配置文件;將CA證書的公鑰cacert.pem複製到主從服務器中

master

[root@master ~]# cp cacert.pem /etc/mysql/ssl/
[root@master ~]# cd /etc/mysql/ssl/
[root@master ssl]# chown -R mysql.mysql ./*
[root@master ssl]# chmod 600 ./* 

[root@master ~]# vim /etc/my.cnf
log_bin=mysql-log
server_id=1
innodb_file_per_table=1
skip_name_resolve=1
ssl
ssl_ca=/etc/mysql/ssl/cacert.pem
ssl_cert=/etc/mysql/ssl/master.crt
ssl_key=/etc/mysql/ssl/master.key

slave 

[root@slave ~]# cp cacert.pem /etc/mysql/ssl/
[root@slave ~]# cd /etc/mysql/ssl/
[root@slave ssl]# chown -R mysql.mysql ./*
[root@slave ssl]# chmod 600 ./*
[root@slave ssl]# ll
total 16
-rw------- 1 mysql mysql 1367 Nov  7 20:19 cacert.pem
-rw------- 1 mysql mysql 4544 Nov  7 20:17 slave.crt
-rw------- 1 mysql mysql 1675 Nov  7 20:14 slave.key

[root@slave ssl]# vim /etc/my.cnf
relay_log=relay-log
server_id=11
innodb_file_per_table=1
skip_name_resolve=1
ssl
ssl_ca=/etc/mysql/ssl/cacert.pem
ssl_cert=/etc/mysql/ssl/slave.crt
ssl_key=/etc/mysql/ssl/slave.key


2.4.5、啓動mysql服務;授權用戶;測試複製是否加密

master

MariaDB [(none)]> show global variables like '%ssl%';
+---------------+------------------------------+
| Variable_name | Value                        |
+---------------+------------------------------+
| have_openssl  | YES                          |
| have_ssl      | YES                          |
| ssl_ca        | /etc/mysql/ssl/cacert.pem |
| ssl_capath    |                              |
| ssl_cert      | /etc/mysql/ssl/master.crt |
| ssl_cipher    |                              |
| ssl_key       | /etc/mysql/ssl/master.key |
+---------------+------------------------------+
7 rows in set (0.00 sec)

MariaDB [(none)]> grant replication slave,replication client on *.* to 'copy'@'172.16.116.232' identified by 'passwd' require ssl;     //授權
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-log.000003 |      506 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


測試~~~
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-log.000003 |      779 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


slave

MariaDB [(none)]> show  global variables like '%ssl%';
+---------------+---------------------------+
| Variable_name | Value                     |
+---------------+---------------------------+
| have_openssl  | YES                       |
| have_ssl      | YES                       |
| ssl_ca        | /etc/mysql/ssl/cacert.pem |
| ssl_capath    |                           |
| ssl_cert      | /etc/mysql/ssl/slave.crt  |
| ssl_cipher    |                           |
| ssl_key       | /etc/mysql/ssl/slave.key  |
+---------------+---------------------------+
7 rows in set (0.00 sec)

MariaDB [(none)]> change master to master_host='172.16.116.231',master_user='copy',master_password='sql-log.000003',master_log_pos=506,master_ssl=1,master_ssl_ca='/etc/mysql/ssl/cacert.pem',master_ssl.crt',master_ssl_key='/etc/mysql/ssl/slave.key'; 
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> change master to master_host='172.16.116.231',master_user='copy',master_password='sql-log.000003',master_log_pos=506,master_ssl=1,master_ssl_ca='/etc/mysql/ssl/cacert.pem',master_ssl.crt',master_ssl_key='/etc/mysql/ssl/slave.key';
Query OK, 0 rows affected (0.02 sec)

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.116.231
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-log.000003
          Read_Master_Log_Pos: 506
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-log.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: 817
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: Yes
           Master_SSL_CA_File: /etc/mysql/ssl/cacert.pem
           Master_SSL_CA_Path: 
              Master_SSL_Cert: /etc/mysql/ssl/slave.crt
            Master_SSL_Cipher: 
               Master_SSL_Key: /etc/mysql/ssl/slave.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)


//查看測試的結果;複製成功!~~
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.116.231
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-log.000003
          Read_Master_Log_Pos: 779                    //複製的位置是779
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 802
        Relay_Master_Log_File: mysql-log.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: 779
              Relay_Log_Space: 1090
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: Yes
           Master_SSL_CA_File: /etc/mysql/ssl/cacert.pem
           Master_SSL_CA_Path: 
              Master_SSL_Cert: /etc/mysql/ssl/slave.crt
            Master_SSL_Cipher: 
               Master_SSL_Key: /etc/mysql/ssl/slave.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)


至此,MariaDB數據庫的複製的幾種模型介紹完畢! O(∩_∩)O 謝謝~~~




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章