探索MySQL-Cluster奥秘系列之SQL节点故障测试(10)

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 在这一小节中,我继续来对MySQL-Cluster集群环境的高可用性进行测试,接下来我们来看下当SQL节点出现故障时,MySQL-Cluster集群环境是如何保障其高可用性的。

在第一小节中我们讲到了,MySQL Cluster 集群环境下的 SQL 节点负责接收和解析应用端发来的 SQL 命令,然后再将解析后的命令传送至数据节点进行数据的过滤和查询,最后再由 SQL 节点将需要的数据反馈至应用端。

网络异常,图片无法展示
|

那么,如果当某一个SQL节点出现故障,例如 mysqld 进程意外中止,或者服务器出现了宕机,这时MySQL Cluster 集群是否仍可以提供应用端的正常读写呢?这一小节,我们就来对“某个 SQL 节点出现单点故障是否会对 MySQL Cluster 整体的读写造成影响”这个问题进行简单的测试。

我们先检测一下两个SQL节点(mysql04、mysql05)的运行状态。在管理节点上查看每个节点的运行状态,看到每个节点的状态均是正常的。

ndb_mgm> show;
Cluster Configuration
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.6 (mysql-5.7.36 ndb-7.6.20, Nodegroup: 0, *)
id=3 @192.168.1.7 (mysql-5.7.36 ndb-7.6.20, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.3 (mysql-5.7.36 ndb-7.6.20)
[mysqld(API)] 2 node(s)
id=4 @192.168.1.4 (mysql-5.7.36 ndb-7.6.20)
id=5 @192.168.1.5 (mysql-5.7.36 ndb-7.6.20)

然后进行一个简单的测试。

mysql04 节点:

[mysql@mysql04 ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.36-ndb-7.6.20-cluster-gpl MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t2;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
|    2 | bbb  |
+------+------+
2 rows in set (0.00 sec)

mysql05 节点:

[mysql@mysql05 ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.36-ndb-7.6.20-cluster-gpl MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t2;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
|    2 | bbb  |
+------+------+
2 rows in set (0.00 sec)

可以看到,两个 SQL 节点均运行正常,这时我们对 mysql04 节点上的 mysqld 进行 kill 操作,来进行故障的模拟。

[mysql@mysql04 ~]$ ps -ef | grep mysqld
mysql      5759   5738  0 09:05 pts/2    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql      5955   5759  3 09:05 pts/2    00:02:57 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/mysql/mydata --plugin-dir=/usr/local/mysql/lib/plugin --log-error=mysql04.err --pid-file=mysql04.pid --socket=/mysql/mydata/mysql.sock --port=3306
mysql      6135   5738  0 10:27 pts/2    00:00:00 grep --color=auto mysqld
[mysql@mysql04 ~]$ kill -9 5759 5955
[mysql@mysql04 ~]$ 
[1]+  Killed                  /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
[mysql@mysql04 ~]$ 
[mysql@mysql04 ~]$ ps -ef | grep mysqld
mysql      6137   5738  0 10:27 pts/2    00:00:00 grep --color=auto mysqld

可以看到,当前在 mysql04 节点上已经不存在 mysqld 进程了,同时通过 mysql04 节点已经无法连接到 MySQL Cluster 集群环境中。

[mysql@mysql04 ~]$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/mysql/mydata/mysql.sock' (111)

接下来,我们在管理节点上确认一下 mysql04 节点的运行状态。

ndb_mgm> show;
Cluster Configuration
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.6 (mysql-5.7.36 ndb-7.6.20, Nodegroup: 0, *)
id=3 @192.168.1.7 (mysql-5.7.36 ndb-7.6.20, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.3 (mysql-5.7.36 ndb-7.6.20)
[mysqld(API)] 2 node(s)
id=4 (not connected, accepting connect from 192.168.1.4)
id=5 @192.168.1.5 (mysql-5.7.36 ndb-7.6.20)

可以看到,当前 mysql04 节点处于 not connected 状态,即 mysql04 是与 MySQL Cluster 集群环境处于连接中断的状态。

然后,我们在 mysql05 节点上测试是否可以连接到 MySQL Cluster 集群环境,同时是否可以在 mysql05 节点上进行数据的读写操作。如下所示:

[mysql@mysql05 ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.36-ndb-7.6.20-cluster-gpl MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t2;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
|    2 | bbb  |
+------+------+
2 rows in set (0.00 sec)
mysql> update t2 set name='aaaaa' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from t2;
+------+-------+
| id   | name  |
+------+-------+
|    1 | aaaaa |
|    2 | bbb   |
+------+-------+
2 rows in set (0.00 sec)

我们发现,通过 mysql05 节点可以正常连接到 MySQL Cluster 集群环境中,同时也可以对数据进行正常读写,mysql04节点的故障并不影响mysql05节点的运行

那么此时,我们修复 mysql04 节点上的故障,启动 mysql04 节点上的 mysqld 进程。

[mysql@mysql04 ~]$ /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
[1] 6140
[mysql@mysql04 ~]$ 2022-03-10T02:35:08.658547Z mysqld_safe Logging to '/mysql/mydata/mysql04.err'.
2022-03-10T02:35:08.683992Z mysqld_safe Starting mysqld daemon with databases from /mysql/mydata

之后,在管理节点上查看其状态。

ndb_mgm> show;
Cluster Configuration
[ndbd(NDB)] 2 node(s)
id=2 @192.168.1.6 (mysql-5.7.36 ndb-7.6.20, Nodegroup: 0, *)
id=3 @192.168.1.7 (mysql-5.7.36 ndb-7.6.20, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.3 (mysql-5.7.36 ndb-7.6.20)
[mysqld(API)] 2 node(s)
id=4 @192.168.1.4 (mysql-5.7.36 ndb-7.6.20)
id=5 @192.168.1.5 (mysql-5.7.36 ndb-7.6.20)

这个时候,mysql04 节点上的服务也恢复正常了,然后通过 mysql04 连接到 MySQL Cluster 集群环境,测试是否可以读取到最新的数据信息,即读取到 t2 表中 id=1 的 name 字段是否为 aaaaa。如下所示:

[mysql@mysql04 ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.36-ndb-7.6.20-cluster-gpl MySQL Cluster Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t2;
+------+-------+
| id   | name  |
+------+-------+
|    1 | aaaaa |
|    2 | bbb   |
+------+-------+
2 rows in set (0.00 sec)

可以看到,在对 mysql04 节点故障恢复后,仍然是可以读取到最新数据的。即单个SQL节点的故障其实并不会影响到整个MySQL Cluster集群环境的可用性

当某个 SQL 节点出现故障后,只需要把应用端的连接指向可用的 SQL 节点即可。这也就是为什么在工作中的 MySQL Cluster 环境往往需要在数据库的前端配备一个负载均衡器,当某一个 SQL 节点出现故障时,应用端的请求在通过负载均衡器时,自动会分发到可用的 SQL 节点上,从而保证 SQL 节点的故障对于应用程序来说是零影响的。

好了,关于 SQL 节点故障的测试我们就讲到这里。在下一小节中,我们来讲解在 MySQL Cluster 集群环境中如何关闭各个节点服务。

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
17天前
|
SQL 存储 缓存
浅析MySQL中的SQL执行过程
本文探讨了MySQL的体系结构、SQL执行流程及SQL执行时间分析方法。首先介绍了MySQL由连接层、SQL层和存储引擎层构成;接着详细描述了SQL从客户端发送到服务器执行的具体流程;最后,通过启用profiling功能,展示了如何分析SQL执行时间,并说明了MySQL 8.0版本后移除查询缓存的原因。
浅析MySQL中的SQL执行过程
|
7天前
|
存储 SQL 关系型数据库
【MySQL调优】如何进行MySQL调优?从参数、数据建模、索引、SQL语句等方向,三万字详细解读MySQL的性能优化方案(2024版)
MySQL调优主要分为三个步骤:监控报警、排查慢SQL、MySQL调优。 排查慢SQL:开启慢查询日志 、找出最慢的几条SQL、分析查询计划 。 MySQL调优: 基础优化:缓存优化、硬件优化、参数优化、定期清理垃圾、使用合适的存储引擎、读写分离、分库分表; 表设计优化:数据类型优化、冷热数据分表等。 索引优化:考虑索引失效的11个场景、遵循索引设计原则、连接查询优化、排序优化、深分页查询优化、覆盖索引、索引下推、用普通索引等。 SQL优化。
【MySQL调优】如何进行MySQL调优?从参数、数据建模、索引、SQL语句等方向,三万字详细解读MySQL的性能优化方案(2024版)
|
7天前
|
存储 关系型数据库 MySQL
MySQL高级篇——覆盖索引、前缀索引、索引下推、SQL优化、主键设计
覆盖索引、前缀索引、索引下推、SQL优化、EXISTS 和 IN 的区分、建议COUNT(*)或COUNT(1)、建议SELECT(字段)而不是SELECT(*)、LIMIT 1 对优化的影响、多使用COMMIT、主键设计、自增主键的缺点、淘宝订单号的主键设计、MySQL 8.0改造UUID为有序
MySQL高级篇——覆盖索引、前缀索引、索引下推、SQL优化、主键设计
|
5天前
|
SQL 监控 关系型数据库
MySQL数据库中如何检查一条SQL语句是否被回滚
检查MySQL中的SQL语句是否被回滚需要综合使用日志分析、事务状态监控和事务控制语句。理解和应用这些工具和命令,可以有效地管理和验证数据库事务的执行情况,确保数据的一致性和系统的稳定性。此外,熟悉事务的ACID属性和正确设置事务隔离级别对于预防数据问题和解决事务冲突同样重要。
19 2
|
8天前
|
SQL 关系型数据库 MySQL
SQL和MySQL
SQL和MySQL
26 1
|
8天前
|
SQL 关系型数据库 MySQL
MySQL根据某个字段包含某个字符串或者字段的长度情况更新另一个字段的值,如何写sql
MySQL根据某个字段包含某个字符串或者字段的长度情况更新另一个字段的值,如何写sql
24 0
|
21天前
|
前端开发 C# 设计模式
“深度剖析WPF开发中的设计模式应用:以MVVM为核心,手把手教你重构代码结构,实现软件工程的最佳实践与高效协作”
【8月更文挑战第31天】设计模式是在软件工程中解决常见问题的成熟方案。在WPF开发中,合理应用如MVC、MVVM及工厂模式等能显著提升代码质量和可维护性。本文通过具体案例,详细解析了这些模式的实际应用,特别是MVVM模式如何通过分离UI逻辑与业务逻辑,实现视图与模型的松耦合,从而优化代码结构并提高开发效率。通过示例代码展示了从模型定义、视图模型管理到视图展示的全过程,帮助读者更好地理解并应用这些模式。
36 0
|
4天前
|
NoSQL 关系型数据库 MySQL
微服务架构下的数据库选择:MySQL、PostgreSQL 还是 NoSQL?
在微服务架构中,数据库的选择至关重要。不同类型的数据库适用于不同的需求和场景。在本文章中,我们将深入探讨传统的关系型数据库(如 MySQL 和 PostgreSQL)与现代 NoSQL 数据库的优劣势,并分析在微服务架构下的最佳实践。
|
6天前
|
存储 SQL 关系型数据库
使用MySQL Workbench进行数据库备份
【9月更文挑战第13天】以下是使用MySQL Workbench进行数据库备份的步骤:启动软件后,通过“Database”菜单中的“管理连接”选项配置并选择要备份的数据库。随后,选择“数据导出”,确认导出的数据库及格式(推荐SQL格式),设置存储路径,点击“开始导出”。完成后,可在指定路径找到备份文件,建议定期备份并存储于安全位置。
69 11
|
25天前
|
弹性计算 关系型数据库 数据库
手把手带你从自建 MySQL 迁移到云数据库,一步就能脱胎换骨
阿里云瑶池数据库来开课啦!自建数据库迁移至云数据库 RDS原来只要一步操作就能搞定!点击阅读原文完成实验就可获得一本日历哦~