侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

mariadb的主从复制、主主复制、半同步复制配置详解

数据库  /  管理员 发布于 5年前   208

主从服务器的时间要同步,数据库版本最好是一致的,以免造成函数处理、日志读取、日志解析等发生异常。

以下三个主从复制的设置是独立的。

注意防火墙和selinux的影响。

1、简单主从复制的实现

(1)主服务器的配置

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 = 1 (id号不能跟从服务器相同)
    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: 497 (所处的位置) Binlog_Do_DB: Binlog_Ignore_DB:

(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=497;MariaDB [(none)]> start slave; (启动复制功能)MariaDB [(none)]> show slave status\G (查看从服务器的状态,下面显示的是部分内容) Master_Host: 10.1.51.60 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000003 Read_Master_Log_Pos: 497 Relay_Log_File: slave-log.000002 Relay_Log_Pos: 530 Relay_Master_Log_File: master-log.000003 Slave_IO_Running: Yes  Slave_SQL_Running: Yes Master_Server_Id: 1

(3)测试

1)在主服务器导入事先准备好的数据库

[root@localhost ~]# mysql < hellodb.sql

2)在从服务器查看是否同步

MariaDB [(none)]> show databases;+--------------------+| Database   |+--------------------+| information_schema || hellodb   |(数据库已经同步)| mysql    || performance_schema || test    |+--------------------+MariaDB [(none)]> use hellodb;MariaDB [hellodb]> show tables; (hellodb数据库的表也是同步的)+-------------------+| Tables_in_hellodb |+-------------------+| classes   || coc    || courses   || scores   || students   || teachers   || toc    |+-------------------+

2、双主复制的实现

(1)服务器1的配置

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 = 1 (id号不能跟从服务器相同)
    log-bin = master-log (自定义主服务器的二进制日志文件名)
    relay-log = slave-log (自定义从服务器的二进制日志文件名)
    auto_increment_offset = 1
    auto_increment_increment = 2

3)在服务器2上查看的master状态

MariaDB [(none)]> show master status\G*************************** 1. row ***************************   File: master-log.000003  Position: 422 Binlog_Do_DB: Binlog_Ignore_DB:

4)启动mariadb server并进行如下配置

[root@localhost ~]# systemctl start mariadb.service[root@localhost ~]# mysql MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd'; MariaDB [(none)]> change master to master_host='10.1.51.50',master_user='repluser',master_password='replpasswd',master_log_file='master-log.000003',master_log_pos=422; MariaDB [(none)]> start slave; MariaDB [(none)]> SHOW SLAVE STATUS\G (仅是部分内容)  Master_Host: 10.1.51.50  Master_User: repluser  Master_Port: 3306  Connect_Retry: 60  Master_Log_File: master-log.000003  Read_Master_Log_Pos: 422  Relay_Log_File: slave-log.000002  Relay_Log_Pos: 530  Relay_Master_Log_File: master-log.000003  Slave_IO_Running: Yes  Slave_SQL_Running: Yes  Master_Server_Id: 2

(2)服务器2的配置

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 = 2
    relay-log = slave-log
    lob-bin = master-log
    auto_increment_offset = 2
    auto_increment_increment = 2

3)在服务器1查看master状态

MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: master-log.000003
        Position: 245
    Binlog_Do_DB:
Binlog_Ignore_DB:

4)启动mariadb server并配置

[root@localhost ~]# systemctl start mariadb.service[root@localhost ~]# mysql MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd'; 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; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G (仅是部分内容)   Master_Host: 10.1.51.60  Master_User: repluser  Master_Port: 3306  Connect_Retry: 60  Master_Log_File: master-log.000003  Read_Master_Log_Pos: 422  Relay_Log_File: slave-log.000003  Relay_Log_Pos: 530  Relay_Master_Log_File: master-log.000003  Slave_IO_Running: Yes  Slave_SQL_Running: Yes  Master_Server_Id: 1

(3)测试

1)在任意一台服务器上创建mydb数据库

MariaDB [(none)]> create database mydb;

2)在另一台服务器上查看

MariaDB [(none)]> show databases;+--------------------+| Database   |+--------------------+| information_schema || mydb    || mysql    || performance_schema || test    |+--------------------+

3、半同步复制的实现

(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)安装rpl semi sync_master插件,并启用

[root@localhost ~]# mysqlMariaDB [(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)安装rpl semi sync_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 hellodbMariaDB [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   |+----------------+


  • 上一条:
    MariaDB配置双主复制方案
    下一条:
    mariadb 在低配 VPS 上崩溃问题处理方案
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客