- 先查看远端的版本
docker search mysql
[root@iZbp17pvndv49r9ozf5hbcZ ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 8760 [OK] mariadb MariaDB is a community-developed fork of MyS… 3072 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 650 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 64 centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK] mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 55 deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] bitnami/mysql Bitnami MySQL Docker Image 35 [OK] tutum/mysql Base docker image to run a MySQL database se… 34 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 28 [OK] prom/mysqld-exporter 23 [OK] linuxserver/mysql A Mysql container, brought to you by LinuxSe… 22 centos/mysql-56-centos7 MySQL 5.6 SQL database server 16 circleci/mysql MySQL is a widely used, open-source relation… 15 mysql/mysql-router MySQL Router provides transparent routing be… 13 arey/mysql-client Run a MySQL client from a docker container 12 [OK] imega/mysql-client Size: 36 MB, alpine:3.5, Mysql client: 10.1.… 8 [OK] yloeffler/mysql-backup This image runs mysqldump to backup data usi… 6 [OK] openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 4 [OK] genschsa/mysql-employees MySQL Employee Sample Database 2 [OK] ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] jelastic/mysql An image of the MySQL database server mainta… 1 widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK] monasca/mysql-init A minimal decoupled init container for mysql 0
- 从远端下载
docker pull mysql
[root@iZbp17pvndv49r9ozf5hbcZ ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql 80369df48736: Pull complete e8f52315cb10: Pull complete cf2189b391fc: Pull complete cc98f645c682: Pull complete 27a27ac83f74: Pull complete fa1f04453414: Pull complete d45bf7d22d33: Pull complete 3dbac26e409c: Pull complete 9017140fb8c1: Pull complete b76dda2673ae: Pull complete bea9eb46d12a: Pull complete e1f050a38d0f: Pull complete Digest: sha256:7345ce4ce6f0c1771d01fa333b8edb2c606ca59d385f69575f8e3e2ec6695eee Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest
- 查看镜像
docker images
[root@iZbp17pvndv49r9ozf5hbcZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest c8ee894bd2bd 2 weeks ago 456MB
- 然后先创建一个临时的mysql,进入容器中
docker run --rm --name mysqlTemp -it -v /usr/local/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql /bin/bash
– rm 退出后就删除该容器
-v /usr/local/mysql/:/var/lib/mysql 装主机目录/usr/local/mysql/映射Docker中的/var/lib/mysql目录
-it 交互模式
/bin/bash 进入 bash 命令模式
5. 查看 my.cnf文件。cat /etc/mysql/my.cnf
root@ea063c22ea1e:/# cat /etc/mysql/my.cnf
# Copyright (c) 2017, 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 as published by
# the Free Software Foundation; version 2 of the License.
#
# 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 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
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
如果是完整的my.cnf文件,直接复制出去,如果不是,则需要把includedir目录一起复到主机
6. 拷贝配置文件
- 把Docker中的/etc/mysql/my.cnf文件拷贝到Docker的/var/lib/mysql目录,及主机的/usr/local/mysql/目录中
cp /etc/mysql/my.cnf /var/lib/mysql
- 把Docker中includedir目录复制出去
cp -R /etc/mysql/conf.d/ /var/lib/mysql/conf.d
- 退出bash,Docker中临时用的mysqlTemp会被删除
然后可以查看/usr/local/mysql/目录下有mysql所有的配置文件[root@iZbp17pvndv49r9ozf5hbcZ ~]# ls /usr/local/mysql/ conf.d my.cnf
- 这里就可以修改配置了。
改完之后,创建正式的mysql容器
如果my.cnf 是完整的配置,这里不需要映射conf.d和mysql.conf.d这二个目录docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123455 -v /usr/local/mysql/data:/var/lib/mysql -v /usr/local/mysql/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql/conf.d:/etc/mysql/conf.d -d mysql
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123455 -v /usr/local/mysql/data:/var/lib/mysql -v /usr/local/mysql/my.cnf:/etc/mysql/my.cnf -d mysql
- –name 容器名称
- -p 端口映射
- -v 配置文件映射:将 宿主机上修改的自定义配置文件,映射到 容器中
- -e MYSQL_ROOT_PASSWORD 设置root密码
- -d 守护进程模式运行