Docker|Docker 安装 Mysql 详解

本文将以完整的安装 MySQL 为例将 镜像与 容器 操作完全串联起来
环境说明 系统为 CentOS 7.5 发行版,内核为 3.10 版本,Docker 为 1.13 版本

[root@localhost ~]# lsb_release -a LSB Version::core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description:CentOS Linux release 7.5.1804 (Core) Release:7.5.1804 Codename:Core [root@localhost ~]# uname -a Linux localhost.localdomain 3.10.0-862.9.1.el7.x86_64 #1 SMP Mon Jul 16 16:29:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [root@localhost ~]# docker version Client: Version:1.13.1 API version:1.26 Package version: docker-1.13.1-68.gitdded712.el7.centos.x86_64 Go version:go1.9.4 Git commit:dded712/1.13.1 Built:Tue Jul 17 18:34:48 2018 OS/Arch:linux/amd64 Server: Version:1.13.1 API version:1.26 (minimum version 1.12) Package version: docker-1.13.1-68.gitdded712.el7.centos.x86_64 Go version:go1.9.4 Git commit:dded712/1.13.1 Built:Tue Jul 17 18:34:48 2018 OS/Arch:linux/amd64 Experimental:false [root@localhost ~]#

此时 docker 中还只要一个 docker.io/tomcat 镜像,需要重新下载 Mysql 镜像
[root@localhost ~]# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE docker.io/tomcat8.5.325808f01b11bf3 days ago463 MB [root@localhost ~]# docker ps -a CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 0f6df57a7fa1tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat2 d67cca0aacf7tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat1 [root@localhost ~]#

Mysql 镜像检索 就下载第一个 docker.io/mysql 官方发行版
[root@localhost ~]# docker search mysql INDEXNAMEDESCRIPTIONSTARSOFFICIALAUTOMATED docker.iodocker.io/mysqlMySQL is a widely used, open-source relati...6749[OK] docker.iodocker.io/mariadbMariaDB is a community-developed fork of M...2150[OK] docker.iodocker.io/mysql/mysql-serverOptimized MySQL Server Docker images. Crea...495[OK] docker.iodocker.io/perconaPercona Server is a fork of the MySQL rela...360[OK] docker.iodocker.io/zabbix/zabbix-server-mysqlZabbix Server with MySQL database support114[OK] docker.iodocker.io/hypriot/rpi-mysqlRPi-compatible Docker Image with Mysql93 docker.iodocker.io/zabbix/zabbix-web-nginx-mysqlZabbix frontend based on Nginx web-server ...62[OK] docker.iodocker.io/centurylink/mysqlImage containing mysql. Optimized to be li...60[OK] docker.iodocker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5ubuntu-16-nginx-php-phpmyadmin-mysql-543[OK] docker.iodocker.io/centos/mysql-57-centos7MySQL 5.7 SQL database server38 docker.iodocker.io/mysql/mysql-clusterExperimental MySQL Cluster Docker images. ...33 docker.iodocker.io/tutum/mysqlBase docker image to run a MySQL database ...32 docker.iodocker.io/schickling/mysql-backup-s3Backup MySQL to S3 (supports periodic back...20[OK] docker.iodocker.io/bitnami/mysqlBitnami MySQL Docker Image16[OK] docker.iodocker.io/zabbix/zabbix-proxy-mysqlZabbix proxy with MySQL database support15[OK] docker.iodocker.io/linuxserver/mysqlA Mysql container, brought to you by Linux...14 docker.iodocker.io/centos/mysql-56-centos7MySQL 5.6 SQL database server9 docker.iodocker.io/circleci/mysqlMySQL is a widely used, open-source relati...6 docker.iodocker.io/openshift/mysql-55-centos7DEPRECATED: A Centos7 based MySQL v5.5 ima...6 docker.iodocker.io/dsteinkopf/backup-all-mysqlbackup all DBs in a mysql server4[OK] docker.iodocker.io/mysql/mysql-routerMySQL Router provides transparent routing ...2 docker.iodocker.io/openzipkin/zipkin-mysqlMirror of https://quay.io/repository/openz...1 docker.iodocker.io/ansibleplaybookbundle/mysql-apbAn APB which deploys RHSCL MySQL0[OK] docker.iodocker.io/cloudfoundry/cf-mysql-ciImage used in CI of cf-mysql-release0 docker.iodocker.io/cloudposse/mysqlImproved `mysql` service with support for ...0[OK] [root@localhost ~]#

可以从 Docker Hub 上查看 所有 Mysql 的版本 ,同理也可以查询各个版本使用的详细命令

Docker|Docker 安装 Mysql 详解
文章图片
image Mysql 镜像下载
[root@localhost ~]# docker pull mysql:5.5 Trying to pull repository docker.io/library/mysql ... 5.5: Pulling from docker.io/library/mysql be8881be8156: Pull complete c3995dabd1d7: Pull complete 9931fdda3586: Pull complete bb1b6b6eff6a: Pull complete a65f125fa718: Pull complete b5332dacc087: Pull complete 77378af32110: Pull complete c863c888ab89: Pull complete 970e89bcad84: Pull complete 199374dd7b11: Pull complete daf925b4c8c8: Pull complete Digest: sha256:973d09f802e90069f4090578bbfb5f1cc095d5c14bdd0bb2651d5e448751f907 Status: Downloaded newer image for docker.io/mysql:5.5 [root@localhost ~]# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE docker.io/tomcat8.5.325808f01b11bf3 days ago463 MB docker.io/mysql5.5c43b4117afc42 weeks ago205 MB [root@localhost ~]#

