MySQL主主复制及相关的排坑

MySQL主主复制及相关的排坑

主主复制的本质就是2台MySQL服务器互为主从。
但如此配置极易产生问题,如数据不一致导致主键的冲突,以及一些其他的错误。
为了减少主键冲突的情况,可以考虑让两个节点的id分别使用技术和偶数,这就需要用到两个服务器选项来配置。

auto_increment_offset       #设置id的开始点
auto_increment_increment    #设置id的步进

主主复制工作中不推荐使用,如确实需要使用,也将其当为主从来使用。

主主复制的搭建

使用2台主机来配置主主复制

主机 ip
Master1 192.168.73.110
Master2 192.168.73.111

配置Master1

1.修改配置文件

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log-bin
server-id=1
auto_increment_offset=1
auto_increment_increment=2

2.启动MySQL服务

[[email protected] ~]# systemctl start mariadb

3.查看二进制日志位置

[[email protected] ~]# mysql -e "SHOW MASTER LOGS;"
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+

4.创建一个用来复制数据的用户

[[email protected] ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO ‘repluser‘@‘192.168.73.%‘ IDENTIFIED BY ‘centos‘;"

配置Master2为Master1的从节点

1.修改配置文件

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log-bin
server-id=2
auto_increment_offset=1
auto_increment_increment=2

2.设置CHANGE MASTER TO

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.73.110‘, MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘centos‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mariadb-bin.000001‘,MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)

3.查看从节点状态,确认无误

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No

4.启动线程

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

5.再次查看从节点状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 407
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 693
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes          #线程已经全部启动

6.查看二进制日志位置
查看二级制日志位置用于,给Master1作为从节点使用。由于Master2上无数据二进制日志为干净日志,所以可以直接供Master1使用。

MariaDB [(none)]> SHOW MASTER LOGS;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)

配置Master1为Master2的从节点

1.输入CHANGE MASTER TO的信息

CHANGE MASTER TO MASTER_HOST=‘192.168.73.111‘, MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘centos‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mariadb-bin.000001‘,MASTER_LOG_POS=245;

2.查看从状态,确认信息无误

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.73.111
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No

3.启动线程

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

4.再次查看slave status

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.111
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

主主复制搭建完毕

测试

测试一、查看Master1输入数据,Master2能否复制

1.从Master1上导入hellodb数据库

