background:
Nowadays, no matter what services or applications are, they can basically run in docker, but in my personal impression, applications like databases that are more important and large and easily damaged are not suitable for running in docker. But there are also many people trying to run databases such as mysql in docker, so try it too. (Okay, the point is that the leader likes it~~)
Get the mirror:
MySQL image can be created by yourself using dockerfile, or directly downloaded in the official docker image library. This article uses the official image.
# docker pull mysql # docker images REPOSITORY TAG IMAGE ID CREATED SIZE /mysql latest d9124e6c552f 12 days ago 383.4 MB
Run the container:
1: Normal operation.
Start the container:
# docker run --name cmh-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d /mysql
Enter the container:
# docker-enter cmh-mysql
Enter mysql:
root@3a2b8ab0d971:~# mysql -u root -pmy-secret-pw mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016,Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
The above creates a docker container for mysql, and you can see that the version is 5.7.16. However, there are two problems with the container created in this way. One is that the data is lost after the container is deleted. The other is that you have to access the database and you must enter the container.
2: Persistent data, map open mysql port
Create a host data storage directory:
# mkdir -p /opt/data/mysql
Start the container:
# docker run --name cmh-mysql -v /opt/data/mysql/:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d /mysql
c38f50a540ff4d5ecf1a5ec49fb721335a8e1b79dec58229cf5e00553f988e44
View container:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c38f50a540ff /mysql "" 9 seconds ago Up 8 seconds 0.0.0.0:3306->3306/tcp cmh-mysql
View port:
# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::3306 :::* LISTEN 28657/docker-proxy
View the mysql data on the host:
# cd /opt/data/mysql # ll total 188452 -rw-r-----. 1 systemd-bus-proxy ssh_keys 56 Dec 6 16:01 -rw-r-----. 1 systemd-bus-proxy ssh_keys 1325 Dec 6 16:01 ib_buffer_pool -rw-r-----. 1 systemd-bus-proxy ssh_keys 79691776 Dec 6 17:16 ibdata1 -rw-r-----. 1 systemd-bus-proxy ssh_keys 50331648 Dec 6 17:16 ib_logfile0 -rw-r-----. 1 systemd-bus-proxy ssh_keys 50331648 Dec 6 16:01 ib_logfile1 -rw-r-----. 1 systemd-bus-proxy ssh_keys 12582912 Dec 6 17:16 ibtmp1 drwxr-x---. 2 systemd-bus-proxy ssh_keys 4096 Dec 6 16:01 mysql drwxr-x---. 2 systemd-bus-proxy ssh_keys 8192 Dec 6 16:01 performance_schema drwxr-x---. 2 systemd-bus-proxy ssh_keys 8192 Dec 6 16:01 sys
-p 3306:3306 maps the container's mysql port 3306 to the host's port 3306. In this way, if you want to access mysql, you can directly access the host's port 3306.
-v /opt/data/mysql:/var/lib/mysql, that is, map the host /opt/data/mysql/ directory to the /var/lib/mysql directory of the container.
Notes:
1: When mapping the directory with the -v option, the host needs to turn off SElinux:
# setenforce 0
Or add relevant selinux permissions to the data directory:
# chcon -Rt svirt_sandbox_file_t /my/own/datadir
2: The -v option originally maps the host's directory into the container, but in this article, it is the opposite. That is, map the directory in the container to the host, because the official image uses the VOLUME /var/lib/mysql option when it is made. This makes /var/lib/mysql in the container a separate volume group, and when using the mount option -v, the directory can be mapped out of the host.
You can refer to the dockerfile of the official mysql image:
/docker-library/mysql/blob/4dd33136c4739667a223d39b6f829beb27b235cf/5.7/Dockerfile
DOCKER IntroductionClick to view
Docker from Beginner to PracticeClick to view