Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In today's technological era, organizations have to release apps quickly to attract and retain business. This makes the teams build and configure the deployment environment at a faster pace with a lesser cost. However, containerization technologies come to the rescue for building a lightweight infrastructure.

This article will elucidate the method of deploying and connecting to a MySQL server in a container-based environment.

Now, let’s get into the nitty-gritty of it.

2. MySQL Container Deployment

Firstly, let’s look into the steps involved in the MySQL container deployment. Basically, MySQL follows the Client-Server architecture model. Here, the server is a container image with databases, whereas the client is used to access the database on the host machine.

We've divided the deployment workflow into three sections.

2.1. MySQL Server Deployment

Now, let’s bring the MySQL server instance into Docker. We can simply build a container based on the MySQL image pulled from the Docker Hub. There are two points we should consider when choosing which version of an image to pull from the Docker Hub:

  • Official Image Stamping – These are more secure and curated images from the MySQL developer team.
  • Latest Tag – Unless we have any reservations on the MySQL version, we can go with the latest version available in the repository.

Let's pull the official MySQL image from Docker Hub using the docker pull command:

$ docker pull mysql:latest
latest: Pulling from library/mysql
f003217c5aae: Pull complete
…
… output truncated …
…
70f46ebb971a: Pull complete
db6ea71d471d: Waiting
c2920c795b25: Downloading [=================================================> ]  105.6MB/107.8MB
26c3bdf75ff5: Download complete

Usually, the images are distinct layers tightly coupled in an ordered form as described in the manifest file. Our docker pull command will get the layers of the images from the blob store and automatically create the image using the manifest file:

…
… output truncated …
…
4607fa685ac6: Pull complete
Digest: sha256:1c75ba7716c6f73fc106dacedfdcf13f934ea8c161c8b3b3e4618bcd5fbcf195
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

The bundled image gets a hash code for future reference as demonstrated above.

Without further ado, let's run the container. The docker run command typically creates the writeable container layer on top of the image layers. We'll need to provide the container name using the -name argument and use the MySQL image with the latest tag. Further, we'll set the MySQL server password through the environment variable MYSQL_ROOT_PASSWORD. In our case, the password is set to “baeldung”.

Finally, the -d option helps us run the container as a daemon. The output throws another hash code for future container management:

$ docker run --name bael-mysql-demo -e MYSQL_ROOT_PASSWORD=baeldung -d mysql:latest
fedf880ce2b690f9205c7a37f32d75f669fdb1da2505e485e44cadd0b912bd35

We can see all running containers in a host through the ps command:

$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                 NAMES
fedf880ce2b6   mysql:latest   "docker-entrypoint.s…"   17 seconds ago   Up 16 seconds   3306/tcp, 33060/tcp   bael-mysql-demo

2.2. MySQL Client Installation

It's mandatory to install a client to get easy access to the MySQL server. Depending on our need, we can either install the client on the host machine or any other machine or container that has IP reachability with the server container:

$ sudo apt install mysql-client -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
mysql-client is already the newest version (5.7.37-0ubuntu0.18.04.1).
…
… output truncated …
…

Now, let's enable the extraction of the installation path and version of the MySQL client:

$ which mysql
/usr/bin/mysql
$ mysql --version
mysql  Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using  EditLine wrapper

2.3. Establish Communication

Next, let's log in to the server using the installed client. Traditionally, we use the MySQL command with username and password for server login. However, it will not work in the case of container-based solutions:

$ mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

We'll end up with socket errors as illustrated above. Here, it's important to understand that the MySQL server is a container and not simply installed on the host machine. As highlighted in the above section, containers are lightweight servers with their own compute resources, networking, and storage.

The inspect command helps to allocate an IP address to the MySQL server instance:

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' bael-mysql-demo
172.17.0.2

Let's provide the above IP address in the client's host option, with the default port number and protocol type as TCP:

$ mysql -h 172.17.0.2 -P 3306 --protocol=tcp -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
…
… output truncated …
…
mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye

Congrats, we have logged into the MySQL server successfully!

3. Conclusion

In summary, we've seen in detail the steps for deploying a MySQL server container, installing the MySQL client on the host machine, and finally, establishing the connection between them using container information.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!