第一步 新建目录和文件

新建以下目录:

  1. mkdir -p /panda/docker-mysql/conf.d #配置目录
  2. mkdir -p /panda/docker-mysql/datadir #数据目录
  3. mkdir -p /panda/docker-mysql/mysql.conf.d #配置目录

新建一下文件:

  1. touch /panda/docker-mysql/conf.d/docker.cnf
  2. touch /panda/docker-mysql/conf.d/mysql.cnf
  3. touch /panda/docker-mysql/conf.d/mysqldump.cnf
  4. touch /panda/docker-mysql/mysql.conf.d/mysqld.cnf

第二步 文件写入配置信息

在 conf.d 文件夹内新建 docker.cnf 文件

  1. [mysqld]
  2. skip-host-cache
  3. skip-name-resolve

在 conf.d 文件夹内新建 mysql.cnf 文件

  1. [mysql]

在 conf.d 文件夹内新建 mysqldump.cnf 文件

  1. [mysqldump]
  2. quick
  3. quote-names
  4. max_allowed_packet = 16M

第三步 mysqld.cnf文件配置(mysql重要配置文件)

在 mysql.conf.d 文件夹内新建 mysqld.cnf 文件

  1. # Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; version 2 of the License.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. #
  16. # The MySQL Server configuration file.
  17. #
  18. # For explanations see
  19. # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
  20. [mysqld]
  21. pid-file = /var/run/mysqld/mysqld.pid
  22. socket = /var/run/mysqld/mysqld.sock
  23. datadir = /var/lib/mysql
  24. #log-error = /var/log/mysql/error.log
  25. #By default we only accept connections from localhost
  26. #bind-address = 127.0.0.1
  27. #Disabling symbolic-links is recommended to prevent assorted security risks
  28. symbolic-links=0

第四步 拉取镜像

  1. docker pull mysql:5.7

第五步 构建容器

  1. docker run --name mysql --restart=always -p 3306:3306 -v /panda/docker-mysql/conf.d:/etc/mysql/conf.d -v /panda/docker-mysql/mysql.conf.d:/etc/mysql/mysql.conf.d -v /panda/docker-mysql/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

—restart 标志会检查容器的退出代码,并据此来决定是否要重启容器,默认是不会重启。
—restart的参数说明
always:无论容器的退出代码是什么,Docker都会自动重启该容器。
on-failure:只有当容器的退出代码为非0值的时候才会自动重启。另外,该参数还接受一个可选的重启次数参数,--restart=on-fialure:5表示当容器退出代码为非0时,Docker会尝试自动重启该容器,最多5次。

-v 容器内的 /var/lib/mysql 在宿主机上 /www/mysql/datadir 做映射
-e MYSQL_ROOT_PASSWORD 初始密码
-p 将宿主机3306的端口映射到容器3306端口