Deploy Mysql in Docker and mount the configuration file

To deploy MySQL in Docker and mount the configuration file, you can use the following steps:

Create local directory

First, create a directory on the host to store the MySQL configuration file. For example, create a mysqldirectory named to store the MySQL configuration file.

mkdir mysql
mkdir mysql/conf
mkdir mysql/data

Pull the MySQL image

Use the following command to pull the latest image of MySQL from Docker Hub:

docker pull mysql

Start the MySQL container

Use the following command to start a MySQL container named mysql and mysqlmount the host's directory to the directory inside the container /etc/mysql/conf.d:

docker run --name mysql -v  /root/mysql/conf:/etc/mysql  -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d -p 3307:3307 mysql:5.7  --defaults-file=/etc/mysql/my.cnf

Among them, --namespecify the container name, mount -v /path/to/mysql:/etc/mysql/conf.dthe host's mysqldirectory to the directory in the container /etc/mysql/conf.d, mount -v /path/to/mysql/data:/var/lib/mysqlthe host's mysql/datadirectory to the directory in the container /var/lib/mysql, -e MYSQL_ROOT_PASSWORD=<password>specify the password of the MySQL root user, -dwhich means starting the container in background mode, and -p 3306:3306set the container's 3306 port Maps to port 3306 of the host.

Modify MySQL configuration file

mysqlCreate a file named in the host's directory my.cnfto modify the MySQL configuration. For example, here is a simple configuration file example:

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

In this example, we specify the MySQL character set and collation.

Restart the MySQL container

mysqlAfter modifying the file in the host directory my.cnf, you need to restart the MySQL container for the configuration to take effect. Restart the container using the following command:

docker restart mysql

Connect to MySQL

Connect to MySQL using the following command:

mysql -h 127.0.0.1 -P 3306 -u root -p

Among them, -hspecify the IP address of MySQL, -Pspecify the port of MySQL, and -uspecify the user name, -pwhich means you need to enter a password.

The above are the steps to deploy MySQL in Docker and mount the configuration file. It should be noted that the MySQL configuration file can be customized according to actual needs, and you can refer to the MySQL official documentation for configuration.

Guess you like

Origin blog.csdn.net/njpkhuan/article/details/131280951