1. View the name of the MySQL image to download on Docker Hub:#
Source: https://blog.csdn.net/weixin_43830765/article/details/123849821
The following commands are executed by default using the root
user or an administrator user; if you are not an administrator, please add sudo
before the command.
In the docker hub
image repository for our development,
open the Docker Hub website
Docker Hub official address
and enter mysql in the search bar above.
Find the image version to pull, and locate the version under tag
.
Return to the virtual machine interface and execute the following command to pull the mysql
image without specifying a version number, which defaults to downloading the latest version:
sudo docker pull mysql
Specify the version number:
sudo docker pull mysql:5.7
2. After the image pull is complete, create a MySQL instance using this image with the following command:#
sudo docker run -d -p 3306:3306 -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
Here is a brief introduction to the parameters above; if you do not want to understand this part, you can directly copy the command above and execute it:
Configure port mapping:
-p 3306:3306 --name mysql
Maps the container's port 3306 to the host's port 3306.
Configure MySQL data volume mounts:
- -v /mydata/mysql/log:/var/log/mysql (log file mount)
Mounts the container's log folder /var/log/mysql to the corresponding /mydata/mysql folder on the host. - -v /mydata/mysql/data:/var/lib/mysql (data file mount)
Mounts the container's data folder /var/lib/mysql to the corresponding /mydata/mysql/data folder on the host. - -v /mydata/mysql/conf:/etc/mysql (configuration file mount)
Mounts the container's configuration folder /etc/mysql to the corresponding /mydata/mysql/conf folder on the host.
(Note: The host mentioned here refers to the current Linux host.)
Configure user:
-e MYSQL_ROOT_PASSWORD=123456
Sets the initial password for the root user to 123456.
Specify image resource:
-d mysql:5.7
-d
: Runs the instance in the background.
mysql:5.7
: Specifies to create a running instance using this image.
The following demonstration shows the command executed by the root
user; if you are not currently the root
user, add sudo
before the command to run it as an administrator.
After successful creation, use the following command to check the created mysql
instance:
docker ps -a
Use Navicat
to test whether the database has started successfully:
The username and password were both set to root
when creating the docker
.
Click to test the connection; if it shows the test was successful, it indicates that the mysql
instance in docker
has started normally.