4. docker - mysql 5.7

最後更新: 2019-02-25

介紹

# Optimized MySQL Server Docker images. Created, maintained and supported by the MySQL team at Oracle

https://hub.docker.com/r/mysql/mysql-server/

 


Download Image

 

Download

# tag: 5.5, 5.6, 5.7, latest(latest GA version)

docker pull mysql/mysql-server:tag

i.e.

docker pull mysql/mysql-server:5.7

Start

# if no  "--name" is supplied, a random one is generated
# "-d"  container run in the background

docker run --name=mysql57 -d mysql/mysql-server:5.7

Check

# The container initialization might take some time.

# When the server is ready for use: STATUS:  from (health: starting) to (healthy)

docker ps

monitor the output from the container:

shell> docker logs mysql57

Stop

docker stop mysql57

 


基本設定

 

# Once initialization is finished -> the random password generated for the root user

docker logs mysql57 2>&1 | grep GENERATED

# start a mysql client inside the Docker container you have started

docker exec -it mysql57 mysql -uroot -p

mysql> select User,Host from mysql.user;

+---------------+-----------+
| User          | Host      |
+---------------+-----------+
| healthchecker | localhost |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

# MYSQL_ONETIME_PASSWORD option is true by default => you must reset the server root password by issuing this statement:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '?';

mysql> flush privileges;

mysql> CREATE USER 'root'@'172.17.0.1' IDENTIFIED BY '?';

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.17.0.1';

mysql> GRANT GRANT OPTION ON *.* TO 'root'@'172.17.0.1';

mysql> flush privileges;

 


Other

 

Shell Access

docker exec -it mysql57 bash

Configuring the MySQL Server

docker run --name mysql57 -d mysql/mysql-server:tag \

--character-set-server=utf8mb4 --collation-server=utf8mb4_col

自定 my.cnf

* 如果要自定 my.cnf, 那它一定要正確

# docker cp CONTAINER:SRC_PATH DEST_PATH

docker cp mysql57:/etc/my.cnf /data/mysql57/config

[mysqld]
innodb_buffer_pool_size = 128M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

skip-name-resolve

To disable DNS host name lookups

The server uses only IP addresses and not host names to match connecting hosts to rows in the MySQL grant tables

skip-host-cache

With the cache disabled, the server performs a DNS lookup every time a client connects.

由於加了 "skip-name-resolve", 所以才加 "skip-host-cache"

Persisting Data and Configuration Changes

docker run --name=mysql57 \
-v /data/mysql57/config/my.cnf:/etc/my.cnf \
-v /data/mysql57/datadir:/var/lib/mysql \
-d mysql/mysql-server:5.7

設定 timezone

docker exec -it mysql57 bash

在 container 內

rm -f /etc/localtime

ln -s /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime

docker restart mysql57

查看它的 Config

docker inspect mysql57

 


NAT

 

firewall-cmd --add-rich-rule='rule family="ipv4" source address="x.x.x.x" forward-port port="3307" protocol="tcp" to-port="3306" to-addr="172.17.0.2"'