background

Using Docker container to build the database, can be very convenient to transplant to other environments. There are two configuration methods in this article. One is to keep all file data inside the container, and the other is to mount external configuration and data. The advantage of this method is that the packaged container will not grow larger with use.

Mount configuration files and data inside the container

Docker pulls latest MySQL

Docker pull mysql docker images // Check whether there are imagesCopy the code

Start docker and create MySQL

docker run --name=mysql -it -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
Copy the code

Setting up Remote Access

use mysql
ALTER USER 'root'@The '%' IDENTIFIED WITH mysql_native_password BY 'password';
flush privileges;
Copy the code

Mount external configuration files and data

Docker pulls latest MySQL

Docker pull mysql: 8.0.18Copy the code

Creating a Configuration File

You can set it to the appropriate directory as required

mkdir -p /usr/local/mysql/conf
mkdir -p /usr/local/mysql/data
mkdir -p /usr/local/mysql/logs
Copy the code

Create the MySQL configuration file

vi /usr/local/mysql/conf/my.cnf
Copy the code

Copy the following content to solve the Chinese garble problem

 
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server=utf8 
[client]
default-character-set=utf8 
[mysql]
default-character-set=utf8 
# Custom config should go here! includedir /etc/mysql/conf.d/Copy the code

Run the container

docker run --restart=always -d -v /usr/local/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql/logs:/logs -v / usr/local/mysql/data/mysql/var/lib/mysql - p 3306:3306-123456 name mysql - e MYSQL_ROOT_PASSWORD = mysql: 8.0.18Copy the code
-d Background running -v directory mapping; Host directory: container directory -p port mapping; Host port: container port -name Container name -e Run The commandCopy the code

Setting up Remote Access

use mysql
ALTER USER 'root'@The '%' IDENTIFIED WITH mysql_native_password BY 'password';
flush privileges;
Copy the code

Refer to the article