Linux Docker安装mysql并持久化


一、下载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

  • 13
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值