MySQL Router 8.4  /  部署 MySQL Router  /  啟動 MySQL Router

3.1 啟動 MySQL Router

以下是一個簡短的範例,示範如何使用引導方式部署 MySQL Router 以使用 InnoDB Cluster。如需其他資訊,請參閱 --bootstrap 和其他引導選項

此範例使用 --directory 選項建立獨立的 MySQL Router 執行個體,啟用 Socket,使用 --account 自訂 Router 的 MySQL 使用者名稱,並將 --account-create 設定為 always,以便僅在帳戶不存在時才引導。此範例假設名為 myCluster 的 InnoDB Cluster 已存在。

$> 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 設定了四個連接埠和四個 Socket。預設會新增連接埠,而 Socket 是透過傳入 --conf-use-sockets 新增的。名為「myCluster」的 InnoDB Cluster 是中繼資料的來源,而 destinations 正在使用 InnoDB Cluster 中繼資料快取來動態設定主機資訊。相關的命令列選項

  • --conf-use-sockets:選擇性地為所有四種類型的連線啟用 UNIX 網域 Socket,如範例所示。

  • --conf-skip-tcp:選擇性地停用 TCP 連接埠,如果您只想要 Socket,則此選項會與 --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 Cluster