[[email protected] ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[[email protected] ~]# mysql < hellodb_innodb.sql
[[email protected] ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+

2.从节点上查看数据库

[[email protected] ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+

测试二、Master2插入数据查看Master1是否能复制

1.在Master2中插入条记录

[[email protected] ~]# mysql -e "INSERT hellodb.teachers(name,age) VALUE (‘Ye Fan‘,‘25‘);"
[[email protected] ~]# mysql -e "INSERT hellodb.teachers(name,age) VALUE (‘Shi Hao‘,‘20‘);"
[[email protected] ~]# mysql -e "SELECT * FROM hellodb.teachers"
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
|   5 | Ye Fan        |  25 | NULL   |
|   7 | Shi Hao       |  20 | NULL   |      #此处可以看到插入数据时主键tid是以2为步进递增的。
+-----+---------------+-----+--------+

2.在Master1上查看数据

[[email protected] ~]# mysql -e "SELECT * FROM hellodb.teachers;"
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
|   5 | Ye Fan        |  25 | NULL   |
|   7 | Shi Hao       |  20 | NULL   |
+-----+---------------+-----+--------+

测试三、两边同时创建一张相同的表

1.同时对两个主机做出创建表的操作

2.查看Master1的hellodb库

[[email protected] ~]# mysql -e "SHOW TABLES FROM hellodb"
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| test              |
| toc               |
+-------------------+

3.查看Master2的hellodb库

[[email protected] ~]# mysql -e "SHOW TABLES FROM hellodb"
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| test              |
| toc               |
+-------------------+

此处看上好像没问提

测试四、继续插入数据,从看看复制状况

1.在Master1上继续往hellodb.test表中插入数据

[[email protected] ~]# mysql -e "INSERT hellodb.test VALUE(1,‘Tang San‘);"

2.Master2上查看复制状况

[[email protected] ~]# mysql
MariaDB [(none)]> SELECT * FROM hellodb.test;
Empty set (0.00 sec)

#没有复制到数据
查错

分别查看Master1和Master2主机上的SLAVE STATUS;
Master1状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.111
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 871
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 1018
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1050
                   Last_Error: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘‘. Query: ‘CREATE TABLE hellodb.test(id int auto_increment primary key,name  char(20))‘
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 732
              Relay_Log_Space: 1453
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1050
               Last_SQL_Error: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘‘. Query: ‘CREATE TABLE hellodb.test(id int auto_increment primary key,name  char(20))‘
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 2
1 row in set (0.00 sec)

ERROR: No query specified

Master2状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.110
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 8360
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 8308
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1050
                   Last_Error: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘‘. Query: ‘CREATE TABLE hellodb.test(id int auto_increment primary key,name  char(20))‘
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 8022
              Relay_Log_Space: 8942
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1050
               Last_SQL_Error: Error ‘Table ‘test‘ already exists‘ on query. Default database: ‘‘. Query: ‘CREATE TABLE hellodb.test(id int auto_increment primary key,name  char(20))‘
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: No query specified

显示出来刚在在创建表时已经复制出错,由于两边同时创建了同一张表发生了冲突

排错

分别在主从节点上停止线程

MariaDB [(none)]> STOP SLAVE;

分别在主从节点上使用sql_slave_skip_counter忽略错误

MariaDB [(none)]> SET GLOBAL sql_slave_skip_counter=1;

分别在主从节点上再次启动线程

MariaDB [(none)]> START SLAVE;

再次在从节点上查test表

[[email protected] ~]# mysql -e "SELECT * FROM hellodb.test;"
+----+----------+
| id | name     |
+----+----------+
|  1 | Tang San |
+----+----------+

此时数据已经能正常复制过去

原文地址:https://blog.51cto.com/11886307/2390814

时间: 2024-08-19 11:15:30

MySQL主主复制及相关的排坑的相关文章

MySQL 主从复制、主主复制、半同步复制

MySQL 复制 =============================================================================== 概述: =============================================================================== MySQL Replication:   1.主从复制的目的和架构 ★Master/Slave(主/从) Master: write/read Slave

搭建MySQL的主从、半同步、主主复制架构

复制其最终目的是让一台服务器的数据和另外的服务器的数据保持同步,已达到数据冗余或者服务的负载均衡.一台主服务器可以连接多台从服务器,并且从服务器也可以反过来作为主服务器.主从服务器可以位于不同的网络拓扑中,由于mysql的强大复制功能,其复制目标可以是所有的数据库,也可以是某些数据库,甚至是某个数据库中的某些表进行复制. MySQL支持的两种复制方案:基于语句复制,基于行复制基于语句复制基于行复制,这两种复制方式都是通过记录主服务器的二进制日志中任何有可能导致数据库内数据发生改变的SQL语句到中

mysql+mycat搭建稳定高可用集群,负载均衡,主备复制,读写分离

主要思路 测试环境 实现mysql主备复制 配置A主mysql 配置B备mysql 验证同步配置结果 验证是否同步 关闭B备mysql的同步,验证读写分离 实现读写分离 安装mycat 配置mycat 启动mycat 测试读写分离 验证是否同步 关闭B备mysql的同步,验证读写分离 数据库性能优化普遍采用集群方式,oracle集群软硬件投入昂贵,今天花了一天时间搭建基于mysql的集群环境. 主要思路 简单说,实现mysql主备复制-->利用mycat实现负载均衡. 比较了常用的读写分离方式,

MySQL数据的主从复制、半同步复制和主主复制详解

一.MySQL复制概述 ⑴.MySQL数据的复制的基本介绍 目前MySQL数据库已经占去数据库市场上很大的份额,其一是由于MySQL数据的开源性和高性能,当然还有重要的一条就是免费~不过不知道还能免费多久,不容乐观的未来,但是我们还是要能熟练掌握MySQL数据的架构和安全备份等功能,毕竟现在它还算是开源界的老大吧! MySQL数据库支持同步复制.单向.异步复制,在复制的过程中一个服务器充当主服务,而一个或多个服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环

mysql+myca搭建稳定高可用集群,负载均衡,主备复制,读写分离

数据库性能优化普遍采用集群方式,oracle集群软硬件投入昂贵,今天花了一天时间搭建基于mysql的集群环境. 主要思路 简单说,实现mysql主备复制-->利用mycat实现负载均衡. 比较了常用的读写分离方式,推荐mycat,社区活跃,性能稳定. 测试环境 MYSQL版本:Server version: 5.5.53,到官网可以下载WINDWOS安装包. 注意:确保mysql版本为5.5以后,以前版本主备同步配置方式不同. linux实现思路类似,修改my.cnf即可. A主mysql.19

mysql主从复制、主主复制与半同步复制的实现

1.主从复制 实验环境:2台装有mariadb的centos6,ip地址分别为192.168.198.203(master ),192.168.194.90(slave) 测试:在master上新建一个数据库,查看slave中是否同步 ##################################################### master上的配置: a. 启动二进制日志:在mariadb的配置文件/etc/my.cnf中添加 [mysqld] log_bin=mysql-bin

mysql的主从,主主,半同步,SSL复制

本实验的目的是实现两台主机上的MySQL数据复制,以及基于SSL的复制. *要注意的两点问题所在: 1,版本问题:复制双方的版本最好一致,若不同,从节点的版本必须高于主节点的版本 2,复制起点问题:(1),从0开始,使用于均为新建服务器.(2),中间开始,就需要完全备份主服务上的数据,并将数据恢复至从服务器: 从服务器从备份时主服务器二进制日志所在位置开始复制. 实验环境: node1:MASTER MariaDB 172.16.18.1 node2:SALVE    MariaDB 172.1

mysql主从和mysql主主和半同步复制

一.准备(主从都需要配置):     yum -y install mysql mysql-server #安装mysql   yum -y install ntpdate #安装时间同步   echo '*/1 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' >>/var/spool/cron/root #配置网络时间同步   service mysqld start #启动服务   chkconfig --add 

MySql --主从复制 主主复制

MySql 复制 系统的扩展方式有:scale up [向上扩展或是垂直扩展]  scale out:[向外扩展或是水平扩展] Mysql的复制就是为了实现mysql的水平扩展 为什么要实现水平扩展 当前端节点很少时可以用垂直扩展的方式进行换更好的硬件,但是这会有上限当达到一定的程度后就无法在继续扩展,同时这种扩展的方式成本会很高. 因此更好的办法还是进行水平扩展.来应对前方的压力.但是这对于mysql水平扩展有一个问题就是数据共享的问题. 共享存储的解决方案有NAS SAN 假设使用的是NAS