MySQL Router 8.4  /  部署 MySQL Router  /  引导 MySQL Router

3.1 引导 MySQL Router

以下是一个简短的示例,演示了如何部署 MySQL Router 以使用 InnoDB 集群进行引导。有关更多信息,请参见 --bootstrap 以及其他 引导选项

此示例使用 --directory 选项创建一个独立的 MySQL Router 实例,启用套接字,使用 --account 自定义 Router 的 MySQL 用户名,并将 --account-create 设置为 always,以便仅在帐户不存在时才进行引导。此示例假设已存在名为 myCluster 的 InnoDB 集群。

$> mysqlrouter --bootstrap root@localhost:3310 --directory /tmp/myrouter 
  --conf-use-sockets --account routerfriend --account-create always

Please enter MySQL password for root: 

# Bootstrapping MySQL Router instance at '/tmp/myrouter'...

Please enter MySQL password for routerfriend: 

- Creating account(s) 
- 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/myrouter/mysqlrouter.conf

# MySQL Router configured for the InnoDB Cluster 'myCluster'

After this MySQL Router has been started with the generated configuration

    $ mysqlrouter -c /tmp/myrouter/mysqlrouter.conf

the cluster 'myCluster' can be reached by connecting to:

## MySQL Classic protocol

- Read/Write Connections: localhost:6446, /tmp/myrouter/mysql.sock
- Read/Only Connections:  localhost:6447, /tmp/myrouter/mysqlro.sock

## MySQL X protocol

- Read/Write Connections: localhost:6448, /tmp/myrouter/mysqlx.sock
- Read/Only Connections:  localhost:6449, /tmp/myrouter/mysqlxro.sock

此时,引导过程已创建了一个包含所需文件的 mysqlrouter.conf 文件,并在指定目录中,结果将显示如何启动此 MySQL Router 实例。生成的 MySQL Router 目录类似于

$> ls -l | awk '{print $9}'

data/
log/
mysqlrouter.conf
mysqlrouter.key
run/
start.sh
stop.sh

生成的 MySQL Router 配置文件 (mysqlrouter.conf) 类似于

# File automatically generated during MySQL Router bootstrap
[DEFAULT]
logging_folder=/tmp/myrouter/log
runtime_folder=/tmp/myrouter/run
data_folder=/tmp/myrouter/data
keyring_path=/tmp/myrouter/data/keyring
master_key_path=/tmp/myrouter/mysqlrouter.key
connect_timeout=15
read_timeout=30
dynamic_state=/tmp/myrouter/data/state.json

[logger]
level = INFO

[metadata_cache:myCluster]
cluster_type=gr
router_id=1
user=routerfriend
metadata_cluster=myCluster
ttl=0.5
auth_cache_ttl=-1
auth_cache_refresh_interval=2
use_gr_notifications=0

[routing:myCluster_rw]
bind_address=0.0.0.0
bind_port=6446
socket=/tmp/myrouter/mysql.sock
destinations=metadata-cache://myCluster/?role=PRIMARY
routing_strategy=first-available
protocol=classic

[routing:myCluster_ro]
bind_address=0.0.0.0
bind_port=6447
socket=/tmp/myrouter/mysqlro.sock
destinations=metadata-cache://myCluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=classic

[routing:myCluster_x_rw]
bind_address=0.0.0.0
bind_port=6448
socket=/tmp/myrouter/mysqlx.sock
destinations=metadata-cache://myCluster/?role=PRIMARY
routing_strategy=first-available
protocol=x

[routing:myCluster_x_ro]
bind_address=0.0.0.0
bind_port=6449
socket=/tmp/myrouter/mysqlx.sock
destinations=metadata-cache://myCluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=x

在此示例中,MySQL Router 配置了四个端口和四个套接字。端口默认添加,套接字通过传入 --conf-use-sockets 添加。名为“myCluster”的 InnoDB 集群是元数据的来源,destinations 使用 InnoDB 集群元数据缓存来动态配置主机信息。相关的命令行选项

  • --conf-use-sockets:可以选择为所有四种连接类型启用 UNIX 域套接字,如示例所示。

  • --conf-skip-tcp:可以选择禁用 TCP 端口,如果只想使用套接字,则可以使用此选项与 --conf-use-sockets 一起传入。

  • --conf-base-port:可以选择更改端口范围,而不是使用默认端口。这将设置经典读写 (PRIMARY) 连接的端口,默认值为 6446

  • --conf-bind-address:可以选择更改每个路由的 bind_address 值。

为了演示 MySQL Router 的行为,以下客户端(应用程序)连接到端口 6446,但连接到端口 3310 上的 MySQL 实例。

$> mysql -u root -h 127.0.0.1 -P 6446 -p

...

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

有关更多示例,请参见 设置 MySQL 服务器沙箱部署生产 InnoDB 集群