第一步 新建目录和文件
新建以下目录:
mkdir -p /panda/docker-mysql/conf.d #配置目录mkdir -p /panda/docker-mysql/datadir #数据目录mkdir -p /panda/docker-mysql/mysql.conf.d #配置目录
新建一下文件:
touch /panda/docker-mysql/conf.d/docker.cnftouch /panda/docker-mysql/conf.d/mysql.cnftouch /panda/docker-mysql/conf.d/mysqldump.cnftouch /panda/docker-mysql/mysql.conf.d/mysqld.cnf
第二步 文件写入配置信息
在 conf.d 文件夹内新建 docker.cnf 文件
[mysqld]skip-host-cacheskip-name-resolve
在 conf.d 文件夹内新建 mysql.cnf 文件
[mysql]
在 conf.d 文件夹内新建 mysqldump.cnf 文件
[mysqldump]quickquote-namesmax_allowed_packet = 16M
第三步 mysqld.cnf文件配置(mysql重要配置文件)
在 mysql.conf.d 文件夹内新建 mysqld.cnf 文件
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; version 2 of the License.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA## The MySQL Server configuration file.## For explanations see# http://dev.mysql.com/doc/mysql/en/server-system-variables.html[mysqld]pid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockdatadir = /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 riskssymbolic-links=0
第四步 拉取镜像
docker pull mysql:5.7
第五步 构建容器
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端口
