Technology sharing | installing MySQL with docker

Time:2020-12-2

Author: Peter Zaitsev
Translator: Guan Changlong
Original text: https://www.percona.com/blog/…

At work, I often need to install a specific version of MySQL, MariaDB or percona to run some experiments, such as checking version differences or providing test instructions. This blog series explains how to use docker to install mysql, MariaDB, or percona. This article is the first, focusing on MySQL.

The advantage of docker is that it is very easy to install the latest version of MySQL and any other version, but it often does not match a typical production installation. Docker is really convenient when you need a simple single instance. If you are looking at some replication related behavior, it may not be appropriate.

These instructions are in the case of running test cases quickly and easily. Not applicable for production deployment. The following assumes that docker is installed.

First, you should know that there are only two “official” MySQL docker repositories. One of them is maintained by the docker team through a simple docker command mysql:latest It works. The other is maintained by Oracle’s MySQL team

docker run mysql / mysql-server:latest

Note: the latest in the syntax is the tag value, indicating the latest version in the default installation library

In the following example, we will use the docker image of the MySQL team, although the docker team works in a similar way.

Using docker to install the latest version of MySQL

docker run --name mysql-latest  \
-p 3306:3306 -p 33060:33060  \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword'   \
-d mysql/mysql-server:latest

This will launch the latest version of MySQL instance, which can be accessed remotely from anywhere using the specified root password. It’s easy to test, but it’s not a good security habit (that’s why it’s not the default).

Connect to the MySQL server docker container

Using docker installation means that you cannot get any tools, utilities, or libraries directly on the host, so you can install them separately, access the created instance from a remote host, or use the command line that comes with the docker image.

Start the MySQL command line client through docker

docker exec -it mysql-latest mysql -uroot -pstrongpassword

Use docker to start MySQL shell:

docker exec -it mysql-latest mysqlsh -uroot -pstrongpassword

Managing MySQL server in docker container

When you want to stop the MySQL server docker container from running:

docker stop mysql-latest

If you want to restart a stopped MySQL docker container, you should not attempt to restart it using docker run. Instead, you should use:

docker start mysql-latest

If an error occurs, for example, if the container is not started, you can access its log using the following command:

docker logs mysql-latest

If you want to recreate a new docker container from scratch, you can run:

docker stop mysql-latest
docker rm mysql-latest

And then do it again docker runCommand.

Pass command line options to the MySQL server in the docker container

If you want to pass some command line options to MySQL server, you can do this in the following ways:

docker run --name mysql-latest  \
-p 3306:3306 -p 33060:33060  \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword'   \
-d mysql/mysql-server:latest \
--innodb_buffer_pool_size=256M \
--innodb_flush_method=O_DIRECT \

Running the specified version of MySQL server in docker

If you want to run a certain version of MySQL in the docker container, this is simple. You can use docker image tag to select the version you want and change the name to a different name to avoid name conflicts:

docker run --name mysql-8.0.17  \
-p 3306:3306 -p 33060:33060  \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword'   \
-d mysql/mysql-server:8.0.17

This will start MySQL 8.0.17 in the docker container.

docker run --name mysql-5.7  \
-p 3306:3306 -p 33060:33060  \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword'   \
-d mysql/mysql-server:5.7

This will launch the latest MySQL 5.7 in docker.

Running multiple versions of MySQL server in docker

At the same time, when running multiple versions of MySQL in docker, the potential problem is TCP port conflict. If you do not access the docker container from outside and only run programs contained in the same container, you can remove the port mapping (- P option), and then you can run multiple containers:

docker run --name mysql-latest  \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword'   \
-d mysql/mysql-server:latest
 
docker run --name mysql-8.0.17  \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword'   \
-d mysql/mysql-server:8.0.17

In more common cases, when you need to access the docker container from outside, you will need to map it to use a different external port name. For example, to start the latest MySQL 8 on ports 3306 / 33060 and MySQL 8.0.17 at 3307 / 33070, we can use:

docker run --name mysql-latest  \
-p 3306:3306 -p 33060:33060  \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword'   \
-d mysql/mysql-server:latest
  
docker run --name mysql-8.0.17  \
-p 3307:3306 -p 33070:33060  \
-e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword'   \
-d mysql/mysql-server:8.0.17

If you want to use MySQL for more complicated things on docker, there are many things to consider.

For more information, please refer to:
https://hub.docker.com/r/mysq…
https://dev.mysql.com/doc/ref…