MySQL集群 双主架构(配置命令)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySQL集群 双主架构(配置命令)

MySQL双主是一种高可用性和容错性的数据库架构,有两个主数据库(Master)。这种架构允许在其中一个主数据库出现故障时,系统仍然能够正常运行,并且在故障恢复后能够继续正常工作。


工作原理:


两台 MySQL 实例都可读写,互为主备。

默认情况下,只有一台主节点(称为主写节点)负责数据的写入,另一台主节点(称为备写节点)处于备用状态。

主写节点将变更记录(binlog)发送给备写节点,备写节点应用变更记录,保证数据一致性。

当主写节点发生故障时,备写节点可以被提升为主写节点,继续提供服务。

优点:


提高读写性能: 两台主节点可以同时处理读写请求,从而提高数据库的整体性能。

增强高可用性: 如果一台主节点发生故障,另一台主节点可以继续提供服务,从而保证数据库的高可用性。

缺点:


数据一致性风险: 双主架构需要保证两台主节点的数据一致性,这可能会带来一些风险,例如数据冲突等。

配置和管理复杂度: 双主架构的配置和管理比单主架构复杂,需要 DBA 具备一定的专业知识。

应用场景:


对读写性能要求较高的应用

对高可用性要求较高的应用

常见实现方式:


双向复制: 两台主节点之间通过 binlog 进行双向复制,保证数据一致性。

仲裁器: 引入一个仲裁器协调两台主节点之间的写入操作,保证数据一致性。

双主配置命令

1.master1配置

1.修改配置文件

配置完成后重启

vim /etc/my.cnf
 
log_bin
server-id=1
gtid_mode=on
enforce_gtid_consistency=on
binlog_format=row
 
log_bin:
此配置项启用二进制日志,它是 MySQL 复制所必需的。
server-id:
此配置项用于为 MySQL 服务器分配唯一的标识符。在复制设置中,每个服务器都应该有一个唯一的 server-id。在您的配置中,服务器的ID被设置为1。确保每个服务器都有一个唯一的ID。
gtid_mode:
此配置项启用 GTID 模式。GTID 是用于在不同 MySQL 实例之间唯一标识事务的机制。启用 GTID 有助于简化复制配置和处理。
enforce_gtid_consistency:
此配置项强制执行 GTID 一致性。这确保在执行复制时事务的一致性。
binlog_format=row:
此配置项指定二进制日志的格式。在您的配置中,设置为row,表示以行为基础记录二进制日志。这是推荐的设置,因为它提供更好的灵活性和一致性。
2.创建授权用户
grant replication slave on *.* to 'rep'@'192.168.180.%' identified by 'Sunshao-123';
 
rep是用户名称
@后边跟上服务器网段


2.master2配置

1.修改配置文件

配置完成后重启

log_bin
server-id=2
#GTID:
gtid_mode=on #开启gtid模式
enforce_gtid_consistency=on
binlog_format=row



1.检测创建账户是否可用
mysql -h 目标服务器 -u创建用户 -p'密码'

master2 访问 master1

2.设置主服务器
4.***设置主服务器**    指向master1
mysql> change master to
    -> master_host='另外一个主服务器的IP',
    -> master_user='rep',
    -> master_password='Sunshao-123',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;        开启复制
Query OK, 0 rows affected (0.00 sec)

3.查看线程状态
mysql> show slave status \G;
************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.180.180
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master1-bin.000002
          Read_Master_Log_Pos: 1720
               Relay_Log_File: master2-relay-bin.000004
                Relay_Log_Pos: 966
        Relay_Master_Log_File: master1-bin.000002
             Slave_IO_Running: Yes
            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: 1720
              Relay_Log_Space: 2452
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 0a562cb8-bf46-11ee-b233-000c2950269e
             Master_Info_File: /var/lib/mysql/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: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-9
            Executed_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-9,
235616ef-b8fc-11ee-86c1-000c2952be42:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)


都为yes表示成功

接下来返回master1继续配置

.***设置主服务器**    指向master2
mysql> change master to
    -> master_host='另外一个主服务器的IP',
    -> master_user='rep',
    -> master_password='Sunshao-123',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.180.181
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master2-bin.000002
          Read_Master_Log_Pos: 194
               Relay_Log_File: master1-relay-bin.000003
                Relay_Log_Pos: 411
        Relay_Master_Log_File: master2-bin.000002
             Slave_IO_Running: Yes
            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: 194
              Relay_Log_Space: 1233
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
                  Master_UUID: 235616ef-b8fc-11ee-86c1-000c2952be42
             Master_Info_File: /var/lib/mysql/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: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 235616ef-b8fc-11ee-86c1-000c2952be42:1-2
            Executed_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-10,
