第一步

新建以下目录:
mkdir -p /www/mysql/conf.d 配置目录
mkdir -p /www/mysql/datadir 数据目录
mkdir -p /www/mysql/mysql.conf.d 配置目录

第二步

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

[mysqld]
skip-host-cache
skip-name-resolve

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

[mysql]

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

[mysqldump]
quick
quote-names
max_allowed_packet      = 16M

第三步

在 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.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

第四步

拉取镜像
docker pull mysql:5.7

第五步构建容器

docker run --name mysql --restart=always -p 3306:3306 -v /www/mysql/conf.d:/etc/mysql/conf.d -v /www/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d -v /www/mysql/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=zan123456 -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端口
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