MySQL

    Versions

    Currently, OKD provides versions and 5.7 of MySQL.

    This image comes in two flavors, depending on your needs:

    • RHEL 7

    • CentOS 7

    RHEL 7 Based Images

    The RHEL 7 image is available through the Red Hat Registry:

    CentOS 7 Based Images

    CentOS images for MySQL 5.6 and 5.7 are available on Docker Hub:

    1. $ docker pull centos/mysql-57-centos7

    To use these images, you can either access them directly from these registries or push them into your OKD container image registry. Additionally, you can create an ImageStream that points to the image, either in your container image registry or at the external location. Your OKD resources can then reference the ImageStream. You can find ImageStream definitions for all the provided OKD images.

    Configuration and Usage

    The first time you use the shared volume, the database is created along with the database administrator user and the MySQL root user (if you specify the **MYSQL_ROOT_PASSWORD** environment variable). Afterwards, the MySQL daemon starts up. If you are re-attaching the volume to another container, then the database, database user, and the administrator user are not created, and the MySQL daemon starts.

    The following command creates a new database with MySQL running in a container:

    1. $ oc new-app \
    2. -e MYSQL_USER=<username> \
    3. -e MYSQL_PASSWORD=<password> \
    4. -e MYSQL_DATABASE=<database_name> \
    5. openshift/mysql-56-centos7

    Running MySQL Commands in Containers

    OKD uses (SCLs) to install and launch MySQL. If you want to execute a MySQL command inside of a running container (for debugging), you must invoke it using bash.

    To do so, first identify the name of the pod. For example, you can view the list of pods in your current project:

    1. $ oc get pods

    Then, open a remote shell session to the pod:

    1. $ oc rsh <pod>

    When you enter the container, the required SCL is automatically enabled.

    You can now run the mysql command from the bash shell to start a MySQL interactive session and perform normal MySQL operations. For example, to authenticate as the database user:

    1. bash-4.2$ mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -h $HOSTNAME $MYSQL_DATABASE
    2. Welcome to the MySQL monitor. Commands end with ; or \g.
    3. Your MySQL connection id is 4
    4. Server version: 5.6.37 MySQL Community Server (GPL)
    5. ...
    6. mysql>

    When you are finished, enter quit or exit to leave the MySQL session.

    Environment Variables

    The MySQL user name, password, and database name must be configured with the following environment variables:

    You must specify the user name, password, and database name. If you do not specify all three, the pod will fail to start and OKD will continuously try to restart it.

    MySQL settings can be configured with the following environment variables:

    Some of the memory-related parameters have two default values. The fixed value is used when a container does not have assigned. The other value is calculated dynamically during a container’s startup based on available memory.

    The MySQL image can be run with mounted volumes to enable persistent storage for the database:

    • /var/lib/mysql/data - This is the data directory where MySQL stores database files.

    Changing Passwords

    Passwords are part of the image configuration, therefore the only supported method to change passwords for the database user (**MYSQL_USER**) and root user is by changing the environment variables **MYSQL_PASSWORD** and **MYSQL_ROOT_PASSWORD**, respectively.

    You can view the current passwords by viewing the pod or deployment configuration in the web console or by listing the environment variables with the CLI:

    Whenever **MYSQL_ROOT_PASSWORD** is set, it enables remote access for the root user with the given password, and whenever it is unset, remote access for the root user is disabled. This does not affect the regular user **MYSQL_USER**, who always has remote access. This also does not affect local access by the root user, who can always log in without a password in localhost.

    Changing database passwords through SQL statements or any way other than through the environment variables aforementioned causes a mismatch between the values stored in the variables and the actual passwords. Whenever a database container starts, it resets the passwords to the values stored in the environment variables.

    To change these passwords, update one or both of the desired environment variables for the related deployment configuration(s) using the oc set env command. If multiple deployment configurations utilize these environment variables, for example in the case of an application created from a template, you must update the variables on each deployment configuration so that the passwords are in sync everywhere. This can be done all in the same command:

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

    Depending on your application, there may be other environment variables for passwords in other parts of the application that should also be updated to match. For example, there could be a more generic DATABASE_USER variable in a front-end pod that should match the database user’s password. Ensure that passwords are in sync for all required environment variables per your application, otherwise your pods may fail to redeploy when triggered.

    Updating the environment variables triggers the redeployment of the database server if you have a . Otherwise, you must manually start a new deployment in order to apply the password changes.

    To verify that new passwords are in effect, first open a remote shell session to the running MySQL pod:

    1. $ oc rsh <pod>

    From the bash shell, verify the database user’s new password:

    1. bash-4.2$ mysql -u $MYSQL_USER -p<new_password> -h $HOSTNAME $MYSQL_DATABASE -te "SELECT * FROM (SELECT database()) db CROSS JOIN (SELECT user()) u"

    If the password was changed correctly, you should see a table like this:

    1. +------------+---------------------+
    2. | database() | user() |
    3. +------------+---------------------+
    4. | sampledb | user0PG@172.17.42.1 |
    5. +------------+---------------------+

    To verify the root user’s new password:

    1. bash-4.2$ mysql -u root -p<new_root_password> -h $HOSTNAME $MYSQL_DATABASE -te "SELECT * FROM (SELECT database()) db CROSS JOIN (SELECT user()) u"

    If the password was changed correctly, you should see a table like this:

    The MySQL templates should have been registered in the default openshift project by your cluster administrator during the initial cluster setup. See Loading the Default Image Streams and Templates for more details, if required.

    There are two templates available:

    • mysql-ephemeral is for development or testing purposes only because it uses ephemeral storage for the database content. This means that if the database pod is restarted for any reason, such as the pod being moved to another node or the deployment configuration being updated and triggering a redeploy, all data will be lost.

    • mysql-persistent uses a persistent volume store for the database data which means the data will survive a pod restart. Using persistent volumes requires a persistent volume pool be defined in the OKD deployment. Cluster administrator instructions for setting up the pool are located in .

    You can instantiate templates by following these instructions.

    Once you have instantiated the service, you can copy the user name, password, and database name environment variables into a deployment configuration for another component that intends to access the database. That component can then access the database via the service that was defined.

    Using MySQL Replication

    Red Hat provides a proof-of-concept template for MySQL master-slave replication (clustering); you can obtain the .

    To upload the example template into the current project’s template library:

    1. $ oc create -f \
    2. https://raw.githubusercontent.com/sclorg/mysql-container/master/examples/replica/mysql_replica.json

    The following sections detail the objects defined in the example template and describe how they work together to start a cluster of MySQL servers implementing master-slave replication. This is the recommended replication strategy for MySQL.

    Creating the Deployment Configuration for the MySQL Master

    To set up MySQL replication, a is defined in the example template that defines a replication controller. For MySQL master-slave replication, two deployment configurations are needed. One deployment configuration defines the MySQL master server and second the MySQL slave servers.

    To tell a MySQL server to act as the master, the **command** field in the container’s definition in the deployment configuration must be set to run-mysqld-master. This script acts as an alternative entrypoint for the MySQL image and configures the MySQL server to run as the master in replication.

    MySQL replication requires a special user that relays data between the master and slaves. The following environment variables are defined in the template for this purpose:

    Variable NameDescriptionDefault

    MYSQL_MASTER_USER

    The user name of the replication user

    master

    MYSQL_MASTER_PASSWORD

    The password for the replication user

    generated

    Example 1. MySQL Master Deployment Configuration Object Definition in the Example Template

    1. apiVersion: "v1"
    2. metadata:
    3. name: "mysql-master"
    4. spec:
    5. type: "Recreate"
    6. triggers:
    7. - type: "ConfigChange"
    8. replicas: 1
    9. selector:
    10. name: "mysql-master"
    11. template:
    12. metadata:
    13. labels:
    14. name: "mysql-master"
    15. spec:
    16. volumes:
    17. - name: "mysql-master-data"
    18. persistentVolumeClaim:
    19. claimName: "mysql-master"
    20. containers:
    21. - name: "server"
    22. image: "openshift/mysql-56-centos7"
    23. command:
    24. - "run-mysqld-master"
    25. ports:
    26. - containerPort: 3306
    27. protocol: "TCP"
    28. env:
    29. - name: "MYSQL_MASTER_USER"
    30. value: "${MYSQL_MASTER_USER}"
    31. - name: "MYSQL_MASTER_PASSWORD"
    32. value: "${MYSQL_MASTER_PASSWORD}"
    33. - name: "MYSQL_USER"
    34. value: "${MYSQL_USER}"
    35. - name: "MYSQL_PASSWORD"
    36. value: "${MYSQL_PASSWORD}"
    37. - name: "MYSQL_DATABASE"
    38. value: "${MYSQL_DATABASE}"
    39. - name: "MYSQL_ROOT_PASSWORD"
    40. value: "${MYSQL_ROOT_PASSWORD}"
    41. volumeMounts:
    42. - name: "mysql-master-data"
    43. mountPath: "/var/lib/mysql/data"
    44. resources: {}
    45. terminationMessagePath: "/dev/termination-log"
    46. imagePullPolicy: "IfNotPresent"
    47. securityContext:
    48. capabilities: {}
    49. privileged: false
    50. restartPolicy: "Always"
    51. dnsPolicy: "ClusterFirst"

    Since we claimed a persistent volume in this deployment configuration to have all data persisted for the MySQL master server, you must ask your cluster administrator to create a persistent volume that you can claim the storage from.

    After the deployment configuration is created and the pod with MySQL master server is started, it will create the database defined by **MYSQL_DATABASE** and configure the server to replicate this database to slaves.

    The example provided defines only one replica of the MySQL master server. This causes OKD to start only one instance of the server. Multiple instances (multi-master) is not supported and therefore you can not scale this replication controller.

    To replicate the database created by the , a deployment configuration is defined in the template. This deployment configuration creates a replication controller that launches the MySQL image with the **command** field set to run-mysqld-slave. This alternative entrypoints skips the initialization of the database and configures the MySQL server to connect to the mysql-master service, which is also defined in example template.

    Example 2. MySQL Slave Deployment Configuration Object Definition in the Example Template

    1. kind: "DeploymentConfig"
    2. apiVersion: "v1"
    3. metadata:
    4. name: "mysql-slave"
    5. spec:
    6. strategy:
    7. type: "Recreate"
    8. - type: "ConfigChange"
    9. replicas: 1
    10. selector:
    11. name: "mysql-slave"
    12. template:
    13. metadata:
    14. labels:
    15. name: "mysql-slave"
    16. spec:
    17. containers:
    18. - name: "server"
    19. image: "openshift/mysql-56-centos7"
    20. command:
    21. - "run-mysqld-slave"
    22. ports:
    23. - containerPort: 3306
    24. protocol: "TCP"
    25. env:
    26. - name: "MYSQL_MASTER_USER"
    27. value: "${MYSQL_MASTER_USER}"
    28. - name: "MYSQL_MASTER_PASSWORD"
    29. value: "${MYSQL_MASTER_PASSWORD}"
    30. - name: "MYSQL_DATABASE"
    31. value: "${MYSQL_DATABASE}"
    32. resources: {}
    33. terminationMessagePath: "/dev/termination-log"
    34. imagePullPolicy: "IfNotPresent"
    35. securityContext:
    36. capabilities: {}
    37. privileged: false
    38. restartPolicy: "Always"
    39. dnsPolicy: "ClusterFirst"

    This example deployment configuration starts the replication controller with the initial number of replicas set to 1. You can scale this replication controller in both directions, up to the resources capacity of your account.

    If either the master or any of the slaves goes down, OKD will bring them back up. The master will reuse the persistent volume, while any restarted slaves will replicate data from the master.

    The pods created by the MySQL slave replication controller must reach the MySQL master server in order to register for replication. The example template defines a headless service named mysql-master for this purpose. This service is not used only for replication, but the clients can also send the queries to mysql-master:3306 as the MySQL host.

    To have a headless service, the **clusterIP** parameter in the service definition is set to None. Then you can use a DNS query to get a list of the pod IP addresses that represents the current endpoints for this service.

    Example 3. Headless Service Object Definition in the Example Template

    1. kind: "Service"
    2. apiVersion: "v1"
    3. metadata:
    4. name: "mysql-master"
    5. labels:
    6. name: "mysql-master"
    7. spec:
    8. ports:
    9. - protocol: "TCP"
    10. port: 3306
    11. targetPort: 3306
    12. nodePort: 0
    13. selector:
    14. name: "mysql-master"
    15. clusterIP: "None"
    16. type: "ClusterIP"
    17. sessionAffinity: "None"
    18. status:
    19. loadBalancer: {}

    Scaling the MySQL Slaves

    To increase the number of members in the cluster:

    1. $ oc scale rc mysql-slave-1 --replicas=<number>

    This tells to create a new MySQL slave pod. When a new slave is created, the slave entrypoint first attempts to contact the mysql-master service and register itself to the replication set. Once that is done, the MySQL master server sends the slave the replicated database.

    When scaling down, the MySQL slave is shut down and, because the slave does not have any persistent storage defined, all data on the slave is lost. The MySQL master server then discovers that the slave is not reachable anymore, and it automatically removes it from the replication.

    This section describes some troubles you might encounter and presents possible resolutions.

    Linux Native AIO Failure

    Symptom

    The MySQL container fails to start and the logs show something like:

    Explanation

    MySQL’s storage engine was unable to use the kernel’s AIO (Asynchronous I/O) facilities due to resource limits.

    Resolution

    Turn off AIO usage entirely by setting environment variable **MYSQL_AIO** to have value 0. On subsequent deployments, this arranges for the MySQL configuration variable **innodb_use_native_aio** to have value 0.

    Alternatively, increase the aio-max-nr kernel resource. The following example examines the current value of aio-max-nr and doubles it.

    1. $ sysctl fs.aio-max-nr
    2. fs.aio-max-nr = 1048576

    This is a per-node resolution and lasts until the next node reboot.