跳到主要内容

二十四、Docker 安装 MySQL

MySQL 是当下最流行的可免费使用的关系型数据库系统,Docker 安装 MySQL 有两种方法

我们以当前最新的版本 8.0.11 安装为例

1. docker pull mysql

这种方法非常适合只需要使用 MySQL 的开发者

1、 查找DockerHub上的mysql镜像;

    [root@pottercoding.cn ~]# docker search mysql
NAME DESCRIPTION OFFICIAL
mysql widely used, open-source... [OK]
mariadb MariaDB is a ... [OK]
...
有很多版本,我们选择官方的 mysql

2、 拉取官方的镜像,标签为8.0.11

    [root@pottercoding.cn ~]# docker pull mysql:8.0.11
8.0.11: Pulling from library/mysql

3、 稍等片刻,就能在本地镜像列表里看到8.0.11了;

    [root@pottercoding.cn ~]# docker images mysql
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.11 a8a59477268d 3 weeks ago 444.8 MB

2. 通过 Dockerfile 构建 MySQL

这种方式类似于自己编译安装,既可以不污染环境,又能学习如何编译安装 MySQL

1、 先创建目录mysql,用于存放后面的相关东西;

    [root@pottercoding.cn ~]# mkdir -p ~/mysql/data ~/mysql/logs ~/mysql/conf
<table> 
<thead>
<tr>
<th align="left">目录</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">data</td>
<td align="left">该目录将映射为 mysql 容器配置的数据文件存放路径</td>
</tr>
<tr>
<td align="left">logs</td>
<td align="left">该目录将映射为 mysql 容器的日志目录</td>
</tr>
<tr>
<td align="left">conf</td>
<td align="left">该目录里的配置文件将映射为 mysql 容器的配置文件</td>
</tr>
</tbody>
</table>