MySQL 是当下最流行的可免费使用的关系型数据库系统,Docker 安装 MySQL
有两种方法
我们以当前最新的版本 8.0.11
安装为例
1. docker pull mysql
这种方法非常适合只需要使用 MySQL
的开发者
-
查找 Docker Hub 上的 mysql 镜像
[root@localhost ~]# docker search mysql NAME DESCRIPTION OFFICIAL mysql widely used, open-source... [OK] mariadb MariaDB is a ... [OK] ...
有很多版本,我们选择官方的
mysql
-
拉取官方的镜像,标签为
8.0.11
[root@localhost ~]# docker pull mysql:8.0.11 8.0.11: Pulling from library/mysql
-
稍等片刻,就能在本地镜像列表里看到
8.0.11
了[root@localhost ~]# docker images mysql REPOSITORY TAG IMAGE ID CREATED SIZE mysql 8.0.11 a8a59477268d 3 weeks ago 444.8 MB
2. 通过 Dockerfile 构建 MySQL
这种方式类似于自己编译安装,既可以不污染环境,又能学习如何编译安装 MySQL
-
先创建目录
mysql
, 用于存放后面的相关东西[root@localhost ~]# mkdir -p ~/mysql/data ~/mysql/logs ~/mysql/conf
目录 说明 data 该目录将映射为 mysql 容器配置的数据文件存放路径 logs 该目录将映射为 mysql 容器的日志目录 conf 该目录里的配置文件将映射为 mysql 容器的配置文件 -
进入创建的
mysql
目录,创建conf/my.cnf
文件[root@localhost ~] cd mysql [root@localhost mysql] vi conf/my.cnf
然后输入以下内容
[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL symbolic-links=0 # Custom config should go here !includedir /etc/mysql/conf.d/
-
创建
docker-entrypoint.sh
文件[root@localhost mysql]# touch docker-entrypoint.sh [root@localhost mysql]# chmod +x docker-entrypoint.sh [root@localhost mysql]# vi docker-entrypoint.sh
然后复制以下内容
# !/bin/bash # 删除第一行 # ! 之间的空格 set -eo pipefail shopt -s nullglob # if command starts with an option, prepend mysqld if [ "${1:0:1}" = '-' ]; then set -- mysqld "$@" fi # skip setup if they want an option that stops mysqld wantHelp= for arg; do case "$arg" in -'?'|--help|--print-defaults|-V|--version) wantHelp=1 break ;; esac done # usage: file_env VAR [DEFAULT] # ie: file_env 'XYZ_DB_PASSWORD' 'example' # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of # "$XYZ_DB_PASSWORD" from a fi