5.1 使用 MySQL Shell 連接

建立一個新的包含 MySQL Shell 的容器,以管理 MySQL InnoDB Cluster。這是首選方法,儘管每個 Kubernetes 的 MySQL Operator 和 MySQL InnoDB Cluster 容器也都有安裝 MySQL Shell,以防您需要對特定 Pod 進行故障排除。

這些範例假設 InnoDB Cluster 名稱為 'mycluster',並且使用 'default' 命名空間。

建立包含 MySQL Shell 的新容器;此範例使用 Kubernetes 的 MySQL Operator 映像檔,但其他映像檔也可以運作,例如 container-registry.oracle.com/mysql/community-server:8.0

此範例使用 MySQL Operator 映像檔建立名為 "myshell" 的新容器,並立即執行 MySQL Shell

$> kubectl run --rm -it myshell --image=container-registry.oracle.com/mysql/community-operator -- mysqlsh
If you don't see a command prompt, try pressing enter.

MySQL JS >

現在從 MySQL Shell 的介面內連接到 InnoDB Cluster

MySQL JS>  \connect root@mycluster

Creating a session to 'root@mycluster'
Please provide the password for 'root@mycluster': ******

MySQL mycluster JS>

root@mycluster 的簡寫有效,因為它假設連接埠 3306(MySQL Router 重新導向至 6446)和 default 命名空間。

或者,可以將其他參數傳遞給 mysqlsh,例如

$> kubectl run --rm -it myshell --image=container-registry.oracle.com/mysql/community-operator -- mysqlsh root@mycluster --sql
If you don't see a command prompt, try pressing enter.
******

MySQL mycluster SQL>

"******" 代表將 MySQL 使用者的密碼輸入到 MySQL Shell,因為 MySQL Shell 預設會提示輸入密碼。root@mycluster 代表主機 mycluster 上的使用者 root,並假設 default 命名空間。設定 "-sql" 會將 MySQL Shell 啟動進入 SQL 模式。

疑難排解特定容器

每個 Kubernetes 的 MySQL Operator 和 MySQL InnoDB Cluster 容器都有安裝 MySQL Shell,因此為了進行疑難排解,您可能需要連接到叢集中的特定 Pod。例如,連接到名為 mycluster-0 的 Pod

$> kubectl --namespace default exec -it mycluster-0 -- bash
Defaulted container "sidecar" out of: sidecar, mysql, initconf (init), initmysql (init)
bash-4.4#

bash-4.4# mysqlsh root@localhost
Please provide the password for 'root@localhost': ******

...