前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >体验 MySQL InnoDB Cluster

体验 MySQL InnoDB Cluster

作者头像
dys
发布2018-04-04 10:17:38
2.2K0
发布2018-04-04 10:17:38
举报
文章被收录于专栏:性能与架构性能与架构

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

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

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

整个过程包括:

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

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

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

部署多个MySQL实例

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

代码语言:javascript
复制
$ mysqlsh

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

代码语言:javascript
复制
mysql-js> dba.deployLocalInstance(3310)

返回信息如下

代码语言:javascript
复制
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

代码语言:javascript
复制
mysql-js> dba.deployLocalInstance(3320)
...
mysql-js> dba.deployLocalInstance(3330)
...

初始化 InnoDB Cluster

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

代码语言:javascript
复制
mysql-js> \c root@localhost:3310

返回信息

代码语言:javascript
复制
Creating a Session to 'root@localhost:3310'
Enter password:
Classic Session successfully established. No default schema selected.

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

创建集群,起名为 mycluster

代码语言:javascript
复制
mysql-js> cluster = dba.createCluster('mycluster')

返回信息

代码语言:javascript
复制
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

向集群中添加实例

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

代码语言:javascript
复制
mysql-js> cluster.addInstance("root@localhost:3320")

返回信息

代码语言:javascript
复制
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

代码语言:javascript
复制
mysql-js> cluster.addInstance("root@localhost:3330")

查看集群状态

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

代码语言:javascript
复制
mysql-js> cluster.status()

返回信息

代码语言:javascript
复制
{
    "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

代码语言:javascript
复制
$ sudo mysqlrouter --bootstrap localhost:3310

返回信息

代码语言:javascript
复制
...
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 执行

代码语言:javascript
复制
# mysqlrouter &

再回到 mytest 用户

代码语言:javascript
复制
$ su mytest

客户端连接 Router

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

代码语言:javascript
复制
$ mysqlsh --uri root@localhost:6446

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

代码语言:javascript
复制
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 连接到集群,我们再次查看一下集群的状态,看是否和之前的一直

登录

代码语言:javascript
复制
$ mysqlsh --uri root@localhost:6446

查看

代码语言:javascript
复制
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 是否会自动进行切换

代码语言:javascript
复制
mysql-js> dba.killLocalInstance(3310)

返回信息

代码语言:javascript
复制
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被成功杀掉了,然后执行查询操作

代码语言:javascript
复制
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 的进步,搭建高可用集群的过程简单了很多

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-09-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 JAVA高性能架构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 部署多个MySQL实例
    • 初始化 InnoDB Cluster
      • 向集群中添加实例
        • 查看集群状态
        • 部署 Mysql Router
          • 启动 Mysql Router
            • 客户端连接 Router
              • 查看集群状态
              • 故障模拟
              • 小结
              相关产品与服务
              云数据库 SQL Server
              腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档