$ oc new-app \ -e MYSQL_USER=data \ -e MYSQL_PASSWORD=data \ -e MYSQL_DATABASE=data \ -e MYSQL_ROOT_PASSWORD=root centos/mysql-57-centos7
注意: 若是要在CentOS下使用最新的MySQL 5.7版本, 容器鏡像的名稱爲centos/mysql-57-centos7
. 官方文檔使用的是openshift/mysql-55-centos
, 具體參考: 初始化數據庫html
列出運行中的PODS:node
oc get pods
登陸遠程SHELL:mysql
oc rsh ${pod-name}
輸入mysql命令進入MySQL交互式SHELLreact
bash-4.2$ mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -h $HOSTNAME $MYSQL_DATABASE Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.37 MySQL Community Server (GPL) ... mysql>
You must either specify the following environment variables: MYSQL_USER (regex: '^[a-zA-Z0-9_]+$') MYSQL_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$') MYSQL_DATABASE (regex: '^[a-zA-Z0-9_]+$') Or the following environment variable: MYSQL_ROOT_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$') Or both. Optional Settings: MYSQL_LOWER_CASE_TABLE_NAMES (default: 0) MYSQL_LOG_QUERIES_ENABLED (default: 0) MYSQL_MAX_CONNECTIONS (default: 151) MYSQL_FT_MIN_WORD_LEN (default: 4) MYSQL_FT_MAX_WORD_LEN (default: 20) MYSQL_AIO (default: 1) MYSQL_KEY_BUFFER_SIZE (default: 32M or 10% of available memory) MYSQL_MAX_ALLOWED_PACKET (default: 200M) MYSQL_TABLE_OPEN_CACHE (default: 400) MYSQL_SORT_BUFFER_SIZE (default: 256K) MYSQL_READ_BUFFER_SIZE (default: 8M or 5% of available memory) MYSQL_INNODB_BUFFER_POOL_SIZE (default: 32M or 50% of available memory) MYSQL_INNODB_LOG_FILE_SIZE (default: 8M or 15% of available memory) MYSQL_INNODB_LOG_BUFFER_SIZE (default: 8M or 15% of available memory)
注意: 某些環境變量有兩種默認值, 當容器沒有設置內存限制時, 使用固定值, 不然是用容器限制的百分比.sql
在容器環境中惟一修復MYSQL_USER和root密碼的方式是經過MYSQL_PASSWORD, MYSQL_ROOT_PASSWORD環境變量, 由於, MYSQL的配置已經被固化到了部署配置(DC: Deployment Config)當中, 經過命令行獲取POD或DC的環境變量列表:數據庫
➜ .minishift oc set env pod mysql-57-centos7-1-qplng --list # pods mysql-57-centos7-1-qplng, container mysql-57-centos7 MYSQL_DATABASE=data MYSQL_PASSWORD=data MYSQL_ROOT_PASSWORD=root MYSQL_USER=data ➜ .minishift oc set env dc mysql-57-centos7 --list # deploymentconfigs mysql-57-centos7, container mysql-57-centos7 MYSQL_DATABASE=data MYSQL_PASSWORD=data MYSQL_ROOT_PASSWORD=data MYSQL_USER=data
注意:
1. 若是環沒有設置境變量MYSQL_ROOT_PASSWORD
, root 用戶是不能遠程登陸的, 容器內本地環境仍然能夠無密碼登陸, 在產品環境下建議不要設置MYSQL_ROOT_PASSWORD
. 這不影響正常的MYSQL_USER
用戶.
2. 建議不要用其餘非環境變量的方式修改密碼, 由於容器在啓動的時候回經過環境變量充值MySQL用戶密碼.centos
$ oc set env dc <dc_name> [<dc_name_2> ...] \ MYSQL_PASSWORD=<new_password> \ MYSQL_ROOT_PASSWORD=<new_root_password>
容器是經過DC進行實例化的, 若是要對每個DC下的實例都有效, 須要修改DC的環境變量, 首先獲取當前項目(名稱空間)下的DC列表:api
➜ .minishift oc get dc NAME REVISION DESIRED CURRENT TRIGGERED BY hello-microservice 1 1 1 config,image(hello-microservice:latest) mysql-57-centos7 1 1 1 config,image(mysql-57-centos7:latest) nodejs-ex 1 1 1 config,image(nodejs-ex:latest)
而後執行:bash
➜ .minishift oc set env dc mysql-57-centos7 MYSQL_PASSWORD=changed MYSQL_ROOT_PASSWORD=changed deploymentconfig "mysql-57-centos7" updated
注意: 更新環境變量會觸發一次從新部署.app
容器若是沒有使用外部的持久化卷, 那麼當容器從新啓動後, 以前更新的數據將會所有丟失, 所以, 像MySQL這類須要持久化數據存儲支持的應用, 若是丟失數據, 是要命的事情!
MySQL 容器鏡像能夠掛載卷, 以持久化數據庫存儲:
/var/lib/mysql/data
- 該目錄是MySQL存儲數據庫文件的目錄
掛載卷以前, 咱們先建立持久化卷(PV), 以下:
定義名稱爲 db.mysql.data
, 卷的訪問模式爲單用戶非共享卷, 也就是同時只能有一個容器可以掛載這個卷. 測試目的, 卷的大小設置爲較小的值128MiB
.
注意:
MiB
(BINARY UNITS, 二進制單位
)的進制是1024
,MB
(DECIMAL UNITS 十進制單位
)的進制是1000
, 下面是建立卷的描述文件:
apiVersion: v1 kind: PersistentVolumeClaim metadata: creationTimestamp: '2017-10-22T13:04:04Z' name: db.mysql.data namespace: reactive-microservices resourceVersion: '7789' selfLink: >- /api/v1/namespaces/reactive-microservices/persistentvolumeclaims/db.mysql.data uid: 7adb87cd-b729-11e7-8e63-665a136940b1 spec: accessModes: - ReadWriteOnce resources: requests: storage: 128Mi status: phase: Pending
經過 oc get 命令查看持久卷被綁定到哪裏了
➜ .minishift oc get persistentvolume |grep mysql pv0020 100Gi RWO,ROX,RWX Recycle Bound reactive-microservices/db.mysql.data 1d ➜ .minishift oc get persistentvolumeclaim NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS AGE db.mysql.data Bound pv0020 100Gi RWO,ROX,RWX 1m
咱們看到咱們的持久化卷請求(PVC: Persistent Volume Claim), 被綁定到了容量爲100Gi的設備pv0020上了.
進入DC(上圖 Applications > Deployments), 選中 mysql-57-centos7 這個DC. 刪除已經掛載的容器內部卷. 並點擊Add Storage
添加簽名建立的卷.
注意: 由於默認
mysql-57-centos7
部署配置的數據目錄默認掛載位置是/var/lib/mysql/data
, 所以爲了不路徑衝突, 須要刪除內置的卷. 由於修改了配置, Openshift 會自動從新觸發一次mysql-57-centos7
的部署.
如今能夠登陸容器建立一些測試數據了
sh-4.2$ oc rsh mysql-57-centos7-10-sffhd Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | data | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use data; Database changed mysql> show tables; +----------------+ | Tables_in_data | +----------------+ | users | +----------------+ 1 row in set (0.00 sec) mysql> select * from users; +----+--------------+ | id | username | +----+--------------+ | 1 | admin | | 2 | system:admin | | 3 | developer | +----+--------------+ 3 rows in set (0.00 sec) mysql>
注意: 你如今能夠從新部署一次這個DC來看看新實例化的容器MySQL的數據有沒有丟失, 以下: