体验 MySQL InnoDB Cluster

时间:2022-05-07
本文章向大家介绍体验 MySQL InnoDB Cluster,主要内容包括部署多个MySQL实例、向集群中添加实例、查看集群状态、部署 Mysql Router、客户端连接 Router、查看集群状态、故障模拟、小结、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

Mysql高可用环境的搭建比较麻烦,这使很多人都不去搭建高可用环境,等到有问题时再说

最近Mysql的动作很快,新版本的发布频繁,推出很多新的好用功能及插件,其中了就包括了简化高可用环境的搭建难度

下面就体验一下新的搭建方法,的确方便了很多

整个过程包括:

  1. 基础环境的安装(mysql 5.7.15、mysql-shell、mysql-router)
  2. 部署多个实例
  3. 创建集群
  4. 部署 Mysql Router
  5. 故障测试

其中第1步的过程较长,便不在本文中介绍,有兴趣自己搭建的小伙伴可以发送消息:01,获取相关安装包和详细的安装说明

所以下面直接从第2步开始

部署多个MySQL实例

我们使用 MySQL Shell 客户端进行操作,先启动

$ mysqlsh

然后在 3310 端口创建一个实例

mysql-js> dba.deployLocalInstance(3310)

返回信息如下

A new MySQL sandbox instance will be created on this host in
/home/mytest/mysql-sandboxes/3310

Please enter a MySQL root password for the new instance:
Deploying new MySQL instance...

Instance localhost:3310 successfully deployed and started.
Use 'connect root@localhost:3310' to connect to the instance.

会要求输入这个实例的密码,我输入了 111111

继续创建两个实例,端口为 3320、3330,密码也都设置为 111111

mysql-js> dba.deployLocalInstance(3320)
...
mysql-js> dba.deployLocalInstance(3330)
...

初始化 InnoDB Cluster

连接到某个实例,执行创建集群的操作,这个被连接的实例就会成为master,被其他实例复制

mysql-js> c root@localhost:3310

返回信息

Creating a Session to 'root@localhost:3310'
Enter password:
Classic Session successfully established. No default schema selected.

会要求输入密码,输入之前设置的密码(111111),成功连接

创建集群,起名为 mycluster

mysql-js> cluster = dba.createCluster('mycluster')

返回信息

A new InnoDB cluster will be created on instance 'root@localhost:3310'.
...
Please specify an administrative MASTER key for the cluster 'mycluster':
Creating InnoDB cluster 'mycluster' on 'root@localhost:3310'...
Adding Seed Instance...

Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.
...
<Cluster:mycluster>

期间会要求为集群指定一个识别码,随意,我输入了:test

向集群中添加实例

集群创建完成,把其他两个实例添加进来

mysql-js> cluster.addInstance("root@localhost:3320")

返回信息

A new instance will be added to the InnoDB cluster
...
Please provide the password for 'root@localhost:3320':
Adding instance to the cluster ...
The instance 'root@localhost:3320' was successfully added to the cluster.

输入3320的密码(111111)后,添加成功

同样的,添加 3330

mysql-js> cluster.addInstance("root@localhost:3330")

查看集群状态

集群创建成功,并添加好了实例,现在查看一下集群的状态

mysql-js> cluster.status()

返回信息

{
    "clusterName": "mycluster",
    "defaultReplicaSet": {
        "status": "Cluster tolerant to up to ONE failure.",
        "topology": {
            "localhost:3310": {
                "address": "localhost:3310",
                "status": "ONLINE",
                "role": "HA",
                "mode": "R/W",
                "leaves": {
                    "localhost:3330": {
                        "address": "localhost:3330",
                        "status": "ONLINE",
                        "role": "HA",
                        "mode": "R/O",
                        "leaves": {}
                    },
                    "localhost:3320": {
                        "address": "localhost:3320",
                        "status": "ONLINE",
                        "role": "HA",
                        "mode": "R/O",
                        "leaves": {}
                    }
                }
            }
        }
    }
}

部署 Mysql Router

集群已经就绪,下面部署 router

$ sudo mysqlrouter --bootstrap localhost:3310

返回信息

...
Please enter the administrative MASTER key for the MySQL InnoDB cluster:
MySQL Router has now been configured for the InnoDB cluster 'mycluster'.

The following connection information can be used to connect to the cluster.

Classic MySQL protocol connections to cluster 'mycluster':
- Read/Write Connections: localhost:6446
- Read/Only Connections: localhost:6447

会要求输入集群的识别码,输入之前创建集群时自定义的那个识别码字符串(test),mysqlrouter便会自动进行配置,给出可以使用的连接地址

启动 Mysql Router

退回到 root 执行

# mysqlrouter &

再回到 mytest 用户

$ su mytest

客户端连接 Router

使用 mysql shell 连接刚才给出的可用连接地址 localhost:6446

$ mysqlsh --uri root@localhost:6446

输入密码(111111)后,进入命令行,切换到sql模式,查看一下现在实际上是连接到了哪个实例

mysql-js> sql
Switching to SQL mode... Commands end with ;

mysql-sql> select @@port;
+--------+
| @@port |
+--------+
|   3310 |
+--------+
1 row in set (0.00 sec)

查看集群状态

客户端连接到 6446,是 router 的端口,是在通过 router 连接到集群,我们再次查看一下集群的状态,看是否和之前的一直

登录

$ mysqlsh --uri root@localhost:6446

查看

mysql-js> cluster=dba.getCluster()
...
Please specify the administrative MASTER key for the default cluster:
<Cluster:mycluster>
mysql-js>
mysql-js> cluster.status()
{
    "clusterName": "mycluster",
...

和之前是一样的

故障模拟

下面把master杀掉,验证 router 是否会自动进行切换

mysql-js> dba.killLocalInstance(3310)

返回信息

The MySQL sandbox instance on this host in
/home/mytest/mysql-sandboxes/3310 will be killed
Killing MySQL instance...
Instance localhost:3310 successfully killed.

3310被成功杀掉了,然后执行查询操作

mysql-js> sql
Switching to SQL mode... Commands end with ;

mysql-sql> SELECT @@port;
ERROR: 2013 (HY000): Lost connection to MySQL server during query
The global session got disconnected.
Attempting to reconnect to 'root@localhost:6446'...
The global session was successfully reconnected.

mysql-sql> SELECT @@port;
+--------+
| @@port |
+--------+
|   3330 |
+--------+
1 row in set (0.00 sec)

可以看到,故障被检查到了,并自动重连,转到了 3330 实例

小结

通过这个体验,可以感受到 Mysql 的进步,搭建高可用集群的过程简单了很多

但需要注意的是:这套方法一定不要在产品环境下使用,因为这还是实验室的预览版,没有正式发布