win7 docker centos安装mysql_CentOS 7 使用docker安装mysql

本文详细介绍了如何在Linux系统上安装Docker,设置国内镜像源以解决下载问题,然后通过Docker拉取并安装MySQL5.7镜像。还讲解了如何创建自定义配置文件并将其挂载到容器中,以便于管理和覆盖默认配置。最后,展示了如何启动容器并设置端口映射以及初始化root用户的密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.检查内核版本,必须是3.10及以上

uname -r

2.安装docker

yum install docker -y

3.启动docker

systemctl start docker

4.设置为开机自启

systemctl enable docker

5.搜索docker仓库

docker search mysql

6下载mysql,这里我安装5.7

docker pull mysql:5.7

这里可能会出现报错

Trying to pull repository docker.io/library/mysql ...

5.7: Pulling from docker.io/library/mysql

Get https://registry-1.docker.io/v2/library/mysql/manifests/sha256:b16d058ac835a0a087d4e42a5c200abc2a4936ec73ff7a427b28257f66bb5c04: net/http: TLS handshake timeout

更换为国内的仓库就好了,点击这个链接https://www.daocloud.io/mirror 这里面有每个平台设置国内仓库的方法

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

该脚本可以将 --registry-mirror 加入到你的 Docker 配置文件 /etc/docker/daemon.json 中。适用于 Ubuntu14.04、Debian、CentOS6 、CentOS7、Fedora、Arch Linux、openSUSE Leap 42.1,其他版本可能有细微不同。更多详情请访问文档。

更换好以后重新下载mysql

docker pull mysql:5.7

等待下载完成后,我们就可以在本地镜像列表里查到刚下的mysql

docker images |grep mysql

最新官方MySQL(5.7.19)的docker镜像在创建时映射的配置文件目录有所不同,在此记录并分享给大家:

官方原文:

The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.

大概意思是说:

MySQL(5.7.19)的默认配置文件是 /etc/mysql/my.cnf 文件。如果想要自定义配置,建议向 /etc/mysql/conf.d 目录中创建 .cnf 文件。新建的文件可以任意起名,只要保证后缀名是 cnf 即可。新建的文件中的配置项可以覆盖 /etc/mysql/my.cnf 中的配置项。

具体操作:

首先需要创建将要映射到容器中的目录以及.cnf文件,然后再创建容器

# pwd

/opt

# mkdir -p docker_v/mysql/conf

# cd docker_v/mysql/conf

# touch my.cnf

# docker run -p 3306:3306 --name mysql -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

命令说明:

-p 3306:3306:将容器的3306端口映射到主机的3306端口

-v /opt/docker_v/mysql/conf:/etc/mysql/conf.d:将主机/opt/docker_v/mysql/conf目录挂载到容器的/etc/mysql/conf.d

-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码

-d: 后台运行容器,并返回容器ID

查看容器运行情况

docker ps

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值