MySQL路由器8.0  / 第 3 章部署 MySQL Router  /  3.2 在沙盒中试用 MySQL Router

3.2 在沙盒中试用 MySQL Router

通过使用 InnoDB Cluster 设置 Router 沙箱来测试 MySQL Router 安装。在这种情况下,路由器充当中间节点,将客户端连接重定向到服务器列表。如果一个服务器出现故障,客户端将被重定向到列表中的下一个可用服务器。

设置 MySQL 服务器沙箱

首先启动三个 MySQL 服务器。您可以通过多种方式执行此操作,包括:

  • 使用 InnoDB Cluster 提供的 MySQL Shell AdminAPI 接口。这是推荐的最简单的方法,并记录在本节中。有关其他信息,请参阅 MySQL AdminAPI

    有关脚本化方法,请参阅 编写 AdminAPI 脚本

  • 通过在三个不同的主机或同一主机上安装三个 MySQL Server 实例。

  • 使用作为mysql-test-run.plMySQL 测试套件框架一部分的脚本。有关其他信息,请参阅MySQL 测试套件

以下示例使用 AdminAPI 方法来设置我们的集群沙箱。这是一个简短的概述,因此请参阅 InnoDB Cluster 手册中的MySQL InnoDB Cluster了解更多详细信息。以下假设您安装了当前版本的 MySQL Shell、MySQL Server 和 MySQL Router。

部署沙盒集群

此示例使用 MySQL Shell AdminAPI 设置一个具有三个 MySQL 实例(一个主实例和两个辅助实例)的 InnoDB 集群,以及一个带有生成配置文件的引导式独立 MySQL 路由器。使用“...”缩短了输出。

Press CTRL+C to copy
$> mysqlsh mysql-js> dba.deploySandboxInstance(3310) ... mysql-js> dba.deploySandboxInstance(3320) ... mysql-js> dba.deploySandboxInstance(3330) ... mysql-js> \connect root@localhost:3310 ... mysql-js> cluster = dba.createCluster("myCluster") ... mysql-js> cluster.addInstance("root@localhost:3320") ... mysql-js> cluster.addInstance("root@localhost:3330") ... mysql-js> cluster.status() { "clusterName": "myCluster", "defaultReplicaSet": { "name": "default", "primary": "127.0.0.1:3310", "ssl": "REQUIRED", "status": "OK", "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", "topology": { "127.0.0.1:3310": { "address": "127.0.0.1:3310", "memberRole": "PRIMARY", "mode": "R/W", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.27" }, "127.0.0.1:3320": { "address": "127.0.0.1:3320", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.27" }, "127.0.0.1:3330": { "address": "127.0.0.1:3330", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": null, "role": "HA", "status": "ONLINE", "version": "8.0.27" } }, "topologyMode": "Single-Primary" }, "groupInformationSourceMember": "127.0.0.1:3310" } mysql-js> \q Bye!

设置路由器

接下来,设置 MySQL Router 以重定向到这些 MySQL 实例。我们将使用引导程序(使用 --bootstrap),并使用创建一个独立的 MySQL Router 安装 --directory。这使用元数据缓存插件来安全地存储凭据。

Press CTRL+C to copy
$> mysqlrouter --bootstrap root@localhost:3310 --directory /tmp/router Please enter MySQL password for root: # Bootstrapping MySQL Router instance at '/tmp/router'... - Creating account(s) (only those that are needed, if any) - Verifying account (using it to run SQL queries that would be run by Router) - Storing account in keyring - Adjusting permissions of generated files - Creating configuration /tmp/router/mysqlrouter.conf # MySQL Router configured for the InnoDB Cluster 'myCluster' After this MySQL Router has been started with the generated configuration $ mysqlrouter -c /tmp/router/mysqlrouter.conf InnoDB Cluster 'myCluster' can be reached by connecting to: ## MySQL Classic protocol - Read/Write Connections: localhost:6446 - Read/Only Connections: localhost:6447 ## MySQL X protocol - Read/Write Connections: localhost:6448 - Read/Only Connections: localhost:6449 $> cd /tmp/router $> ./start.sh

MySQL Router 现已配置并运行,并且正在使用 我们之前设置 的myCluster集群。

测试路由器

现在通过连接到已配置的 MySQL 路由器端口,像连接任何其他 MySQL 服务器一样连接到 MySQL 路由器。

以下示例连接到端口 6446 上的 MySQL Router,我们为读写连接配置的端口:

Press CTRL+C to copy
$> mysql -u root -h 127.0.0.1 -P 6446 -p mysql> SELECT @@port; +--------+ | @@port | +--------+ | 3310 | +--------+

正如演示的那样,我们使用端口 6446 连接到 MySQL Router,但看到我们连接到端口 3310(我们的 PRIMARY)上的 MySQL 实例。接下来让我们连接到一个只读的 MySQL 实例:

Press CTRL+C to copy
$> mysql -u root -h 127.0.0.1 -P 6447 -p mysql> SELECT @@port; +--------+ | @@port | +--------+ | 3320 | +--------+

正如演示的那样,我们使用端口 6447 连接到 MySQL Router,但看到我们在端口 3320(我们的辅助端口之一)上连接到我们的 MySQL 实例。只读模式默认为循环策略,其中下一个连接引用不同的辅助节点:

Press CTRL+C to copy
$> mysql -u root -h 127.0.0.1 -P 6447 -p mysql> SELECT @@port; +--------+ | @@port | +--------+ | 3330 | +--------+

正如演示的那样,我们与端口 6447 的第二个只读连接连接到另一个 MySQL 辅助服务器,在本例中连接到端口 3330 而不是 3320。

现在通过首先终止我们连接到上面的主要 MySQL 实例(端口 3310)来测试故障转移。

Press CTRL+C to copy
$> mysqlsh --uri root@127.0.0.1:6446 mysql-js> dba.killSandboxInstance(3310) The MySQL sandbox instance on this host in /home/philip/mysql-sandboxes/3310 will be killed Killing MySQL instance... Instance localhost:3310 successfully killed.

您可以继续使用 MySQL Shell 来检查连接,但让我们使用与上面相同的mysql客户端示例:

Press CTRL+C to copy
$> mysql -u root -h 127.0.0.1 -P 6446 -p mysql> SELECT @@port; +--------+ | @@port | +--------+ | 3320 | +--------+ $> mysql -u root -h 127.0.0.1 -P 6447 -p mysql> SELECT @@port; +--------+ | @@port | +--------+ | 3330 | +--------+

如图所示,尽管连接到相同的端口(主要端口为 6446,次要端口为 6447),但底层端口发生了变化。我们的新主服务器从端口 3310 更改为 3320,而我们的辅助服务器从 3320 更改为 3330。

我们现在已经演示了 MySQL Router 执行简单的重定向到主要和次要 MySQL 实例的列表。

路由器还默认在生成 mysqlrouter.conf的引导程序中启用 REST API,默认情况下,以下 URL 显示 swagger.json用于本地设置的: https://127.0.0.1:8443/api/20190715/swagger.json。另见第 6 章,MySQL Router REST API