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 mysql
directory 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 mysql
mount 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, --name
specify the container name, mount -v /path/to/mysql:/etc/mysql/conf.d
the host's mysql
directory to the directory in the container /etc/mysql/conf.d
, mount -v /path/to/mysql/data:/var/lib/mysql
the host's mysql/data
directory to the directory in the container /var/lib/mysql
, -e MYSQL_ROOT_PASSWORD=<password>
specify the password of the MySQL root user, -d
which means starting the container in background mode, and -p 3306:3306
set the container's 3306 port Maps to port 3306 of the host.
Modify MySQL configuration file
mysql
Create a file named in the host's directory my.cnf
to 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
mysql
After 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, -h
specify the IP address of MySQL, -P
specify the port of MySQL, and -u
specify the user name, -p
which 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.