1.拉取mysql5.7 镜像
docker pull mysql:5.7
2.检查镜像是否下载完毕
输入
docker images
如果如下图显示就说明下载成功
3.准备数据目录
目的:我们经历过一次服务器宕机,mysql的数据找不回来了,所以推荐把数据存储到
mkdir -p /home/dockerdata/mysql/conf
mkdir -p /home/dockerdata/mysql/logs
mkdir -p /home/dockerdata/mysql/mysql
4.创建配置文件
cd /home/dockerdata/mysql/conf/
vi my.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
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_connections = 2000
max_user_connections = 1900
max_connect_errors = 100000
max_allowed_packet = 50M
lower_case_table_names=1
[mysqld]
skip-name-resolve
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#保存后退出
5.启动容器
docker run -p 3307:3306 --name mysql2.0 -v /home/dockerdata/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /home/dockerdata/mysql/logs:/logs -v /home/dockerdata/mysql/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
6.启动后数据目录截图
7.链接测试
8.常见命令
如果容器启动失败使用命令查看docker日志
docker logs 97069f94437b
进入容器
docker attach 44fc0f0582d9
本文同步分享在 博客“DencyCheng”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。