MariaDB半同步測試

MariaDB半同步測試

環境:

  • 192.168.205.17: as master server
  • 192.168.205.27: as slave server
  • 192.168.205.37: as slave server

版本:

  • OS: centos 7 1810 with mini install
  • mariadb-5.5.60

目地:

默認狀況下,MySQL的複製功能是異步的,異步複製能夠提供最佳的性能,主庫把binlog日誌發送給從庫即結束,並不驗證從庫是否接收完畢。這意味着當主服務器或從服務器端發生故障時,有可能從服務器沒有接收到主服務器發送過來的binlog日誌,這就會形成主服務器和從服務器的數據不一致,甚至在恢復時形成數據的丟失, 半同步解決了性能和數據安全綜合,在同步從服務器時只要有一個從服務器確認同步完成,即返回結果,不用等到所有複製完成,這樣解決了數據同步複製時要等全部從服務器返回結果,實現了最佳性能和數據安全的結合mysql

步驟:

  1. 安裝數據庫在全部服務器,初始化運行如下腳本
    [root@master ~]#cat /data/maridb_yum.sh 
    rpm -q mariadb-server ||yum install -y mariadb-server
    mkdir /data/{mysql,logs}
    chown mysql:mysql /data/{mysql,logs}
    sed -i '/\[mysqld\]/a log-bin=/data/logs/bin' /etc/my.cnf
    sed -i '/\[mysqld\]/a innodb_file_per_table = on' /etc/my.cnf
    sed -i '/\[mysqld\]/a skip_name_resolve = on' /etc/my.cnf
    sed -i 's@(datadir=).*@\1/data/mysql@' /etc/my.cnf
  2. 修改配置文件
    [root@master ~]#vi /etc/my.cnf
    [mysqld]
    server-id=17
    [root@slave1 ~]#vi /etc/my.cnf
    [mysqld]
    server-id=27    
    [root@slave2 ~]#vi /etc/my.cnf
    [mysqld]
    server-id=37
  3. 主服務器上創建同步賬號及,記錄位置或備份主服器恢復從服務器二選一
    MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.205.%' identified by 'centos';
    MariaDB [(none)]> show master logs;                                                                 
    +------------+-----------+
    | Log_name   | File_size |
    +------------+-----------+
    | bin.000001 |     30373 |
    | bin.000002 |   1038814 |
    | bin.000003 |       406 |
    +------------+-----------+
    3 rows in set (0.00 sec)
  4. 從服務器上修改change master to,並啓用slavesql

    MariaDB [(none)]> CHANGE MASTER TO 
        -> MASTER_HOST='192.168.205.17', 
        -> MASTER_USER='repluser', 
        -> MASTER_PASSWORD='centos', 
        -> MASTER_PORT=3306,
        -> MASTER_LOG_FILE='bin.000003', 
        -> MASTER_LOG_POS=406; 
    Query OK, 0 rows affected (0.01 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: 192.168.205.17
                      Master_User: repluser
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: bin.000003
              Read_Master_Log_Pos: 406
                   Relay_Log_File: mariadb-relay-bin.000002
                    Relay_Log_Pos: 523
            Relay_Master_Log_File: bin.000003
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
  5. 要啓用主從的半同步複製須要安裝一個插件
    [root@centos7 data]#rpm -ql mariadb-server
    /usr/lib64/mysql/plugin/semisync_master.so
    /usr/lib64/mysql/plugin/semisync_slave.so
  6. 在數據庫中能夠查看那些插件安裝
    MariaDB [(none)]> show plugins;
  7. 在主服務器上安裝插件
    MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
    Query OK, 0 rows affected (0.00 sec)
    MariaDB [(none)]> show plugins;
    …
    | rpl_semi_sync_master           | ACTIVE   | REPLICATION        | semisync_master.so | GPL     |
    +--------------------------------+----------+--------------------+--------------------+---------+
    43 rows in set (0.00 sec)
  8. 查看半同步的狀態
    MariaDB [(none)]> show global variables like '%semi%' ;
    +------------------------------------+-------+
    | Variable_name                      | Value |
    +------------------------------------+-------+
    | rpl_semi_sync_master_enabled       | OFF   | #半同步默認off
    | rpl_semi_sync_master_timeout       | 10000 | #超時毫秒,10秒
    | rpl_semi_sync_master_trace_level   | 32    |
    | rpl_semi_sync_master_wait_no_slave | ON    |
    +------------------------------------+-------+
    4 rows in set (0.00 sec)
  9. 啓用半同步
    MariaDB [(none)]> set global rpl_semi_sync_master_enabled=on;
    Query OK, 0 rows affected (0.00 sec)
  10. 查看半同步狀態信息
    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     |
    +--------------------------------------------+-------+
    14 rows in set (0.00 sec)
  11. 在全部的從節點安裝slave semi插件;
    MariaDB [(none)]>  install plugin rpl_semi_sync_slave soname 'semisync_slave.so';       
    Query OK, 0 rows affected (0.00 sec)
    MariaDB [(none)]> show plugins;
    …
    | rpl_semi_sync_slave            | ACTIVE   | REPLICATION        | semisync_slave.so | GPL     |
    +--------------------------------+----------+--------------------+-------------------+---------+
    43 rows in set (0.00 sec)
  12. 查看slave的半同步狀態,並啓用,並須要從新啓動線程,再查看半同步狀態是on才能夠數據庫

    MariaDB [(none)]> show global variables like '%semi%';
    +---------------------------------+-------+
    | Variable_name                   | Value |
    +---------------------------------+-------+
    | rpl_semi_sync_slave_enabled     | OFF   |
    | rpl_semi_sync_slave_trace_level | 32    |
    +---------------------------------+-------+
    2 rows in set (0.00 sec)
    MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on; 
    Query OK, 0 rows affected (0.00 sec)
    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)]> stop slave;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> start slave;
    Query OK, 0 rows affected (0.00 sec)
    MariaDB [(none)]> show global status like '%semi%';  
    +----------------------------+-------+
    | Variable_name              | Value |
    +----------------------------+-------+
    | Rpl_semi_sync_slave_status | ON    |
    +----------------------------+-------+
    1 row in set (0.00 sec)
  13. 此時查看主節點的狀態
    MariaDB [(none)]> show global status like '%semi%';
    +--------------------------------------------+-------+
    | Variable_name                              | Value |
    +--------------------------------------------+-------+
    | Rpl_semi_sync_master_clients               | 2     |
    | 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     |
    +--------------------------------------------+-------+
    14 rows in set (0.00 sec)
  14. 測試,在主節點上建立一個庫
    MariaDB [(none)]> create database db1;
    Query OK, 1 row affected (0.00 sec)
  15. 在從節點上能夠看到同步
    MariaDB [(none)]> show databases;     
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | db1                |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    5 rows in set (0.00 sec)
  16. 此進咱們把slave1停掉,在主節點建立數據庫,顯示能夠成功, 只會複製到slave2節點上
    [root@slave1 ~]#systemctl stop mariadb
    MariaDB [(none)]> create database db2; 
    Query OK, 1 row affected (0.01 sec)
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | db1                |
    | db2                |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    6 rows in set (0.00 sec)
  17. 若是所有停掉從節點,再從主節點建立數據庫,會等10秒纔會顯示成功
    [root@slave2 ~]#systemctl stop mariadb;
    MariaDB [(none)]> create database db3;
    Query OK, 1 row affected (10.00 sec)
  18. 將兩個服務器再從新啓動後,查看數據也會同步
    [root@slave1 ~]#systemctl start mariadb 
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | db1                |
    | db2                |
    | db3                |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    7 rows in set (0.00 sec)
相關文章
相關標籤/搜索