MySQL in Docker

Marek Vondra
5 min readApr 20, 2022

--

Sometimes you need to use a program within your development work. Always you are not forced to install. Since Docker is an as common equipment in our computer, you are able to run this program without complication. Today I would like to describe how you can start MySql.

First of all, you are sure that Docker was installed. Open terminal or command line (depending on the operation system) and check the version with the commanddocker version. You can see something as:

picture 1: docker version

1- Download the images
Next what it is necessary to do is download the images of MySql.

docker pull mysql/mysql-server

with this command, images of MySql were downloaded with the tag latest. If you need to use any other version please go to the link and search for the version which you required. After you choose the correct tag, you can extend command with the version:

docker pull mysql/mysql-server:8.0.28

If the pull was successful you can see something:

picture 2: completed pull image of MySQL

Just to be sure, you can check the list of images with commands docker images . For our next work, it is very important the image id or name of the repository (see picture 3).

picture 3: all images

2- Start instance of MySQL

Now if your images completed is, you start the instance. To start, you have to execute the following command, which consists of

  • docker run the command for starting the docker container from the image
  • --name='my_sql_container' after the argument, is defined the name of the container, without this argument name will be generated automatically
  • -d a very important argument, which ensures, that the container in detached mode will be running
  • -p 3306:3306 mapping port from a docker container to host port. Because MySQL is running on port 3306 I mapped exactly this port outside of container.
  • mysql/mysql-server name of our images — see section 1

The completed command is:

docker run --name='my_sql_container' -d -p 3306:3306 mysql/mysql-server

If everything is ok, you have to see the following message in your cmd or terminal. This string is ID of the container, which is running in your docker instance.

picture 4: ID of docker container

You can also check your running container using the docker command.

docker ps
picture 5: check running docker container

In the list you see all the important information in one place. First of all, there is the shorter container ID, which we can see in picture 4. Follow with name of images (mysql/mysql-server). Very important for the future work is, that the port is mapped (0.0.0.3306 -> 3306/tcp ). It says, that port 3306 in a container is mapped to localhost:3306. Of course, it could be mapped in your host compute to of one’s own choice but it is good practice to use the same number.

3- Start instance of MySQL

Now your database is already running. Last what we to do is set up the DB. First of all, we have to check the log because we need to get the initial password. Execute the command:

docker logs my_sql_container

picture 6: initial password for root user

If you try to connect with some tools (for example DBeaver) it is not working. The reason is, that the root user has no corresponding permission. You muss grant the permission. Due to start the command line in the container.

docker exec -it my_sql_container bash

  • docker execexecution docker command
  • -it allow tty and keep stdin
  • my_sql_container name of running container
  • bash because we want to start bash

Go to the location of MySQL in the container cd /var/lib/mysql and connect to the database with mysql -u root -p. Enter the initial password. For the next work, it is also necessary to restart default password ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword'; of course, you can choose any password.

Now we are ready to check the list of users:

use mysql;
select user from user;

you have to see some similar list:

picture 7: list of users

4- Create a user for connection

In the last step, you have to create a user for the connection. I have chosen the name dbeaver with the same password but you are fully free with choice.

CREATE USER 'dbeaver'@'%' IDENTIFIED BY 'dbeaver';
GRANT ALL PRIVILEGES ON *.* TO 'dbeaver'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Finally, you are ready to connect with some database client. I have chosen for DBeaver, because I have used it for a time. In the next picture, you can see the configuration. It is not so complicated :)

picture 8: settings for DB-Connection

That is all my friends. It is really not so difficult and I hope, that it helps you to work more with docker. Of course, I am so happy for your reaction and feedback.

--

--