MySQL

MySQL is a widely used open source relational database management system. By using a MySQL container, your build process can run against an actual database, allowing for more complex and accurate testing.

Setup

The MySQL service uses the default MySQL Docker image. While this image supports a handful of configuration options, the one requirement is MYSQL_ROOT_PASSWORD. As the name implies, this environment variable defines the password for the root MySQL account, which you can then use to access your database from within your application:

build:
 services:
   - id: mysql
     env:
       MYSQL_ROOT_PASSWORD: password
 box:
   id: php
 steps:
   # build the project

Beyond MYSQL_ROOT_PASSWORD, there are a few other useful optional environment variables that can be passed as well:

  • MYSQL_DATABASE - This variable specifies the name of the database to be created on startup.
  • MYSQL_USER, MYSQL_PASSWORD - These variables create a new user with the defined username and password (both variables are required if passed). If MYSQL_DATABASE is also defined, then the created user will be granted superuser access to the created database.
  • MYSQL_ALLOW_EMPTY_PASSWORD - Setting this variable to yes will allow the MySQL service to be started with no root password.

Environment Variables

Once booted, several environment variables will be made available during the build process. The naming convention behind these variables can be found on the Linking Services page.

MYSQL_ENV_MYSQL_ROOT_PASSWORD=password
MYSQL_NAME=/wercker-pipeline-023ebb1f-5571-44d4-97ba-cb7b68e91274/mysql
MYSQL_PORT=tcp://172.XX.X.XX:3306
MYSQL_PORT_3306_TCP=tcp://172.X.X.X:3306
MYSQL_PORT_3306_TCP_ADDR=172.X.X.X
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP_PROTO=tcp

Known Issues

As outlined in the MariaDB documentation, MySQL and MariaDB Docker images occasionally have issues starting the database in time. You can read more about this issue in Docker’s official documentation. A common strategy for addressing it is to add a small delay in your build process in order to ensure that MySQL is up and running.

Further Reading

Read more about the MySQL Docker image on Docker Hub.