在容器中部署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)