Mysql 容器运行 错误运行 如下所示演示的是 Mysql 镜像运行时没有指定 密码而导致异常退出启动失败的情况
通过 docker logs container-name/container-id 命令 查看日志已经提醒的很明显了,必须指定 root 密码、或者允许密码为空、或者使用随机密码
最后将此错误的容器删除掉:docker rm container-id
[root@localhost ~]# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE docker.io/tomcat8.5.325808f01b11bf3 days ago463 MB docker.io/mysql5.5c43b4117afc42 weeks ago205 MB [root@localhost ~]# docker run --name mysql01 -d mysql:5.5 fb1c96d110c715034c263185e40f066dc688e2284c02c250243e25c017cf872b [root@localhost ~]# docker ps -a CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES fb1c96d110c7mysql:5.5"docker-entrypoint..."9 seconds agoExited (1) 6 seconds agomysql01 0f6df57a7fa1tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat2 d67cca0aacf7tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat1 [root@localhost ~]# docker logs fb1c96d110c7 error: database is uninitialized and password option is not specified You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD [root@localhost ~]# docker rm fb1c96d110c7 fb1c96d110c7 [root@localhost ~]# docker ps -a CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 0f6df57a7fa1tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat2 d67cca0aacf7tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat1 [root@localhost ~]#

正确运行 Docker Hub 上 Mysq 官方 提供的启动命令如下所示 ,需要使用 -e 参数指定 roor 账户密码进行启动
Start a mysql server instance
Starting a MySQL instance is simple:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

... where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be set for the MySQL root user and tag is the tag specifying the MySQL version you want. See the list above for relevant tags.
[root@localhost ~]# docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.5 3a896a0867265c59d84d209a93d60f3d837b4667c649981eb3933e997a459c55 [root@localhost ~]# docker ps -a CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 3a896a086726mysql:5.5"docker-entrypoint..."3 seconds agoUp 2 seconds3306/tcpmysql01 0f6df57a7fa1tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat2 d67cca0aacf7tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat1 [root@localhost ~]#

如上所示,此时 mysql 容器运行是成功的,但是《 Docker 容器操作 常用命令》中已经说过,此时启动的 3306 端口是 Mysql 镜像在容器内部的端口,外部访问必须进行端口映射,再次删除此无效的容器。
[root@localhost ~]# docker stop 3a896a086726 3a896a086726 [root@localhost ~]# docker rm 3a896a086726 3a896a086726 [root@localhost ~]# docker ps -a CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 0f6df57a7fa1tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat2 d67cca0aacf7tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat1 [root@localhost ~]#

端口映射 端口映射加上 -p 参数即可,可以参考《 Docker 容器操作 常用命令》,因为 CentOS 服务器本身外部已经安装了 Mysql 已经占用了 3306 端口,所以使用 3307 映射 Docker 容器中的 3306 端口
[root@localhost ~]# docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=root -p 3307:3306 -d mysql:5.5 2aad1d2a8e76261715ac0f1d4ef03217ae54866db479d26b81364a39e2ec3801 [root@localhost ~]# netstat -ntlpu Active Internet connections (only servers) Proto Recv-Q Send-Q Local AddressForeign AddressStatePID/Program name tcp00 0.0.0.0:220.0.0.0:*LISTEN1041/sshd tcp00 127.0.0.1:250.0.0.0:*LISTEN1188/master tcp600 :::33060:::*LISTEN1308/mysqld tcp600 :::3306:::*LISTEN1308/mysqld tcp600 :::3307:::*LISTEN2892/docker-proxy-c tcp600 :::22:::*LISTEN1041/sshd tcp600 ::1:25:::*LISTEN1188/master udp00 0.0.0.0:680.0.0.0:*857/dhclient [root@localhost ~]# docker ps -a CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 2aad1d2a8e76mysql:5.5"docker-entrypoint..."14 seconds agoUp 14 seconds0.0.0.0:3307->3306/tcpmysql01 0f6df57a7fa1tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 44 hours agomyTtomcat2 d67cca0aacf7tomcat:8.5.32"catalina.sh run"44 hours agoExited (143) 43 hours agomyTtomcat1 [root@localhost ~]#

如上所示做了端口映射后,就可以从外部访问 Docker 容器内部的 Mysql 了,但是如果要从 CentOS 服务器外部访问,则还需要 CentOS 服务器开放 3307 端口
[root@localhost ~]# firewall-cmd --zone=public --list-port 9876/tcp 8090/tcp 80/tcp 8080/tcp [root@localhost ~]# firewall-cmd --zone=public --add-port=3307/tcp --permanent success [root@localhost ~]# firewall-cmd --reload success [root@localhost ~]# firewall-cmd --zone=public --list-port 9876/tcp 8090/tcp 80/tcp 8080/tcp 3307/tcp [root@localhost ~]#

连接测试 如下所示,从 Windows 上使用 Navicat 连接虚拟机 CentOS 中 Docker 中的 Mysql
Docker|Docker 安装 Mysql 详解
文章图片
image
Docker|Docker 安装 Mysql 详解
文章图片
image 高级命令 指定编码运行 【Docker|Docker 安装 Mysql 详解】例如,如果要更改所有表的默认编码和排序,使用UTF-8(UTF8Mb4),只需使用如下命令运行
更多命令,请参考官方文档:https://hub.docker.com/r/library/mysql/
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

    推荐阅读