Methods to modify the mysql configuration file under docker

  • 2020-11-30 08:39:03
  • OfStack

Since sql_mode of mysql needs to be modified, every restart of the modification on the command line will fail because the configuration file of mysql under docker is modified.

Operating system: centos7

docker version: Docker version 17.05.0-ES14en, build 89658be

mysql version: 5.7.18

1. Pull the mirror image


docker pull mysql:5.7.18

2. List the mirrors


[root@zk01 ~]# docker images
REPOSITORY                          TAG         IMAGE ID      CREATED       SIZE
mysql                            5.7.18       e799c7f9ae9c    5 weeks ago     407MB

3. Run docker


docker run -d -p 3306:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.18

4. List the running containers


[root@zk01 docker]# docker ps
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS          NAMES
e1066fe2db35    mysql:5.7.18    "docker-entrypoint..."  6 seconds ago    Up 6 seconds    0.0.0.0:3306->3306/tcp  mymysql

Step 5 Enter the container


docker exec -it e1066fe2db35 /bin/bash

6. View the configuration file


/etc/mysql/mysql.conf.d/mysqld.cnf

Configuration file content:


[mysqld]
pid-file  = /var/run/mysqld/mysqld.pid
socket   = /var/run/mysqld/mysqld.sock
datadir   = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address  = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0

7. Check sql_mode of mysql


mysql> SELECT @@GLOBAL.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@GLOBAL.sql_mode                                                             |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

8. Stop and delete the container


docker stop e1066fe2db35
docker rm e1066fe2db35

9. Restart the container, specifying the data directory and configuration file


docker run -d -p 3306:3306 -v /soft/mysql/my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /soft/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mymysql mysql:5.7.18

10. View the sql_mode


[root@zk01 ~]# docker images
REPOSITORY                          TAG         IMAGE ID      CREATED       SIZE
mysql                            5.7.18       e799c7f9ae9c    5 weeks ago     407MB
0

conclusion


Related articles: