透過使用 InnoDB Cluster 設定 Router 沙箱來測試 MySQL Router 安裝。在此情況下,Router 作為一個中繼節點,將用戶端連線重新導向到伺服器清單。如果其中一個伺服器失敗,用戶端會被重新導向到清單中下一個可用的伺服器。
首先啟動三個 MySQL Server。您可以使用多種方式執行此操作,包括:
-
使用 InnoDB Cluster 提供的 MySQL Shell AdminAPI 介面。這是建議且最簡單的方法,並在本節中說明。如需其他資訊,請參閱 MySQL AdminAPI。
對於腳本方法,請參閱 腳本化 AdminAPI。
在三個不同的主機或同一主機上安裝三個 MySQL Server 執行個體。
使用屬於 MySQL 測試套件架構一部分的
mysql-test-run.pl
腳本。如需其他資訊,請參閱 MySQL 測試套件。
以下範例使用 AdminAPI 方法設定我們的叢集沙箱。這是一個簡要概述,因此請參閱 InnoDB Cluster 手冊中的 MySQL InnoDB Cluster 以取得其他詳細資訊。以下假設您已安裝最新版本的 MySQL Shell、MySQL Server 和 MySQL Router。
部署沙箱叢集
此範例使用 MySQL Shell AdminAPI 設定具有三個 MySQL 執行個體 (一個主要執行個體和兩個次要執行個體) 的 InnoDB Cluster,以及使用產生的組態檔引導啟動的獨立 MySQL Router。輸出已使用「...」縮短。
$> 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
),並使用 --directory
建立獨立的 MySQL Router 安裝。這會使用元數據快取外掛程式安全地儲存憑證。
$> 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 Server 一樣,透過連線到已設定的 MySQL Router 連接埠來連線到 MySQL Router。
以下範例連線到連接埠 6446 上的 MySQL Router,這是我們為讀寫連線設定的連接埠
$> mysql -u root -h 127.0.0.1 -P 6446 -p
mysql> SELECT @@port;
+--------+
| @@port |
+--------+
| 3310 |
+--------+
如所示,我們使用連接埠 6446 連線到 MySQL Router,但我們看到我們連線到連接埠 3310 (我們的 PRIMARY) 上的 MySQL 執行個體。接下來,讓我們連線到唯讀 MySQL 執行個體
$> mysql -u root -h 127.0.0.1 -P 6447 -p
mysql> SELECT @@port;
+--------+
| @@port |
+--------+
| 3320 |
+--------+
如所示,我們使用連接埠 6447 連線到 MySQL Router,但連線到連接埠 3320 (其中一個次要執行個體) 上的 MySQL 執行個體。唯讀模式預設為循環配置策略,其中下一個連線會參考不同的次要執行個體
$> mysql -u root -h 127.0.0.1 -P 6447 -p
mysql> SELECT @@port;
+--------+
| @@port |
+--------+
| 3330 |
+--------+
如所示,我們到連接埠 6447 的第二個唯讀連線連線到不同的 MySQL 次要執行個體,在此情況下是連接埠 3330 而不是 3320。
現在透過先終止我們以上連線的主要 MySQL 執行個體 (連接埠 3310) 來測試容錯移轉。
$> 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 用戶端範例
$> 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 執行個體清單的簡單重新導向。
Router 也預設在引導啟動時產生的 mysqlrouter.conf
中啟用 REST API,且預設情況下,以下 URL 會為您的本機設定顯示 swagger.json
:https://127.0.0.1:8443/api/20190715/swagger.json
。另請參閱 第 6 章,MySQL Router REST API。