Create an

$ oc new-app \
-e MYSQL_USER=data \
-e MYSQL_PASSWORD=data \
-e MYSQL_DATABASE=data \
-e MYSQL_ROOT_PASSWORD=root centos/mysql-57-centos7

Note: If you want to use the latest version of MySQL 5.7 on CentOS, the container image name is CentOS /mysql-57-centos7. OpenShift/MySQL-55-CentOS is used in the official documentation. For details, see: Initialize the database

Run the MySQL command in the container

List the PODS in operation:

oc get pods

Log in to remote SHELL:

oc rsh ${pod-name}

Enter the MySQL command to enter the MySQL Interactive Shell

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)... or \ G. Your MySQL Connection ID is 4 Server Version: 5.5.37 MySQL Community Server (GPL)... mysql>

The environment variable

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)

Note: Some environment variables have two default values. If the container does not set a memory limit, use a fixed value. Otherwise, use a percentage of the container limit.

Change database password

The only way to fix MYSQL_USER and root passwords in the container environment is through the MYSQL_PASSWORD, MYSQL_ROOT_PASSWORD environment variable, because the MySQL configuration has been solidified to the deployment configuration (DC: Deployment Config), get the list of environment variables for POD or DC from the command line:

➜. Minishift oc set env pod mysql-57-centos7-1-qplng --list # pods mysql-57-centos7-1-qplng, mysql-57-centos7-1-qplng, Container 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

Note: 1. If the environment variable MYSQL_ROOT_PASSWORD is not set in the ring, the root user cannot log in remotely, and the local environment in the container can still log in without password. It is recommended not to set MYSQL_ROOT_PASSWORD in the production environment. This does not affect normal MYSQL_USER users. 2. It is recommended that you do not change the password by using other non-environment variables, because the container will recharge the MySQL user password by using the environment variable at startup.

Modify the environment variables for the Deployment configuration (DC: Deployment Config)

$ oc set env dc <dc_name> [<dc_name_2> ...]  \ MYSQL_PASSWORD=<new_password> \ MYSQL_ROOT_PASSWORD=<new_root_password>

Containers are instantiated with DC. If you want to be valid for each instance under DC, you need to modify the DC’s environment variable by first getting the list of DCs under the current project (namespace) :

➜. MiniShift OC get DC NAME Revision Desired Current TRIGGERED BY HelloMicroService 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)

Then execute:

➜  .minishift oc set env dc mysql-57-centos7 MYSQL_PASSWORD=changed MYSQL_ROOT_PASSWORD=changed     
deploymentconfig "mysql-57-centos7" updated

Note: Updating environment variables triggers a redeployment.

Data persistence

If the container does not use an external persistent volume, all the previously updated data will be lost when the container is restarted. Therefore, for applications like MySQL that require persistent data storage support, losing data can be fatal!

Create a persistent volume

The MySQL Container image can mount volumes to persist database storage:

/var/lib/mysql/data – This is the directory where MySQL stores database files

Before we mount the volume, we create the persistent volume (PV) as follows:

The name of the definition is db.mysql.data, and the access mode of the volume is single-user non-shared volume, meaning that only one container can mount the volume ata time. For test purposes, the volume size is set to a smaller value of 128MiB.

Note: The base of MIB (BINARY UNITS) is 1024, and the base of MB(DECIMAL UNITS) is 1000. Here is the description file for creating the volume:

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

Use the oc get command to see where the persistent volume is bound to

➜. Minishift oc get persistentvolume | grep mysql pv0020 100 gi RWO, ROX, RWX Recycle Bound Reactive - microservices/db. Mysql. The data 1 d ➜. Minishift oc get persistentvolumeclaim NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS AGE db.mysql.data Bound pv0020 100Gi RWO,ROX,RWX 1m

We see that our PVC: Persistent Volume Clintend is bound to a 100GI device PV0020.

mount

Enter the DC(Applications > Deployments) and select the DC mysql-57-centos7. Deletes the mounted volume inside the container. And click Add Storage to Add the volume created by the signature.

Note: Since the default data directory for the default mysql-57-centos7 deployment configuration is mounted at /var/lib/mysql/data by default, the built-in volumes need to be removed to avoid path conflicts. OpenShift automatically triggers a redeployment of mysql-57-centos7 because of the configuration change.

Now you can log into the container and create some test data

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: 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 the set (0.00 SEC) mysql > use data; Database changed mysql> show tables; + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + | Tables_in_data | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + | users | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - + 1 row in the set (0.00 SEC) mysql > select * from users; +----+--------------+ | id | username | +----+--------------+ | 1 | admin | | 2 | system:admin | | 3 | developer | + - + -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 3 rows in the set (0.00 SEC) mysql >

Note: You can now redeploy this DC to see if any data is missing from the newly instantiated container MySQL, as follows: