方法二、docker pull mysql
查找Docker Hub上的mysql镜像
runoob@runoob:/mysql$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relati... 2529 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Crea... 161 [OK]
centurylink/mysql Image containing mysql. Optimized to be li... 45 [OK]
sameersbn/mysql 36 [OK]
google/mysql MySQL server for Google Compute Engine 16 [OK]
appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]
marvambass/mysql MySQL Server based on Ubuntu 14.04 6 [OK]
drupaldocker/mysql MySQL for Drupal 2 [OK]
azukiapp/mysql Docker image to run MySQL by Azuki - http:... 2 [OK]
...
这里我们拉取官方的镜像,标签为5.6
runoob@runoob:~/mysql$ docker pull mysql:5.6
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql,标签为5.6的镜像。
使用mysql镜像
运行容器
runoob@runoob:~/mysql$ docker run -p 3306:3306 --name mymysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
21cb89213c93d805c5bacf1028a0da7b5c5852761ba81327e6b99bb3ea89930e
runoob@runoob:~/mysql$
命令说明:
-
-p 3306:3306:将容器的3306端口映射到主机的3306端口
-
-v $PWD/conf/my.cnf:/etc/mysql/my.cnf:将主机当前目录下的conf/my.cnf挂载到容器的/etc/mysql/my.cnf
-
-v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs
-
-v $PWD/data:/mysql_data:将主机当前目录下的data目录挂载到容器的/mysql_data
-
-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码
查看容器启动情况
runoob@runoob:~/mysql$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
21cb89213c93 mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql
Brian
153***2799@qq.com
最新官方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 imageID
4ec4f56455ea2d6d7251a05b7f308e314051fdad2c26bf3d0f27a9b0c0a71414
命令说明:
- -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
- imageID: mysql镜像ID
查看容器运行情况
# docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
4ec4f56455ea c73c7527c03a "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mysql
笔记列表