一、下载mysql镜像(以mysql5.7.29为例)
## 下载mysql5.7.29
sudo docker pull mysql:5.7.29
二、在宿主机创建数据持久化的文件夹
进入服务器根目录后创建mysql数据库持久化文件夹
## 创建mysql5.7的数据持久化配置文件夹
sudo mkdir -p /mysqlData/mysql5.7/conf
## 创建mysql5.7的数据持久化数据文件夹
sudo mkdir -p /mysqlData/mysql5.7/data
## 创建mysql5.7的日志文件夹
sudo mkdir -p /mysqlData/mysql5.7/logs
创建结果如下:
三、启动MySql服务
进入刚刚创建的/mysqlData/mysql5.7/文件夹下
在/mysqlData/mysql5.7/文件夹下创建外部挂载配置文件mysql.cnf
sudo vim mysql.cnf
编写mysql.cnf文件(mysql5.7的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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#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 run语句启动mysql服务
docker run -itd -p 3307:3306 --name mysql5.7.29 --privileged=true -v $PWD/conf:/etc/mysql/conf.d -v $PWD/data:/var/lib/mysql -v $PWD/logs:/logs -e MYSQL_ROOT_PASSWORD=luckserver -e TZ=Asia/Shanghai mysql:5.7.29
## 启动参数说明
## 提升root权限
--privileged=true
## 将容器的 3306 端口映射到主机的 3307 端口。
-p 3307:3306
## 将宿主机上的当前目录下的conf目录挂载到容器的/etc/mysql/conf.d目录
-v $PWD/conf:/etc/mysql/conf.d
## 将宿主机上的当前目录下的data目录挂载到容器的 /var/lib/mysql
-v $PWD/data:/var/lib/mysql
## 将宿主机上的当前目录下的 logs 目录挂载到容器的 /logs。
-v $PWD/logs:/logs
## 设置时区
-e TZ=Asia/Shanghai
查看mysql服务启动情况及对应的挂在目录
使用Navicate测试连接
三、测试mysql持久化
在Navicat中新建数据库并新增一张表
在mysql挂载目录下查看对应的数据
停掉mysql服务并删除对应的容器
回到mysql5.7目录下重新执行mysql服务启动命令(注意:这里一定要回到mysql5.7目录下,否则无法挂载之前的数据)
在mysql挂载目录下查看数据是否存在
使用Navicat重新查看之前创建的库与数据是否存在
参考博文:
https://blog.csdn.net/weixin_38657051/article/details/98596926
https://hub.docker.com/_/mysql