MySQL路由器8.0  / 第 3 章部署 MySQL Router  /  3.1 引导 MySQL 路由器

3.1 引导 MySQL 路由器

这是一个简短的示例,用于演示如何部署 MySQL Router 以使用引导程序使用 InnoDB Cluster。有关其他信息,请参阅 --bootstrap和其他 引导程序选项

此示例使用 --directory选项创建一个独立的 MySQL Router 实例,启用套接字,用于--account自定义 Router 的 MySQL 用户名, 如果帐户尚不存在,则设置为仅引导--account-createalways并创建它。此示例假定名为的 InnoDB ClustermyCluster已经存在。

$> 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 路由器配置文件 ( 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 Cluster 是元数据的来源,destinations使用 InnoDB Cluster 元数据缓存来动态配置主机信息。相关的命令行选项:

  • --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 集群