1. Introduction

In this tutorial, we’ll learn how to determine when a Docker MySQL container is ready to handle queries. First, we’ll create a docker container with MySQL. Next, we’ll examine a method based on running the mysqladmin command from outside the container. Finally, we’ll learn a method based on executing a mysql command inside the container.

2. Create a Docker MySQL Container

We can create a MySQL container using the following command:

$ docker run --name <container-name> -e MYSQL_ROOT_PASSWORD=<secret-password> -d mysql:<tag>

In the above command, we need to replace the parameter with the name we want to assign to the container. In addition, we need to replace with the password to be set for the MySQL root user and with the MySQL version we want. We can find all the available versions in the official documentation for the MySQL docker image.

The following command creates a docker container named my_db_server with the latest MySQL version:

$ docker run --name my_db_server -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest

However, after the container is created, it’ll take a while before the MySQL service is actually ready to handle queries. Next, we’ll look at two different methods for checking MySQL’s readiness.

3. Ping MySQL Using the mysqladmin Tool

We can perform a connectivity test to check whether MySQL is ready to handle queries. A simple way to do this is using the mysqladmin ping command.

First, we need to install the mysql-client package:

$ sudo apt install mysql-client

Next, we can test connectivity with MySQL:

$ mysqladmin ping -h"<container-ip-address>" --user=root --password=<secret-password>

The above command checks MySQL readiness only once. Therefore, we need to repeat it until MySQL is actually ready. An efficient way to do this is through a loop that repeats the ping command until it gets a successful response.

The following shell script performs exactly this task in an automated fashion:

#!/bin/bash
DB_HOST=$(docker inspect   -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container-name>)
until mysqladmin ping -h"<container-ip-address>" --user=root --password=<secret-password> --silent
do
  echo "Waiting for a 'ping reply'..."
  # Wait for two seconds before ping again
  sleep 2
done
echo "MySQL is up!"

First, the script obtains the IP address of the MySQL container via the docker inspect command. Next, it performs a loop repeating the mysqladmin ping command every 2 seconds until it gets a response from the MySQL service running inside the container. Once this happens, the script prints the message MySQL is up!.

We’ll get the following output when running the script:

Waiting for a 'ping reply'...
Waiting for a 'ping reply'...
Waiting for a 'ping reply'...
MySQL is up!

4. Checking for MySQL Status via Docker Command

Another efficient way to check for the MySQL service is through the mysql command line tool. In this case, we need to run the mysql command inside the container. A very straightforward way to do this is through the docker exec command.

The command below is an example of how to do this. We need to remember to replace the and parameters with the values used when creating the container.

$ docker exec <container-name> mysql --user=root --password=<secret-password>

Since the above command only checks MySQL’s readiness once, we need to create a loop to run it until the MySQL service is actually active. Using the same idea as the script used in the previous method, the following script periodically checks MySQL until it’s ready:

#!/bin/bash
until docker exec <container-name> mysql --user=root --password=<secret-password> -e "status" &> /dev/null
do
  echo "Waiting for database connection..."
  # Wait for two seconds before checking again
  sleep 2
done
echo "MySQL is up!"

When we run the script, we’ll get an output similar to the following:

Waiting for database connection...
Waiting for database connection...
Waiting for database connection...
MySQL is up!

5. Conclusion

In this article, we’ve learned two methods for determining when a Docker MySQL container is ready to handle queries.

The first requires installing the mysql-client package, while the second uses the docker exec command to run the check inside the container.