欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker 安装 Mysql 详解

程序员文章站 2022-04-14 10:37:17
...

本文导读

环境说明

  • 系统为 CentOS 7.5 发行版,内核为 3.10 版本,Docker 为 1.13 版本
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# 
  • 此时 docker 中还只要一个 docker.io/tomcat 镜像,需要重新下载 Mysql 镜像
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/tomcat    8.5.32              5808f01b11bf        3 days ago          463 MB
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
0f6df57a7fa1        tomcat:8.5.32       "catalina.sh run"   44 hours ago        Exited (143) 43 hours ago                       myTtomcat2
d67cca0aacf7        tomcat:8.5.32       "catalina.sh run"   44 hours ago        Exited (143) 43 hours ago                       myTtomcat1
[aaa@qq.com ~]# 

Mysql 镜像检索

  • 就下载第一个 docker.io/mysql 官方发行版
[aaa@qq.com ~]# docker search mysql
INDEX       NAME                                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                                                  MySQL is a widely used, open-source relati...   6749      [OK]       
docker.io   docker.io/mariadb                                                MariaDB is a community-developed fork of M...   2150      [OK]       
docker.io   docker.io/mysql/mysql-server                                     Optimized MySQL Server Docker images. Crea...   495                  [OK]
docker.io   docker.io/percona                                                Percona Server is a fork of the MySQL rela...   360       [OK]       
docker.io   docker.io/zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       114                  [OK]
docker.io   docker.io/hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          93                   
docker.io   docker.io/zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server ...   62                   [OK]
docker.io   docker.io/centurylink/mysql                                      Image containing mysql. Optimized to be li...   60                   [OK]
docker.io   docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          43                   [OK]
docker.io   docker.io/centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   38                   
docker.io   docker.io/mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. ...   33                   
docker.io   docker.io/tutum/mysql                                            Base docker image to run a MySQL database ...   32                   
docker.io   docker.io/schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic back...   20                   [OK]
docker.io   docker.io/bitnami/mysql                                          Bitnami MySQL Docker Image                      16                   [OK]
docker.io   docker.io/zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        15                   [OK]
docker.io   docker.io/linuxserver/mysql                                      A Mysql container, brought to you by Linux...   14                   
docker.io   docker.io/centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   9                    
docker.io   docker.io/circleci/mysql                                         MySQL is a widely used, open-source relati...   6                    
docker.io   docker.io/openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 ima...   6                    
docker.io   docker.io/dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                4                    [OK]
docker.io   docker.io/mysql/mysql-router                                     MySQL Router provides transparent routing ...   2                    
docker.io   docker.io/openzipkin/zipkin-mysql                                Mirror of https://quay.io/repository/openz...   1                    
docker.io   docker.io/ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                    [OK]
docker.io   docker.io/cloudfoundry/cf-mysql-ci                               Image used in CI of cf-mysql-release            0                    
docker.io   docker.io/cloudposse/mysql                                       Improved `mysql` service with support for ...   0                    [OK]
[aaa@qq.com ~]# 

Docker 安装 Mysql 详解

Mysql 镜像下载

[aaa@qq.com ~]# 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
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/tomcat    8.5.32              5808f01b11bf        3 days ago          463 MB
docker.io/mysql     5.5                 c43b4117afc4        2 weeks ago         205 MB
[aaa@qq.com ~]# 

Mysql 容器运行

错误运行

  • 如下所示演示的是 Mysql 镜像运行时没有指定 密码而导致异常退出启动失败的情况
  • 通过 docker logs container-name/container-id 命令 查看日志已经提醒的很明显了,必须指定  root  密码、或者允许密码为空、或者使用随机密码
  • 最后将此错误的容器删除掉:docker rm container-id
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/tomcat    8.5.32              5808f01b11bf        3 days ago          463 MB
docker.io/mysql     5.5                 c43b4117afc4        2 weeks ago         205 MB
[aaa@qq.com ~]# docker run --name mysql01 -d mysql:5.5
fb1c96d110c715034c263185e40f066dc688e2284c02c250243e25c017cf872b
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
fb1c96d110c7        mysql:5.5           "docker-entrypoint..."   9 seconds ago       Exited (1) 6 seconds ago                        mysql01
0f6df57a7fa1        tomcat:8.5.32       "catalina.sh run"        44 hours ago        Exited (143) 43 hours ago                       myTtomcat2
d67cca0aacf7        tomcat:8.5.32       "catalina.sh run"        44 hours ago        Exited (143) 43 hours ago                       myTtomcat1
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# docker rm fb1c96d110c7
fb1c96d110c7
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
0f6df57a7fa1        tomcat:8.5.32       "catalina.sh run"   44 hours ago        Exited (143) 43 hours ago                       myTtomcat2
d67cca0aacf7        tomcat:8.5.32       "catalina.sh run"   44 hours ago        Exited (143) 43 hours ago                       myTtomcat1
[aaa@qq.com ~]# 

