1. Get the image:
The mysql image can be made by yourself using dockerfile, or downloaded directly from the official docker image library. This article uses the official image.
# docker pull mysql
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest d9124e6c552f 12 days ago 383.4 MB
2. Run the container:
Create the host data storage directory:
# mkdir -p /opt/data/mysql Start the container: # docker run --name docker-mysql -v /opt/data/mysql/:/var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -d docker.io/mysql c38f50a540ff4d5ecf1a5ec49fb721335a8e1b79dec58229cf5e00553f988e44
3.查看容器:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c38f50a540ff docker.io/mysql "docker-entrypoint.sh" 9 seconds ago Up 8 seconds 0.0.0.0:3306->3306/tcp cmh-mysql
4. Description
-p External port: container port, map the mysql port 3306 of the container to port 3306 of the host, so that if you want to access mysql, you can directly access port 3306 of the host.
-v /opt/data/mysql:/var/lib/mysql, which maps the host /opt/data/mysql/ directory to the container's /var/lib/mysql directory.
Precautions:
1: When using the -v option to map a directory, the host needs to close 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's the other way around. That is, the directory in the container is mapped out of the host, because the VOLUME /var/lib/mysql option is used when the official image is made. This makes /var/lib/mysql a separate volume group in the container, which can be mapped out of the host when the mount option -v is used.
3.mysql ignore case
The default official image configuration file of mysql is in /etc/mysql/mysql.conf.d /mysqld.cnf; modify this configuration file to add lower_case_table_names=1, and then restart the container;
Install the vi command in the container:
apt-get update
apt-get install vim
Or use the shell sed command:
sed -i '/lower_case_table_names/d' /etc/mysql/mysql.conf.d/mysqld.cnf
sed -i '/\[mysqld\]/$a lower_case_table_names=1' /etc/mysql/mysql.conf.d/mysqld.cnf
The first is to delete lower_case_table_names;
The second is to add;