235616ef-b8fc-11ee-86c1-000c2952be42:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

测试

master1

master2同步master1

master1上
mysql> insert into t1 values(666666);
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from test.t1;
+--------+
| id     |
+--------+
|     11 |
|     22 |
|     22 |
|     22 |
| 666666 |
+--------+
5 rows in set (0.00 sec)
 
mysql> 
 
 
master2上
mysql> select * from test.t1;
+--------+
| id     |
+--------+
|     11 |
|     22 |
|     22 |
|     22 |
| 666666 |
+--------+
5 rows in set (0.01 sec)
 

master2

master1同步master2

master2上
mysql> insert into test.t1 values(77777);
Query OK, 1 row affected (0.01 sec)
 
mysql> select * from test.t1;
+--------+
| id     |
+--------+
|     11 |
|     22 |
|     22 |
|     22 |
| 666666 |
|  77777 |
+--------+
6 rows in set (0.00 sec)
 
mysql> 
 
master1上
mysql> select * from test.t1;
+--------+
| id     |
+--------+
|     11 |
|     22 |
|     22 |
|     22 |
| 666666 |
|  77777 |
+--------+
6 rows in set (0.00 sec)



相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
16天前
|
NoSQL 关系型数据库 MySQL
微服务架构下的数据库选择:MySQL、PostgreSQL 还是 NoSQL?
在微服务架构中,数据库的选择至关重要。不同类型的数据库适用于不同的需求和场景。在本文章中,我们将深入探讨传统的关系型数据库(如 MySQL 和 PostgreSQL)与现代 NoSQL 数据库的优劣势,并分析在微服务架构下的最佳实践。
|
2月前
|
Ubuntu Linux
查看Linux系统架构的命令,查看linux系统是哪种架构:AMD、ARM、x86、x86_64、pcc 或 查看Ubuntu的版本号
查看Linux系统架构的命令,查看linux系统是哪种架构:AMD、ARM、x86、x86_64、pcc 或 查看Ubuntu的版本号
215 3
|
2月前
|
存储 关系型数据库 MySQL
初步了解MySQL数据库的基本命令
初步了解MySQL数据库的基本命令
29 0
|
9天前
|
存储 关系型数据库 MySQL
MySQL基础命令及使用示例
这些基础命令构成了与MySQL数据库交互的核心,理解并掌握它们对于进行有效的数据库操作至关重要。在实际使用中,建议结合实际案例和需求来练习这些命令,以加深理解和提高效率。
19 3
|
10天前
|
存储 关系型数据库 MySQL
MySQL基础命令及使用示例
这些基础命令构成了与MySQL数据库交互的核心,理解并掌握它们对于进行有效的数据库操作至关重要。在实际使用中,建议结合实际案例和需求来练习这些命令,以加深理解和提高效率。
36 4
|
2月前
|
负载均衡 算法 关系型数据库
MySQL集群如何实现负载均衡?
【8月更文挑战第16天】MySQL集群如何实现负载均衡?
72 6
|
2月前
|
存储 负载均衡 关系型数据库
MySQL集群
【8月更文挑战第16天】MySQL集群
36 5
|
2月前
|
SQL 负载均衡 关系型数据库
*配置MySQL集群
【8月更文挑战第16天】*配置MySQL集群
35 2
|
2月前
|
SQL 关系型数据库 MySQL
Nacos 1.2.1 集群搭建(二)MySQL、cluster 配置
Nacos 1.2.1 集群搭建(二)MySQL、cluster 配置
52 1
|
2月前
|
SQL 关系型数据库 MySQL
(二十五)MySQL主从实践篇:超详细版读写分离、双主热备架构搭建教学
在上篇《主从原理篇》中,基本上把主从复制原理、主从架构模式、数据同步方式、复制技术优化.....等各类细枝末节讲清楚了,本章则准备真正对聊到的几种主从模式落地实践,但实践的内容通常比较枯燥乏味,因为就是调整各种配置、设置各种参数等步骤。
265 2
下一篇
无影云桌面