mysql 双主复制_配置步骤、遇到的问题及解决办法

2023-09-15 10:23:55

MySQL主主复制结构区别于主从复制结构。在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上,这样两台服务器互为主从,并且都能向外提供服务。

配置步骤:

一、修改配置文件

(一)服务器A(192.168.115.119)配置如下该配置需要写到 [mysqld]区域内

server-id=10

log-bin=master-bin

log-slave-updates=true

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

binlog_format=STATEMENT

auto-increment-increment = 2

auto-increment-offset = 1

(二)服务器B(192.168.115.120)配置如下该配置需要写到 [mysqld]区域内

server-id=20

log-bin=master-bin

log-slave-updates=true

binlog-ignore-db=mysql

binlog-ignore-db=information_schema

binlog_format=STATEMENT

auto-increment-increment = 2

auto-increment-offset = 2

注:二都只有server-id不同和 auto-increment- offset不同

auto-increment-offset是用来设定数据库中自动增长的起点的,回为这两能服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突

replicate-do-db 可以指定同步的数据库

另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2

(三)两台服务器都重启

systemctl restart mysqld

二、相互授权用户(在A服务器授权一个允许B访问的用户,反之亦然)

(一)在服务器A(192.168.115.119)上

grant replication slave on *.* to 'slave'@'192.168.115.%' identified by '123.com';

flush privileges;

(二)在服务器B(192.168.115.120)上

grant replication slave on *.* to 'slave'@'192.168.115.%' identified by '123.com';

flush privileges;

三、互告bin-log信息

(一)在服务器A(192.168.115.119)上

show master status;

(二)在服务器B(192.168.115.120)上

show master status;

(三)在服务器A(192.168.115.119)上指定master

change master to master_host='192.168.115.120',master_user='slave',master_password='123.com',master_log_file='master-bin.000001',master_log_pos=612;

(四)在服务器B(192.168.115.120)上指定master

change master to master_host='192.168.115.119',master_user='slave',master_password='123.com',master_log_file='master-bin.000001',master_log_pos=612;

四、在两服务器都执行以下命令

start slave;

五、查看状态

show slave status\G;

(一)在服务器A(192.168.115.119)上显示

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State:

                  Master_Host: 192.168.115.120

                  Master_User: slave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master-bin.000001

          Read_Master_Log_Pos: 612

               Relay_Log_File: bogon-relay-bin.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File: master-bin.000001

             Slave_IO_Running: No

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 612

              Relay_Log_Space: 154

              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: 1593

                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

                  Master_UUID:

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp: 230727 10:23:39

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

(二)在服务器B(192.168.115.120)上显示

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State:

                  Master_Host: 192.168.115.120

                  Master_User: slave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master-bin.000001

          Read_Master_Log_Pos: 612

               Relay_Log_File: bogon-relay-bin.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File: master-bin.000001

             Slave_IO_Running: No

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 612

              Relay_Log_Space: 154

              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: 1593

                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

                  Master_UUID:

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp: 230727 10:23:35

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

(三)错误解析

ERROR:

No query specified

此时看到两台服务器显示相同的错误,错误解析

(四)解决问题

1、在主服务器上运行以下命令,获取当前的服务器 ID:

SHOW VARIABLES LIKE 'server_id';

2、在从服务器上运行以下命令,获取当前的服务器 ID:

SHOW VARIABLES LIKE 'server_id';

此时可以看到两台服务器的ID是一样的,因此,更改的两台服务器 MySQL 配置文件/etc/my.cnf,找到 `server-id` 参数,并将其设置为一个不同于主服务器的唯一值。(分别将两个配置文件中最后的`server-id`注释掉)

3、重启两台服务器,确保设置生效

systemctl restart mysqld

4、两台服务器再次进入mysql,使用以下命令启动复制和复制线程:

stop slave;

start slave;

(五)执行完此命令后

在服务器A(192.168.115.119)上显示

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State:

                  Master_Host: 192.168.115.120

                  Master_User: slave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master-bin.000001

          Read_Master_Log_Pos: 612

               Relay_Log_File: bogon-relay-bin.000002

                Relay_Log_Pos: 4

        Relay_Master_Log_File: master-bin.000001

             Slave_IO_Running: No

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 612

              Relay_Log_Space: 154

              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: 1593

                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 20

                  Master_UUID:

             Master_Info_File: /usr/local/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp: 230727 10:45:35

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

ERROR:

No query specified

错误编号 1593 是由于主服务器和从服务器具有相同的 MySQL 服务器 UUID 导致的问题。错误消息 “Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.” 指出,为了使复制工作正常,主服务器和从服务器的 MySQL 服务器 UUID 必须不同。

MySQL 服务器 UUID 是一个用于唯一标识服务器的值,用于复制过程中的身份验证和标识。由于两个服务器的 UUID 相同,从服务器的 IO 线程停止工作,无法继续进行复制。

(六)问题描述:

集群搭建时克隆主服务的镜像导致所有节点的服务UUID都一致,此时在集群中添加节点时会提示UUID冲突报错。

(七)解决方案

1、利用uuid函数生成新的uuid

mysql> select uuid();

+--------------------------------------+

| uuid()                               |

+--------------------------------------+

| 18093dc8-2c8f-11ee-bf5e-000c2973c1d9 |

+--------------------------------------+

1 row in set (0.00 sec)

2、查看配置文件目录

mysql> show variables like 'datadir';

