半同步复制的实现
admin
2023-03-23 04:01:10
0

1、在主服务器上的配置

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个)

2、从服务器的配置

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;

完成上面配置后,可以在主服务器上查看半同步复制的相关信息,命令如下:

MariaDB [(none)]> show global status like '%semi%';
    Rpl_semi_sync_master_clients    1 (从服务器有一台)

3、测试

测试以个人实际情况而定
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、从服务器的配置

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;

2、测试

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           |
+----------------+


相关内容

热门资讯

终于懂了“炫龙牛牛怎么开挂?”... 网上科普关于“炫龙牛牛有没有挂”话题很是火热,小编也是针对炫龙牛牛作*弊开挂的方法以及开挂对应的知识...
今日重大消息“樱花之盛炸/金/... 今日重大消息“樱花之盛炸/金/花是不是有挂?”(必胜开挂神器)您好,樱花之盛炸/金/花这个游戏其实有...
【第一消息】“茶虞姬开挂器?”... 有 亲,根据资深记者爆料茶虞姬是可以开挂的,确实有挂(咨询软件无需打开直...
【今日要闻】“喜扣跑胡子真的有... 家人们!今天小编来为大家解答喜扣跑胡子透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买...
今日重大通报“新版悟空炸/金/... 有 亲,根据资深记者爆料新版悟空炸/金/花是可以开挂的,确实有挂(咨询软...
【第一资讯】“二八杠到底有挂吗... 网上科普关于“二八杠有没有挂”话题很是火热,小编也是针对二八杠作*弊开挂的方法以及开挂对应的知识点,...
【第一消息】“鹤岗52麻将究竟... 网上科普关于“鹤岗52麻将有没有挂”话题很是火热,小编也是针对鹤岗52麻将作*弊开挂的方法以及开挂对...
玩家攻略科普“乐易四川麻将究竟... 玩家攻略科普“乐易四川麻将究竟有挂吗?”(外卦神器下载)您好,乐易四川麻将这个游戏其实有挂的,确实是...
终于明白“博雅地方棋牌是不是有... 网上科普关于“博雅地方棋牌有没有挂”话题很是火热,小编也是针对博雅地方棋牌作*弊开挂的方法以及开挂对...
【第一财经】“顺欣茶坊真的有挂... 有 亲,根据资深记者爆料顺欣茶坊是可以开挂的,确实有挂(咨询软件无需打开...