当前位置: 首页 > news >正文

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

CSDN 成就一亿技术人!

今天刚开学第一天给大家分享一期:MySQL集群双主的配置需求和命令

CSDN 成就一亿技术人!

神秘泣男子主页:作者首页    <————

MySQL专栏      :MySQL数据库专栏<————

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

工作原理:

  • 两台 MySQL 实例都可读写,互为主备。
  • 默认情况下,只有一台主节点(称为主写节点)负责数据的写入,另一台主节点(称为备写节点)处于备用状态。
  • 主写节点将变更记录(binlog)发送给备写节点,备写节点应用变更记录,保证数据一致性。
  • 当主写节点发生故障时,备写节点可以被提升为主写节点,继续提供服务。

优点:

  • 提高读写性能: 两台主节点可以同时处理读写请求,从而提高数据库的整体性能。
  • 增强高可用性: 如果一台主节点发生故障,另一台主节点可以继续提供服务,从而保证数据库的高可用性。

缺点:

  • 数据一致性风险: 双主架构需要保证两台主节点的数据一致性,这可能会带来一些风险,例如数据冲突等。
  • 配置和管理复杂度: 双主架构的配置和管理比单主架构复杂,需要 DBA 具备一定的专业知识。

应用场景:

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

常见实现方式:

  • 双向复制: 两台主节点之间通过 binlog 进行双向复制,保证数据一致性。
  • 仲裁器: 引入一个仲裁器协调两台主节点之间的写入操作,保证数据一致性。

双主配置命令

1.master1配置

1.修改配置文件

配置完成后重启

vim /etc/my.cnflog_bin
server-id=1
gtid_mode=on
enforce_gtid_consistency=on
binlog_format=rowlog_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 eventMaster_Host: 192.168.180.180Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: master1-bin.000002Read_Master_Log_Pos: 1720Relay_Log_File: master2-relay-bin.000004Relay_Log_Pos: 966Relay_Master_Log_File: master1-bin.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 1720Relay_Log_Space: 2452Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: 0a562cb8-bf46-11ee-b233-000c2950269eMaster_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-9Executed_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-9,
235616ef-b8fc-11ee-86c1-000c2952be42:1-2Auto_Position: 1Replicate_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 eventMaster_Host: 192.168.180.181Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: master2-bin.000002Read_Master_Log_Pos: 194Relay_Log_File: master1-relay-bin.000003Relay_Log_Pos: 411Relay_Master_Log_File: master2-bin.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 194Relay_Log_Space: 1233Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2Master_UUID: 235616ef-b8fc-11ee-86c1-000c2952be42Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 235616ef-b8fc-11ee-86c1-000c2952be42:1-2Executed_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-10,
235616ef-b8fc-11ee-86c1-000c2952be42:1-2Auto_Position: 1Replicate_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)
http://www.lryc.cn/news/306995.html

相关文章:

  • 网络安全之安全事件监测
  • 【BUG 记录】MyBatis-Plus 处理枚举字段和 JSON 字段
  • Web性能优化-详细讲解与实用方法-MDN文档学习笔记
  • 组态王连接施耐德M580PLC
  • pop链构造 [NISACTF 2022]babyserialize
  • 【VIP专属】Python应用案例——基于Keras, OpenCV和MobileNet口罩佩戴识别
  • Doris——荔枝微课统一实时数仓建设实践
  • Stable Diffusion 绘画入门教程(webui)-ControlNet(Inpaint)
  • LeetCode146: LRU缓存
  • 【ArcGIS】基于DEM/LUCC等数据统计得到各集水区流域特征
  • vue3中安装并使用CSS预处理器Sass的方法介绍
  • 过滤器(Filter)
  • AMRT3D数字孪生引擎详解
  • Sqlite数据库详解
  • 基于YOLOv8深度学习+Pyqt5的电动车头盔佩戴检测系统
  • 【数据结构】B树,B+树,B*树
  • 常用实验室器皿耐硝酸盐酸进口PFA材质容量瓶螺纹盖密封效果好
  • 【kubernetes】二进制部署k8s集群之cni网络插件flannel和calico工作原理
  • Pycharm一直打不开,无任何报错
  • 用html编写的小广告板
  • hive中如何取交集并集和差集
  • 2024.2.26
  • 【kubernetes】关于k8s集群的声明式管理资源
  • 8.openEuler操作系统网络管理和防火墙(二)
  • 1904_ARM Cortex M系列芯片特性小结
  • 热闹元宵进行中,如何利用VR全景展示民宿品牌形象?
  • css3实现无缝滚动,鼠标经过暂停
  • SpringCache缓存专题
  • Doris实战——结合Flink构建极速易用的实时数仓
  • 阿里开源低代码引擎 - Low-Code Engine