在容器中部署MySQL
- 創建mysql項目
$ oc new-project mysql
- 登錄OpenShift控制檯,進入Developer視圖,然後在左側菜單中進入“+Add”,在右側點擊Database。
- 在Developer Catalog頁面中進入Database的MySQL分類,然後點擊進入MySQL。
- 在右滑的頁面中點擊Instantiate Template按鈕。
- 提供MySQL所需要的用戶名和密碼,然後點擊Create。
- 執行命令,查看OpenShift創建的對象狀態。成功後應該有1個Pod在運行。
$ oc get all
NAME READY STATUS RESTARTS AGE
pod/mysql-1-922n9 1/1 Running 0 8h
pod/mysql-1-deploy 0/1 Completed 0 8h
NAME DESIRED CURRENT READY AGE
replicationcontroller/mysql-1 1 1 1 8h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql ClusterIP 172.30.153.211 <none> 3306/TCP 8h
NAME REVISION DESIRED CURRENT TRIGGERED BY
deploymentconfig.apps.openshift.io/mysql 1 1 1 config,image(mysql:8.0)
驗證容器化MySQL的可用性
- 執行命令,用上一步的Pod名稱進入該Pod的容器中。
$ oc rsh mysql-1-922n9
sh-4.2$
- 登錄MySQL。
sh-4.2$ mysql -uadmin -ppassword
- 查看目前已有的數據庫,然後切換到sampledb數據庫環境中
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| sampledb |
+--------------------+
2 rows in set (0.00 sec)
mysql> use sampledb;
Database changed
- 創建一個測試表,然後插入一些測試數據。
mysql> create table test (name char(10));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test value ('Tom');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test value ('Jobs');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test value ('Bill');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+
| name |
+------+
| Tom |
| Jobs |
| Bill |
+------+
3 rows in set (0.00 sec)
- 刪除運行MySQL的Pod,然後確認OpenShift會自動創建一個新的Pod實例運行MySQL。
$ oc delete pod mysql-1-922n9
pod "mysql-1-922n9" deleted
$ oc get pod
NAME READY STATUS RESTARTS AGE
mysql-1-deploy 0/1 Completed 0 9h
mysql-1-xpczm 1/1 Running 0 7s
- 再次進入運行MySQL的Pod。
$ oc rsh mysql-1-xpczm
sh-4.2$
- 然後查看在sampledb數據庫中以前創建的test表。
sh-4.2$ mysql -uadmin -ppassword
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 75
Server version: 8.0.17 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> use sampledb;
Database changed
mysql> select * from test;
+------+
| name |
+------+
| Tom |
| Jobs |
| Bill |
+------+
3 rows in set (0.00 sec)