正确运行

  • 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.

[aaa@qq.com ~]# docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.5
3a896a0867265c59d84d209a93d60f3d837b4667c649981eb3933e997a459c55
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
3a896a086726        mysql:5.5           "docker-entrypoint..."   3 seconds ago       Up 2 seconds                3306/tcp            mysql01
0f6df57a7fa1        tomcat:8.5.32       "catalina.sh run"        44 hours ago        Exited (143) 43 hours ago                       myTtomcat2
d67cca0aacf7        tomcat:8.5.32       "catalina.sh run"        44 hours ago        Exited (143) 43 hours ago                       myTtomcat1
[aaa@qq.com ~]# 
  • 如上所示,此时 mysql 容器运行是成功的,但是《 Docker 容器操作 常用命令》中已经说过,此时启动的 3306 端口是 Mysql 镜像在容器内部的端口,外部访问必须进行端口映射,再次删除此无效的容器。
[aaa@qq.com ~]# docker stop 3a896a086726
3a896a086726
[aaa@qq.com ~]# docker rm 3a896a086726
3a896a086726
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
0f6df57a7fa1        tomcat:8.5.32       "catalina.sh run"   44 hours ago        Exited (143) 43 hours ago                       myTtomcat2
d67cca0aacf7        tomcat:8.5.32       "catalina.sh run"   44 hours ago        Exited (143) 43 hours ago                       myTtomcat1
[aaa@qq.com ~]# 

端口映射

  • 端口映射加上 -p 参数即可,可以参考《 Docker 容器操作 常用命令》,因为 CentOS 服务器本身外部已经安装了 Mysql 已经占用了 3306 端口,所以使用 3307 映射 Docker 容器中的 3306 端口
[aaa@qq.com ~]# docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=root -p 3307:3306 -d mysql:5.5
2aad1d2a8e76261715ac0f1d4ef03217ae54866db479d26b81364a39e2ec3801
[aaa@qq.com ~]# netstat -ntlpu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1041/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1188/master         
tcp6       0      0 :::33060                :::*                    LISTEN      1308/mysqld         
tcp6       0      0 :::3306                 :::*                    LISTEN      1308/mysqld         
tcp6       0      0 :::3307                 :::*                    LISTEN      2892/docker-proxy-c 
tcp6       0      0 :::22                   :::*                    LISTEN      1041/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1188/master         
udp        0      0 0.0.0.0:68              0.0.0.0:*                           857/dhclient        
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                    NAMES
2aad1d2a8e76        mysql:5.5           "docker-entrypoint..."   14 seconds ago      Up 14 seconds               0.0.0.0:3307->3306/tcp   mysql01
0f6df57a7fa1        tomcat:8.5.32       "catalina.sh run"        44 hours ago        Exited (143) 44 hours ago                            myTtomcat2
d67cca0aacf7        tomcat:8.5.32       "catalina.sh run"        44 hours ago        Exited (143) 43 hours ago                            myTtomcat1
[aaa@qq.com ~]# 
  • 如上所示做了端口映射后,就可以从外部访问 Docker 容器内部的 Mysql 了,但是如果要从 CentOS 服务器外部访问,则还需要 CentOS 服务器开放 3307 端口
[aaa@qq.com ~]# firewall-cmd --zone=public --list-port
9876/tcp 8090/tcp 80/tcp 8080/tcp
[aaa@qq.com ~]# firewall-cmd --zone=public --add-port=3307/tcp --permanent
success
[aaa@qq.com ~]# firewall-cmd --reload
success
[aaa@qq.com ~]# firewall-cmd --zone=public --list-port
9876/tcp 8090/tcp 80/tcp 8080/tcp 3307/tcp
[aaa@qq.com ~]# 

连接测试

  • 如下所示,从 Windows 上使用 Navicat 连接虚拟机 CentOS 中 Docker 中的 Mysql

Docker 安装 Mysql 详解

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