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.
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:
- id: mysql
# 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.
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.
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.
Read more about the MySQL Docker image on Docker Hub.