技术交流28群

服务热线

135-6963-3175

微信服务号

Docker安装Mysql 更新时间 2019-1-21 浏览2206次

Docker容器安装Mysql

1、查看可用的mysql镜像版本

https://hub.docker.com/_/mysql?tab=tags

docker mysql可用版本

默认是最新版本 mysql:latest

也可以通过docker search mysql命令查看可用版本

[root@localhost ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9876                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3611                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   721                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   503                 [OK]                
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   81                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   74                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      44                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                                      
prom/mysqld-exporter                                                              31                                      [OK]
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   30                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         29                                      
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   25                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   20                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   19                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   16                                      
arey/mysql-client                 Run a MySQL client from a docker container      14                                      [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   8                                       [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3                                       
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…   1                                       [OK]


2、拉取镜像

docker pull mysql:lastest

[root@localhost ~]# docker pull mysql:latest
latest: Pulling from library/mysql
bf5952930446: Downloading [========>                                          ]  4.345MB/27.09MB
8254623a9871: Download complete 
938e3e06dac4: Downloading [=================================>                 ]  2.815MB/4.178MB
ea28ebf28884: Download complete 
f3cef38785c2: Waiting 
894f9792565a: Waiting 
1d8a57523420: Waiting 
6c676912929f: Waiting 
ff39fdb566b4: Waiting 
fff872988aba: Waiting 
4d34e365ae68: Waiting 
7886ee20621e: Waiting 
latest: Pulling from library/mysql
bf5952930446: Pull complete 
8254623a9871: Pull complete 
938e3e06dac4: Pull complete 
ea28ebf28884: Pull complete 
f3cef38785c2: Pull complete 
894f9792565a: Pull complete 
1d8a57523420: Pull complete 
6c676912929f: Pull complete 
ff39fdb566b4: Pull complete 
fff872988aba: Pull complete 
4d34e365ae68: Pull complete 
7886ee20621e: Pull complete 
Digest: sha256:c358e72e100ab493a0304bda35e6f239db2ec8c9bb836d8a427ac34307d074ed
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

查看镜像库是否已有了mysql

[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              0d64f46acfd1        2 weeks ago         544MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB


运行容器

我们以后台方式运行一个mysql容器

[root@localhost ~]# docker run -itd --name mysql-dev -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
5abf309c553b321546c4b83d075d2b7ee99482337fccb7aadc973b9853609f15

参数说明:--name 给容器起个名字

               -p 3306:3306,指定端口映射,格式为:主机(宿主)端口:容器端口,映射容器服务的端口3306到宿主机的3306端口,可以通过宿主机的3306端口直接访问。

              -e设置环境变量

               MYSQL_ROOT_PASSWORD: mysql root用户密码。

然后我们查看运行中的容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
5abf309c553b        mysql               "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-dev

下面可以用navicat客户端或者命令行测试一下是否安装成功:

docker搭建mysql

注:因为本示例安装docker环境的ip为192.168.100,也就是mysql的宿主机为此ip,所以直接连接此ip即可,当然也要保证宿主机端口已开放。