以下是一个简短的示例,演示如何使用引导程序部署 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
:可选地更改端口范围,而不是使用默认端口。这将设置用于经典读写(主)连接的端口,默认为 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 集群。