MySQL is the world’s most popular open source database. With its reliability, ease of use and performance, MySQL has become the database of choice for web applications.

1. Check the available MySQL versions

mirror of mysql

Alternatively, you can search for available versions by docker search mysql.

2. Pull the mysql image

1
2
3
4
5
6
docker pull mysql:latest

或者拉取其他版本
docker pull mysql:8.0.27
docker pull mysql:5.7.36
docker pull mysql:5.6.51

3. Running mysql

Starting a MySQL instance is simple

1
docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

Parameter description.

  • -p 3306:3306 : Maps the 3306 port of the container service to the 3306 port of the host, so that external hosts can access the MySQL service directly via host ip:3306.
  • MYSQL_ROOT_PASSWORD=123456 : Set the password for the MySQL service root user.

Start mysql via yml file

The following is stack.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Use root/example as user/password credentials
version: '3.1'

services:
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Run docker stack deploy -c stack.yml mysql (or docker-compose -f stack.yml up), wait for it to fully initialise, then access http://swarm-ip:8080, http://localhost:8080, or http://host-ip:8080 (as appropriate).

4. Container shell access and viewing MySQL logs

The exec command allows access to the mysql container to execute commands

1
docker exec -it some-mysql bash

Get container logs via logs

1
docker logs some-mysql

5. Using a custom MySQL configuration file

The default configuration for MySQL can be found in /etc/mysql/my.cnf.

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this

1
docker run -itd --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

To change the default encoding and sorting rules for all tables to use UTF-8 ( utf8mb4 ), simply run the following command.

1
docker run -itd --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

View the full list of support

1
docker run -it --rm mysql:tag --verbose --help

6. Environment variables

parameters must description
MYSQL_ROOT_PASSWORD Must The password set as the MySQLroot superuser account.
MYSQL_DATABASE Optional Specifies the name of the database created at startup, and if a user/password is provided (see below), that user will be granted superuser access to that database
MYSQL_USER MYSQL_PASSWORD Optional Creates a new user and sets the password for that user, who will be granted superuser access to the database specified by the MYSQL_DATABASE variable. These two variables are required to create a user. Not using this to create the root superuser will by default use MYSQL_ROOT_PASSWORD.
MYSQL_ALLOW_EMPTY_PASSWORD Optional default no, set to allow root to log in with an empty password if yes.
MYSQL_RANDOM_ROOT_PASSWORD optional default no, set if yes to generate a random password for root, the generated password is printed to stdout( GENERATED ROOT PASSWORD: …..) .
MYSQL_ONETIME_PASSWORD Optional On completion of initialization, sets the root (not the user specified in MYSQL_USER!) user to expire, forcing the password to be changed on first login. Any non-null value will activate this setting. Note: This feature is only supported on MySQL 5.6+. Using this option on MySQL 5.5 will raise an appropriate error during initialization.
MYSQL_INITDB_SKIP_TZINFO Optional By default, the entry point script will automatically load the time zone data required by the CONVERT_TZ() function. If not required, any non-null value will disable time zone loading

7. Password file transfer

For example, a loaded password from the /run/secrets/<secret_name> file. For example.

1
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-root -d mysql:tag

Currently, this only supports MYSQL_ROOT_PASSWORD, MYSQL_ROOT_HOST, MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD.

8. Data storage

The preferred option here is to create a directory on the host (outside the container) and mount it to the container data directory

For example

  1. create a data directory on a suitable volume on the host system, e.g. /my/own/datadir.
  2. mysql Start your container like this.
1
docker run -it -d --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

For used data directories, you can omit $MYSQL_ROOT_PASSWORD

9. Create database dumps

The following data is dumped to a file via exec

1
docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql

10. Recovering data from data files

For recovering data. You can use the command docker exec with the -i flag, similar to the following.

1
docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql