Docker: docker installs MySQL and synchronizes data to the local machine

Posted by tomhoad on Mon, 10 Jan 2022 08:06:42 +0100

First understand MySQL startup mode

MySQL startup needs to set the password, so go to the official to check the startup command.
MySQL dockerhub address: https://registry.hub.docker.com/_/mysql

# Pipe sail test run command
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

# Parameter analysis
-e Represents the configuration environment;
MYSQL_ROOT_PASSWORD This variable is mandatory and will be specified as MySQL Password set for superuser account

Pull MySQL image

I specify here to pull the MySQL version of 5.7

# MySQL image, TAG[5.7]
[root@localhost /]# docker pull mysql:5.7

MySQL data persistence to local

MySQL data persistence problem. The data cannot only exist in the container. It is unsafe and needs to be synchronized locally.

The first time you start, set MYSQL_ROOT_PASSWORD

docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v/home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

Parameter analysis
-d background operation
-p port mapping
-v volume mount
-e environment configuration

View directory mount

Mount information

"Mounts": [
            {
                "Type": "bind",
                "Source": "/home/mysql/conf",
                "Destination": "/etc/mysql/conf.d",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/home/mysql/data",
                "Destination": "/var/lib/mysql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],

Native directory view

[root@localhost /]# docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v/home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
54d46f730f41d9aacb1c79e34cf8fbaa12b5fb7f2cb9b1d95a9632c8907a9b16
[root@localhost /]# cd /home/mysql/data
[root@localhost data]# ll
total 188484
-rw-r-----. 1 polkitd input       56 Jan  6 03:14 auto.cnf
-rw-------. 1 polkitd input     1680 Jan  6 03:14 ca-key.pem
-rw-r--r--. 1 polkitd input     1112 Jan  6 03:14 ca.pem
-rw-r--r--. 1 polkitd input     1112 Jan  6 03:14 client-cert.pem
-rw-------. 1 polkitd input     1680 Jan  6 03:14 client-key.pem
-rw-r-----. 1 polkitd input     1352 Jan  6 03:14 ib_buffer_pool
-rw-r-----. 1 polkitd input 79691776 Jan  6 03:33 ibdata1
-rw-r-----. 1 polkitd input 50331648 Jan  6 03:33 ib_logfile0
-rw-r-----. 1 polkitd input 50331648 Jan  6 03:14 ib_logfile1
-rw-r-----. 1 polkitd input 12582912 Jan  6 03:33 ibtmp1
drwxr-x---. 2 polkitd input     4096 Jan  6 03:14 mysql
drwxr-x---. 2 polkitd input     8192 Jan  6 03:14 performance_schema
-rw-------. 1 polkitd input     1676 Jan  6 03:14 private_key.pem
-rw-r--r--. 1 polkitd input      452 Jan  6 03:14 public_key.pem
-rw-r--r--. 1 polkitd input     1112 Jan  6 03:14 server-cert.pem
-rw-------. 1 polkitd input     1680 Jan  6 03:14 server-key.pem
drwxr-x---. 2 polkitd input     8192 Jan  6 03:14 sys
[root@localhost data]#

Data persistence

# Force deletion of running containers
docker rm rf 1d2db7256fa2

Even if the container is deleted, the local data still exists.

MySQL Environment Variables: Environment Variables

Variable namemeaning
MYSQL_ROOT_PASSWORDThis variable is mandatory and specifies the password that will be set for the MySQL superuser account
MYSQL_DATABASEThis variable is optional and allows you to specify the name of the database to be created at image boot time. If a user / password is provided (see below), the user will be granted superuser access to this database (corresponding to granting ALL).
MYSQL_USER,MYSQL_PASSWORDThese variables are optional and are used to create a new user and set the user's password. For MySQL_ The database specified by the database variable will grant the user superuser permission (see above). These two variables are required to create a user. Note that it is not necessary to use this mechanism to create a super user. By default, the user will use mysql_ ROOT_ Create the user with the password specified by the password variable.
MYSQL_ALLOW_EMPTY_PASSWORDThis is an optional variable. Set to a non null value, such as "YES", to allow the container to be started with the blank password of the root user. Note: it is not recommended to set this variable to YES unless you really know what you are doing, because this will make the MySQL instance completely unprotected and allow anyone to obtain full super user access.
MYSQL_RANDOM_ROOT_PASSWORDThis is an optional variable. Set to a non null value, for example, generate a random initial password for the root user (using pwgen). The generated root password will be printed to stdout (generated root password:.).
MYSQL_ONETIME_PASSWORDSet root (not the user specified in MySQL_USER!) The user expires upon completion, forcing the user to change the password on the first login. Any non null value will activate this setting. Note: MySQL 5.6 + only supports this feature. On mysql5 Using this option on 5 will throw an appropriate error during initialization.
MYSQL_INITDB_SKIP_TZINFOBy default, entry point scripts automatically load transformations_ The time zone data required by the tz() function. If not required, any non null value disables time zone loading.

Topics: Database MySQL Docker