[Azure] 在 Kubernetes 裡使用 mysql pod 連線到 Azure MySQL 資料庫

[Azure] 在 Kubernetes 裡使用 mysql pod 連線到 Azure MySQL 資料庫

最近在 Azure 上建了一個 Azure Database for MySQL 資料庫,

不過因為防火牆的關係,從家裡是連不上的,

必須和 MySQL 在同一個 vnet (虛擬網路) 裡面才行…

 

因為在同一個 vnet 裡面,也有 Kubernetes,

這時可以在裡面起一個 mysql 的 pod:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ kubectl run mysql-client --image=mysql:5.7 -it --rm --restart=Never -- /bin/bash
If you don't see a command prompt, try pressing enter.
root@mysql-client:/#
$ kubectl run mysql-client --image=mysql:5.7 -it --rm --restart=Never -- /bin/bash If you don't see a command prompt, try pressing enter. root@mysql-client:/#
$ kubectl run mysql-client --image=mysql:5.7 -it --rm --restart=Never -- /bin/bash

If you don't see a command prompt, try pressing enter.
root@mysql-client:/#

 

在這 mysql pod 裡面,再去連 MySQL 資料庫,

因為是在同樣的 vnet 裡面,就不會遇到問題了:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# mysql --host=mytest.mysql.database.azure.com --user=test@mytest --password=abc
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 63869
Server version: 5.6.47.0 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
# mysql --host=mytest.mysql.database.azure.com --user=test@mytest --password=abc mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 63869 Server version: 5.6.47.0 MySQL Community Server (GPL) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
# mysql --host=mytest.mysql.database.azure.com --user=test@mytest --password=abc

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 63869
Server version: 5.6.47.0 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

 

資料庫的操作也都能正常運行:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> show tables;
+---------------------+
| Tables_in_mytest |
+---------------------+
| hostnames |
| ips |
+---------------------+
2 rows in set (0.00 sec)
mysql> show tables; +---------------------+ | Tables_in_mytest | +---------------------+ | hostnames | | ips | +---------------------+ 2 rows in set (0.00 sec)
mysql> show tables;
+---------------------+
| Tables_in_mytest    |
+---------------------+
| hostnames           |
| ips                 |
+---------------------+
2 rows in set (0.00 sec)

 

參考資料:mysql-client in Kubernetes to test your connection to mysql server

(本頁面已被瀏覽過 304 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料