半同步複製的實現

一、在主服務器上的配置

1)安裝mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)編輯/etc/my.cnf

[root@localhost ~]# vim /etc/my.cnf
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 1
    log-bin = master-log

3)受權能夠複製本地數據庫信息的主機

[root@localhost ~]# systemctl start mariadb.service (啓動mariadb server)

[root@localhost ~]# mysql
    MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd';
    MariaDB [(none)]> flush privileges;

MariaDB [(none)]> show master status\G (查看主服務器的狀態信息,在從服務器中要用到)
*************************** 1. row ***************************
            File: master-log.000003 (正在使用的二進制日誌文件)
        Position: 245 (所處的位置)
    Binlog_Do_DB: 
Binlog_Ignore_DB:

4)安裝rplsemisync_master插件,並啓用

[root@localhost ~]# mysql

MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
MariaDB [(none)]> set global rpl_semi_sync_master_enabled = ON; 

補充:
MariaDB [(none)]> show plugins;(可查看插件是否激活)
MariaDB [(none)]> show global variables like 'rpl_semi%';(可查看安裝的插件是否啓用)
MariaDB [(none)]> show global status like '%semi%';(可查看從服務器的個數,此時是0個)

二、從服務器的配置

1)安裝mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)編輯/etc/my.cnf文件

[root@localhost ~]# vim /etc/my.cnf
    在[mysqld]段的最後添加如下內容
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 2 (id號不能跟主服務器相同)
    relay-log = slave-log (自定義二進制日誌文件名)

3)設置要從哪一個主服務器的那個位置開始同步

[root@localhost ~]# systemctl start mariadb.service

[root@localhost ~]# mysql

    MariaDB [(none)]> change master to master_host='10.1.51.60',master_user='repluser',master_password='replpasswd',master_log_file='master-log.000003',master_log_pos=245;

4)安裝rplsemisync_slave插件並啓用

[root@localhost ~]# mysql   

    MariaDB [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
    MariaDB [(none)]> set global rpl_semi_sync_slave_enabled = ON;
    MariaDB [(none)]> start slave;

完成上面配置後,能夠在主服務器上查看半同步複製的相關信息,命令以下:mysql

MariaDB [(none)]> show global status like '%semi%';
    Rpl_semi_sync_master_clients    1 (從服務器有一臺)

三、測試

測試以我的實際狀況而定

1)在主服務器上導入事先準備好的數據庫hellodb.sql

MariaDB [hellodb]> source /root/hellodb.sql;

2)在主服務器上查看半同步複製的狀態

MariaDB [hellodb]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 |     8102 |              |                  |
+-------------------+----------+--------------+------------------+

MariaDB [hellodb]> show global status like '%semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 1684  |
| Rpl_semi_sync_master_net_wait_time         | 60630 |
| Rpl_semi_sync_master_net_waits             | 36    |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 1884  |
| Rpl_semi_sync_master_tx_wait_time          | 65965 |
| Rpl_semi_sync_master_tx_waits              | 35    |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 35    |
+--------------------------------------------+-------+

3)在從服務器上查看是否同步

MariaDB [(none)]> show databases;
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> select * from students;

補充:基於上面的半同步複製配置複製的過濾器,複製過濾最好在從服務器上設置,步驟以下

一、從服務器的配置

1)關閉mariadb server

[root@localhost ~]# systemctl stop mariadb.service

2)編輯/etc/my.cnf文件

[root@localhost ~]# vim /etc/my.cnf
    skip_name_resolve = ON
    innodb_file_per_table = ON
    server-id = 2
    relay-log = slave-log
    replicate-do-db = mydb (只複製mydb數據庫的內容)

補充:經常使用的過濾選項以下
    Replicate_Do_DB=
    Replicate_Ignore_DB=
    Replicate_Do_Table=
    Replicate_Ignore_Table=
    Replicate_Wild_Do_Table=
    Replicate_Wild_Ignore_Table=

3)重啓mariadb server

[root@localhost ~]# systemctl start mariadb.service

4)重啓mariadb server後,半同步複製功能將被關閉,所以要從新啓動

MariaDB [(none)]> show global variables like '%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 = ON;
MariaDB [(none)]> stop slave;(需先關閉從服務器複製功能再重啓)
MariaDB [(none)]> start slave;

二、測試

1)主服務器上的hellodb數據庫建立一個新表semitable

MariaDB [hellodb]> create table semitable (id int);

2)在從服務器上查看hellodb數據庫是否有semitable

MariaDB [(none)]> use hellodb
MariaDB [hellodb]> show tables;(並無)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+

3)在主服務器上建立mydb數據庫,併爲其建立一個tbl1表

MariaDB [hellodb]> create database mydb;

4)在從服務器上查看mydb數據庫的是否有tbl1表

MariaDB [hellodb]> use mydb;
MariaDB [mydb]> show tables; (能夠查看到)
+----------------+
| Tables_in_mydb |
+----------------+
| tbl1           |
+----------------+
相關文章
相關標籤/搜索