电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 1658 人浏览分享

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

[复制链接]
1658 0

主从服务器的时间要同步,数据库版本最好是一致的,以免造成函数处理、日志读取、日志解析等发生异常。
以下三个主从复制的设置是独立的。
注意防火墙和selinux的影响。
1、简单主从复制的实现
(1)主服务器的配置

1)安装mariadb-server

  1. [root@localhost ~]# yum -y install mariadb-server
复制代码


2)编辑/etc/my.cnf文件

  1. [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)授权可以复制本地数据库信息的主机

  1. [root@localhost ~]# systemctl start mariadb.service (启动mariadb server)

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

  5. MariaDB [(none)]> show master status\G (查看主服务器的状态信息,在从服务器中要用到)
  6. *************************** 1. row ***************************
  7.    File: master-log.000003 (正在使用的二进制日志文件)
  8.   Position: 497 (所处的位置)
  9. Binlog_Do_DB:
  10. Binlog_Ignore_DB:
复制代码


(2)从服务器的配置

1)安装mariadb-server


  1. [root@localhost ~]# yum -y install mariadb-server
复制代码


2)编辑/etc/my.cnf文件

  1. [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)设置要从哪个主服务器的那个位置开始同步

复制代码


(3)测试

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

  1. [root@localhost ~]# mysql < hellodb.sql
复制代码


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

  1. MariaDB [(none)]> show databases;
  2. +--------------------+
  3. | Database   |
  4. +--------------------+
  5. | information_schema |
  6. | hellodb   |(数据库已经同步)
  7. | mysql    |
  8. | performance_schema |
  9. | test    |
  10. +--------------------+
  11. MariaDB [(none)]> use hellodb;
  12. MariaDB [hellodb]> show tables; (hellodb数据库的表也是同步的)
  13. +-------------------+
  14. | Tables_in_hellodb |
  15. +-------------------+
  16. | classes   |
  17. | coc    |
  18. | courses   |
  19. | scores   |
  20. | students   |
  21. | teachers   |
  22. | toc    |
  23. +-------------------+
复制代码


2、双主复制的实现

(1)服务器1的配置

1)安装mariadb-server


  1. [root@localhost ~]# yum -y install mariadb-server
复制代码


2)编辑/etc/my.cnf文件


  1. [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状态

  1. MariaDB [(none)]> show master status\G
  2. *************************** 1. row ***************************
  3.    File: master-log.000003
  4.   Position: 422
  5. Binlog_Do_DB:
  6. Binlog_Ignore_DB:
复制代码


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

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

  2. [root@localhost ~]# mysql

  3. MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd';

  4. 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;

  5. MariaDB [(none)]> start slave;

  6. MariaDB [(none)]> SHOW SLAVE STATUS\G (仅是部分内容)
  7.   Master_Host: 10.1.51.50
  8.   Master_User: repluser
  9.   Master_Port: 3306
  10.   Connect_Retry: 60
  11.   Master_Log_File: master-log.000003
  12.   Read_Master_Log_Pos: 422
  13.   Relay_Log_File: slave-log.000002
  14.   Relay_Log_Pos: 530
  15.   Relay_Master_Log_File: master-log.000003
  16.   Slave_IO_Running: Yes
  17.   Slave_SQL_Running: Yes
  18.   Master_Server_Id: 2
复制代码


(2)服务器2的配置

1)安装mariadb-server

  1. [root@localhost ~]# yum -y install mariadb-server
复制代码


2)编辑/etc/my.cnf文件

  1. [root@localhost ~]# vim /etc/my.cnf
  2.     skip_name_resolve = ON
  3.     innodb_file_per_table = ON
  4.     server-id = 2
  5.     relay-log = slave-log
  6.     lob-bin = master-log
  7.     auto_increment_offset = 2
  8.     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并配置

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

  2. [root@localhost ~]# mysql

  3. MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd';

  4. 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;

  5. MariaDB [(none)]> start slave;

  6. MariaDB [(none)]> show slave status\G (仅是部分内容)
  7.   Master_Host: 10.1.51.60
  8.   Master_User: repluser
  9.   Master_Port: 3306
  10.   Connect_Retry: 60
  11.   Master_Log_File: master-log.000003
  12.   Read_Master_Log_Pos: 422
  13.   Relay_Log_File: slave-log.000003
  14.   Relay_Log_Pos: 530
  15.   Relay_Master_Log_File: master-log.000003
  16.   Slave_IO_Running: Yes
  17.   Slave_SQL_Running: Yes
  18.   Master_Server_Id: 1
复制代码


(3)测试

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

  1. MariaDB [(none)]> create database mydb;
复制代码


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


  1. MariaDB [(none)]> show databases;
  2. +--------------------+
  3. | Database   |
  4. +--------------------+
  5. | information_schema |
  6. | mydb    |
  7. | mysql    |
  8. | performance_schema |
  9. | test    |
  10. +--------------------+
复制代码


3、半同步复制的实现

(1)在主服务器上的配置

1)安装mariadb-server


  1. [root@localhost ~]# yum -y install mariadb-server
复制代码


2)编辑/etc/my.cnf

  1. [root@localhost ~]# vim /etc/my.cnf
  2.     skip_name_resolve = ON
  3.     innodb_file_per_table = ON
  4.     server-id = 1
  5.     log-bin = master-log
