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 command is 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
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
If you do not specify a version number, the latest version will be downloaded by default:
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. Use the following command to create it:#
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
1.-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
2.-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
3.-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, you need to 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, and if the test is successful, it indicates that the mysql
instance in docker
has started normally.