mariadb的主从复制、主主复制、半同步复制

主从服务器的时间要同步,数据库版本最好是一致的,以免造成函数处理、日志读取、日志解析等发生异常。
以下三个主从复制的设置是独立的。
注意防火墙和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)安装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           |
+----------------+

原创文章,作者:pao,如若转载,请注明出处:http://www.178linux.com/60221

(0)
paopao
上一篇 2016-11-20
下一篇 2016-11-21

相关推荐

  • Redhat kickstart 无人值守安装

                       Redhat kickstart 无人值守安装 一、PXE       PXE工作于Client/Server的网络模式,支持可以通…

    系统运维 2016-09-21
  • DNS and BIND 介绍及安装

    DNS DNS是什么 为什么要用DNS 域名 BIND DNS是什么 DNS:Domain Name System,域名系统DNS作为域名和IP地址相互映射的一个分布式数据库,能够让用户更方便的访问互联网。它的正向映射是吧一个主机和IP地址关联起来,它的反向映射则是IP地址到主机名。DNS协议运行在UDP协议上,使用53端口。 为什么要用DNS 连接到互联网…

    Linux干货 2016-05-28
  • linux 怎么使用帮助

    linux怎么使用帮助 Which命令 whatis命令 使用手册来获取帮助-man命令 通过info命令查看帮助 使用软件自带的帮助文档来获取帮助 通过在线文档获取帮助 使用网站和搜索 linux 怎么使用帮助 在linux中有很多命令,配置文件,很多命令选项和参数,我们不可能完全记住的,那工作中遇到怎么办,我们可以查看linux自带的帮助文档,或者利用搜…

    Linux干货 2016-09-05
  • 来马哥教育后

    来北京三天了,说实话,我长了不少见识,甚至可以说是见过了以前一些从没想到过的事情,我拼了命往大城市来,如今如我所愿了,我来的时候发誓对自己说:我要留在北京,我也要积累足够的资本让自己真正成为大城市的上层社会人士,现在吃多少苦我都无所谓。 我以前在建筑工地的时候想:我将来能学IT是最幸福的事了。当时真是这么想的,等我真到马哥教育,我发现,我把问题想的太简单,就…

    Linux干货 2018-03-26
  • vfork 挂掉的一个问题

    在知乎上,有个人问了这样的一个问题——为什么vfork的子进程里用return,整个程序会挂掉,而且exit()不会?并给出了如下的代码,下面的代码一运行就挂掉了,但如果把子进程的return改成exit(0)就没事。 我受邀后本来不想回答这个问题的,因为这个问题明显就是RTFM的事,后来,发现这个问题放在那里好长时间,而挂在下面的几个答案又跑偏得比较严重,…

    Linux干货 2016-08-15
  • Linux基础命令 -管道密切配合使用 tee

    基础命令和管道密切配合使用  tee     基础命令:tee   tee命令 功能: 把命令1的STDOUT保存在文件名中,然后管道输入给命令2; 补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。 使用场景: 1.保存不同阶段的输出 2.复杂管道的故障排除 3.同时查看和记录…

    Linux干货 2016-08-03