复制代码


3)授权可以复制本地数据库信息的主机

  1. [root@localhost ~]# systemctl start mariadb.service (启动mariadb server)

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

  5. MariaDB [(none)]> show master status\G (查看主服务器的状态信息,在从服务器中要用到)
  6. *************************** 1. row ***************************
  7.    File: master-log.000003 (正在使用的二进制日志文件)
  8.   Position: 245 (所处的位置)
  9. Binlog_Do_DB:
  10. Binlog_Ignore_DB:
复制代码


4)安装rpl semi sync_master插件,并启用


  1. [root@localhost ~]# mysql

  2. MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
  3. 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

  1. [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)设置要从哪个主服务器的那个位置开始同步

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

  2. [root@localhost ~]# mysql

  3. 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插件并启用

  1. [root@localhost ~]# mysql

  2. MariaDB [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
  3. MariaDB [(none)]> set global rpl_semi_sync_slave_enabled = ON;
  4. MariaDB [(none)]> start slave;
复制代码


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

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


(3)测试


测试以个人实际情况而定

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

  1. MariaDB [hellodb]> source /root/hellodb.sql;
复制代码


2)在主服务器上查看半同步复制的状态

  1. MariaDB [hellodb]> show master status;
  2. +-------------------+----------+--------------+------------------+
  3. | File    | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  4. +-------------------+----------+--------------+------------------+
  5. | master-log.000003 |  8102 |    |     |
  6. +-------------------+----------+--------------+------------------+

  7. MariaDB [hellodb]> show global status like '%semi%';
  8. +--------------------------------------------+-------+
  9. | Variable_name        | Value |
  10. +--------------------------------------------+-------+
  11. | Rpl_semi_sync_master_clients    | 1  |
  12. | Rpl_semi_sync_master_net_avg_wait_time  | 1684 |
  13. | Rpl_semi_sync_master_net_wait_time   | 60630 |
  14. | Rpl_semi_sync_master_net_waits    | 36 |
  15. | Rpl_semi_sync_master_no_times    | 1  |
  16. | Rpl_semi_sync_master_no_tx     | 1  |
  17. | Rpl_semi_sync_master_status    | ON |
  18. | Rpl_semi_sync_master_timefunc_failures  | 0  |
  19. | Rpl_semi_sync_master_tx_avg_wait_time  | 1884 |
  20. | Rpl_semi_sync_master_tx_wait_time   | 65965 |
  21. | Rpl_semi_sync_master_tx_waits    | 35 |
  22. | Rpl_semi_sync_master_wait_pos_backtraverse | 0  |
  23. | Rpl_semi_sync_master_wait_sessions   | 0  |
  24. | Rpl_semi_sync_master_yes_tx    | 35 |
  25. +--------------------------------------------+-------+
复制代码


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

  1. MariaDB [(none)]> show databases;
  2. MariaDB [(none)]> use hellodb;
  3. MariaDB [hellodb]> select * from students;
复制代码


补充:基于上面的半同步复制配置复制的过滤器,复制过滤最好在从服务器上设置,步骤如下
(1)从服务器的配置

1)关闭mariadb server


  1. [root@localhost ~]# systemctl stop mariadb.service
复制代码


2)编辑/etc/my.cnf文件

  1. [root@localhost ~]# vim /etc/my.cnf
  2. skip_name_resolve = ON
  3. innodb_file_per_table = ON
  4. server-id = 2
  5. relay-log = slave-log
  6. 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

  1. [root@localhost ~]# systemctl start mariadb.service
复制代码


4)重启mariadb server后,半同步复制功能将被关闭,因此要重新启动

  1. MariaDB [(none)]> show global variables like '%semi%';
  2. +---------------------------------+-------+
  3. | Variable_name     | Value |
  4. +---------------------------------+-------+
  5. | rpl_semi_sync_slave_enabled  | OFF |
  6. | rpl_semi_sync_slave_trace_level | 32 |
  7. +---------------------------------+-------+
复制代码


MariaDB [(none)]> set global rpl_semi_sync_slave_enabled = ON;
MariaDB [(none)]> stop slave;(需先关闭从服务器复制功能再重启)
MariaDB [(none)]> start slave;


(2)测试

1)主服务器上的hellodb数据库创建一个新表semitable

  1. MariaDB [hellodb]> create table semitable (id int);
复制代码

2)在从服务器上查看hellodb数据库是否有semitable

  1. MariaDB [(none)]> use hellodb
  2. MariaDB [hellodb]> show tables;(并没有)
  3. +-------------------+
  4. | Tables_in_hellodb |
  5. +-------------------+
  6. | classes   |
  7. | coc    |
  8. | courses   |
  9. | scores   |
  10. | students   |
  11. | teachers   |
  12. | toc    |
  13. +-------------------+
复制代码


3)在主服务器上创建mydb数据库,并为其创建一个tbl1表

  1. MariaDB [hellodb]> create database mydb;
复制代码


4)在从服务器上查看mydb数据库的是否有tbl1表

  1. MariaDB [hellodb]> use mydb;
  2. MariaDB [mydb]> show tables; (可以查看到)
  3. +----------------+
  4. | Tables_in_mydb |
  5. +----------------+
  6. | tbl1   |
  7. +----------------+
复制代码





您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.