+---------------+------------------------+

| Variable_name | Value                  |

+---------------+------------------------+

| datadir       | /usr/local/mysql/data/ |

+---------------+------------------------+

1 row in set (0.01 sec)

3、编辑配置文件目录

vim /usr/local/mysql/data/auto.cnf

4、uuid修改新生成的uuid

server-uuid=18093dc8-2c8f-11ee-bf5e-000c2973c1d9

5、重启服务

service mysqld restart

之后再次进入mysql,两台服务器分别执行show master status;发现下图所示数据发生改变,然后在两台服务器上再次指定master,开启slave,查看slave状态

直到看到了两个yes,即:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

说明已经配置成功了

六、测试是否同步

(一)在服务器A(192.168.115.119)上操作

create database jx;

show databases;

(二)在服务器B(192.168.115.120)上操作

show databases;

(三)在服务器B(192.168.115.120)上操作

create database jx1;

show databases;

(四)在服务器A(192.168.115.119)上操作

show databases;

在测试的过程当中,我也遇到一些问题主要是两台机器互相通信的问题

请注意,一定要保持两台的服务器的mysql端口都向对方打开,要不然是不能成功同步的。

更多推荐

中级职称评审论文重要吗?是不是必须要论文呢?

现在评中级职称职称对论文有什么要求?没有论文可以参与职称评审吗?建筑中级职称怎么评?那自然是从多方面来考核人才是否具备了评中级工程师的能力,职称论文就是考核的标准之一。甘建二告诉你,现在评职称论文是很重要的,没有论文职称是通过不了的,所以评职称的小伙伴,首先建议您这边先考虑发表论文,湖北这边职称评审对于论文的一个要求是

多语言多平台给线程增加命名

在许多编程语言和多线程库中,可以为线程分配一个名称以便更好地识别和调试线程。以下是在一些常见编程语言中如何为线程增加命名的示例:1.Python(使用threading模块):在Python中,你可以使用threading模块来创建和管理线程。要为线程增加名称,可以在创建线程对象时指定name参数:importthre

[C++随笔录] vector使用

vector使用初始化排序算法reverse和resize通过上一篇文章string的模拟实现,其实我们就已经踏入了STL的门槛.STL容器的大致用法是差不多的⇒那我们这篇博客就讲一点跟string类不一样的新颖的东西初始化跟string一样,vector可以采用下面的形式初始化//默认空间,默认初始化vector<i

瑞芯微:基于RK3568的ocr识别

光学字符识别(OpticalCharacterRecognition,OCR)是指对文本资料的图像文件进行分析识别处理,获取文字及版面信息的过程。亦即将图像中的文字进行识别,并以文本的形式返回。OCR的应用场景卡片证件识别类:大陆、港澳台身份证、通行证、护照识别,卡类识别,车辆类驾驶证识别、行驶证识别,执照识类识别,企

浅谈GPGPU任务调度-1

文章目录前言1.Stream的概念1.文献1的工作2.Hyper-Q的改进前言转自GPUandComputing公众号在先前的文章中《近距离看GPU计算(2)》我们介绍了GPUSM单元以ThreadBlock为单位的调度方法,这些的Block属于同一个kernel任务,当然处于相同的进程上下文(CUDAContext)

什么是BDD测试(行为驱动开发测试)?

1、什么是BDD测试?BDD(BehaviorDrivenDevelopment)测试,即行为驱动开发测试,是一种基于用户行为和需求的软件测试方法。通过将测试用例编写为自然语言脚本,BDD测试可以促进业务需求、开发和测试团队之间的沟通和协作,从而提高代码的可读性、可维护性和可重复性。BDD测试的优点在于,它能够将开发、

微信小程序与idea后端如何进行数据交互

交互使用的其实就是调用的req.get('url')方法进行路径访问,你要先保证自己的springboot项目已经成功运行了:如下:如何交互的?微信小程序:如下为index.js页面在onLoad()事件中调用方法Project.findAllCities()要在当前js页面中先引入project.js别名Projec

Java——》线程的打断(停止正在运行的线程)

推荐链接:总结——》【Java】总结——》【Mysql】总结——》【Redis】总结——》【Kafka】总结——》【Spring】总结——》【SpringBoot】总结——》【MyBatis、MyBatis-Plus】总结——》【Linux】总结——》【MongoDB】总结——》【Elasticsearch】Java—

Linux下git安装及使用

Linux下Git使用1.git的安装sudoaptinstallgit安装完,使用git--version查看git版本2.配置gitgitconfig--globaluser.name"YourName“##配置用户gitconfig--globaluser.emailemail@example.com##配置邮箱

vuex实现简易购物车加购效果

目录一、加购效果动图二、前提条件三、开始操作四、解决vuex刷新数据丢失问题五、最终效果一、加购效果动图二、前提条件创建了vue项目,安装了vuex三、开始操作目录结构如下:main.js文件中引入store:importVuefrom'vue'importAppfrom'./App.vue'importstorefr

服务器的维护是如何操作

服务器的维护是如何操作服务器可以说是不可或缺的资源,因为现在网络技术发达,我们的生活也都离不开网络的存在,我们想要获取的业务、资料等大多是通过网络进行,所以想要顺应潮流并获得发展,肯定需要服务器来将企业的相关信息与产品等发布到网络中,供客户选择。那应该如何维护好服务器呢?硬件维护1、增加内存和硬盘容量的工作。增加内存是

热文推荐