Docker Method steps for installing MySQL8

  • 2020-12-05 17:28:53
  • OfStack

1. Download the image

docker Hub website URL: https: / / hub docker. com / _ / mysql /

Download latest version: docker pull mysql
Download the specified version: docker pull mysql:verison(8.0.11, 8.0, 8)

2. Start the mirror

docker run  -d --name mysql -v /data/datadir:/var/lib/mysql -v /etc/mysql/my.cnf:/etc/mysql/my.cnf -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:tag

No/data/datadir path or/etc/mysql/my cnf configuration file, you can use the default, does not need to mount

3. User authorization

1. Log in to the started MySQL container


docker exec -it mysql /bin/sh

2. Login MySQL using the password set by -ES49en MYSQL_ROOT_PASSWORD in the startup command


mysql -uroot -p

3. Execute the authorization command. Here, root user is taken as an example


create user root@'192.168.0.2' identified by '123456';
grant all privileges on databas_name.* to root@'192.168.0.2';
ALTER USER 'root'@'192.168.0.2' IDENTIFIED WITH mysql_native_password BY '123456';

4. Login to MySQL


mysql -h 192.168.0.2 -u root -p

4. Start the environment variable in the command

When you start the mysql image, you can adjust the configuration of the MySQL instance by passing one or more environment variables on the docker run command line. Note that if you start the container with a data directory that already contains the database, none of the following variables make any difference: any preexisting database remains unchanged when the container is started.

. See also https: / / dev mysql. com doc/refman / 5.7 / en environment - variables html MySQL itself to comply with in order to understand the environment variables of the document (especially MYSQL_HOST such variables, these variables in the image 1 use will cause problems).

MYSQL_ROOT_PASSWORD

This variable is mandatory and specifies the password to be set for the MySQL root superuser account. In the above example, it is set to ES98en-ES99en-ES100en.

MYSQL_DATABASE

This variable is optional and allows you to specify the name of the database that you want to create when the image starts. If a user/password is provided (see below), the user is granted superuser access to the database (corresponding to GRANT ALL).

MYSQL_USER, MYSQL_PASSWORD

These variables are optional and can be used to create a new user and set that user's password. This user will be granted superuser privileges (see above) specified by the MYSQL_DATABASE variable. Both variables are required to create the user.

Note that this mechanism is not required to create the root user of the superuser, who will be created by default using the password specified by the MYSQL_ROOT_PASSWORD variable.

MYSQL_ALLOW_EMPTY_PASSWORD

This is an optional variable. Setting to yes allows the container to start with the empty password of the root user. Note: yes do not set this variable to "not recommended" unless you really know what you are doing, as this will leave your instance of MySQL completely unprotected, allowing anyone to gain full superuser access.

MYSQL_RANDOM_ROOT_PASSWORD

This is an optional variable. Set yes to 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_PASSWORD

1 Once initialization is complete, set root user (not MYSQL_USER specified by the user) as expired, and force password change on first login. Note: This feature is only supported on MySQL 5.6+. Using this option on MySQL 5.5 will raise an error during initialization.


Related articles: