The docker installation USES the mysql image

  • 2020-06-07 05:35:21
  • OfStack

Background:

Almost every service and application can run in docker these days, but in my personal impression, a database is not suitable for running in docker, which is relatively important and large and data prone. But there are a lot of people trying to run mysql and other databases in docker, so give it a try. (Well, the point is that leaders like it ~~)

Get the mirror:

You can make one image of mysql yourself, or download it directly from the official docker image library, which is used in this article.


# docker pull mysql

# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

docker.io/mysql latest d9124e6c552f 12 days ago 383.4 MB

Running container:

1: Normal operation.

Start container:

# docker run --name cmh-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d docker.io/mysql

Enter the container:

# docker-enter cmh-mysql

Enter the 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>

This creates an docker container for mysql, as you can see from version 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, and the other is that the data must be entered into the container to access the database.

2: Persistent data, mapping open mysql port

Create host data storage directory:

# mkdir -p /opt/data/mysql

Start container:

# docker run --name cmh-mysql -v /opt/data/mysql/:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d docker.io/mysql

c38f50a540ff4d5ecf1a5ec49fb721335a8e1b79dec58229cf5e00553f988e44

View the container:

# 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

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 mysql data on host:


# cd /opt/data/mysql

# ll

total 188452

-rw-r-----. 1 systemd-bus-proxy ssh_keys 56 Dec 6 16:01 auto.cnf

-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

-ES86en 3306:3306 maps the container's mysql port 3306 to the host's port 3306 so that people who want to access mysql can directly access the host's port 3306.

- v/opt/data mysql: / var/lib/mysql, namely the hosting/opt/data/mysql/directory mapping to the container/var/lib/mysql directory.

Notes:

1: When using the -v option to map the directory, 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

The 2: -ES121en option was originally intended to map the host directory into the container, but in this article, it's the other way around. The directory in the container is mapped to the host because the official image was made using the VOLUME /var/lib/mysql option. This makes /var/lib/mysql in the container a separate volume group that can be mapped out to the host when the mount option -ES129en is used.

Refer to the official mysql mirror dockerfile:

https://github.com/docker-library/mysql/blob/4dd33136c4739667a223d39b6f829beb27b235cf/5.7/Dockerfile

DOCKER introduction click here

Docker from primer to Practice click to view


Related articles: