docker入门实践,安装指定版本的mysql镜像

本文以安装mysql5.7为例

查找镜像仓库

含有mysql关键字的记录,这里选择了stars最高,即第1条

$ docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   8147                [OK]
mariadb                                                MariaDB is a community-developed fork of MyS…   2775                [OK]
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   607                                     [OK]
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       192                                     [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          113
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   101                                     [OK]
centurylink/mysql                                      Image containing mysql. Optimized to be link…   60                                      [OK]
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   53
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. Cr…   44
tutum/mysql                                            Base docker image to run a MySQL database se…   32
zabbix/zabbix-web-apache-mysql                         Zabbix frontend based on Apache web-server w…   29                                      [OK]
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic backup…   27                                      [OK]
bitnami/mysql                                          Bitnami MySQL Docker Image                      26                                      [OK]
zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        22                                      [OK]
linuxserver/mysql                                      A Mysql container, brought to you by LinuxSe…   20
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   13
mysql/mysql-router                                     MySQL Router provides transparent routing be…   11
circleci/mysql                                         MySQL is a widely used, open-source relation…   11
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 image…   6
dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                6                                       [OK]
jelastic/mysql                                         An image of the MySQL database server mainta…   1
cloudposse/mysql                                       Improved `mysql` service with support for `m…   0                                       [OK]
widdpim/mysql-client                                   Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                                       [OK]

还需要进一步查看mysql镜像有哪些版本

$ docker-tags mysql
IMAGE [mysql:]:
mysql:latest
mysql:5
mysql:5.5.40
mysql:5.5.41
mysql:5.5.42
mysql:5.5.43
mysql:5.5.44
mysql:5.5.45
mysql:5.5.46
mysql:5.5.47
mysql:5.5.48
mysql:5.5.49
mysql:5.5.50
mysql:5.5.51
mysql:5.5.52
mysql:5.5.53
mysql:5.5.54
mysql:5.5.55
mysql:5.5.56
mysql:5.5.57
mysql:5.5.58
mysql:5.5.59
mysql:5.5.60
mysql:5.5.61
mysql:5.5.62
mysql:5.6
mysql:5.6.17
mysql:5.6.20
mysql:5.6.21
mysql:5.6.22
mysql:5.6.23
mysql:5.6.24
mysql:5.6.25
mysql:5.6.26
mysql:5.6.27
mysql:5.6.28
mysql:5.6.29
mysql:5.6.30
mysql:5.6.31
mysql:5.6.32
mysql:5.6.33
mysql:5.6.34
mysql:5.6.35
mysql:5.6.36
mysql:5.6.37
mysql:5.6.38
mysql:5.6.39
mysql:5.6.40
mysql:5.6.41
mysql:5.6.42
mysql:5.6.43
mysql:5.6.44
mysql:5.7
mysql:5.7.10
mysql:5.7.11
mysql:5.7.12
mysql:5.7.13
mysql:5.7.14
mysql:5.7.15
mysql:5.7.16
mysql:5.7.17
mysql:5.7.18
mysql:5.7.19
mysql:5.7.20
mysql:5.7.21
mysql:5.7.22
mysql:5.7.23
mysql:5.7.24
mysql:5.7.25
mysql:5.7.26
mysql:5.7.4
mysql:5.7.4-m14
mysql:5.7.5
mysql:5.7.5-m15
mysql:5.7.6
mysql:5.7.6-m16
mysql:5.7.7
mysql:5.7.7-rc
mysql:5.7.8
mysql:5.7.8-rc
mysql:5.7.9
mysql:8
mysql:8.0
mysql:8.0.0
mysql:8.0.1
mysql:8.0.11
mysql:8.0.12
mysql:8.0.13
mysql:8.0.14
mysql:8.0.15
mysql:8.0.16
mysql:8.0.2
mysql:8.0.3
mysql:8.0.4
mysql:8.0.4-rc

备注:这里的docker-tags是自己写的脚本,附脚本内容:

#!/bin/bash

API="https://registry.hub.docker.com/v1/repositories"
DEFAULT_NAME="nginx"
DEFAULT_TIMEOUT=3

function Usage(){
cat << HELP

Usage: docker-tags NAME[:TAG]

docker-tags list all tags for docker image on a remote registry.

Example:
    docker-tags (default nginx)
    docker-tags nginx
    docker-tags nginx:1.15.8
    docker search nginx | docker-tags
    docker search nginx | docker-tags :1.15.8
    echo nginx | docker-tags
    echo nginx | docker-tags :1.15.8
HELP
}

ARG=$1
if [[ "$ARG" =~ "-h" ]];then
    Usage
    exit 0
fi

function ParseJson(){
    tr -d '[\[\]" ]' | tr '}' '\n' | awk -F: -v image=$1 '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'
}

function GetTags(){
    image=$1
    tag=$2
    ret=`curl -s ${API}/${image}/tags`
    tag_list=`echo $ret | ParseJson ${image}`
    if [ -z "$tag" ];then
        echo -e "$tag_list"
    else
        echo -e "$tag_list" | grep -w "$tag"
    fi
}

if [ -z $ARG ] || [[ ${ARG:0:1} == ":" ]];then
    if [ -x /usr/bin/timeout ];then
        images=`timeout $DEFAULT_TIMEOUT` awk '{print $1}' | grep -v "NAME" || echo $DEFAULT_NAME
    else
        images=`awk '{print $1}' | grep -v "NAME"`
    fi
else
    images=`echo $ARG | awk -F: '{print $1}'`
fi
tag=`echo $ARG | awk -F: '{print $2}'`

for i in ${images}
do
    tags=`GetTags $i $tag`
    count=`echo $tags | wc -w`
    if [[ $count -gt 0 ]];then
        echo -e "IMAGE [$i:$tag]:"
        echo -e "$tags"
        echo
    fi
done

另外如果不使用命令的方式查看tag的话也可以打开浏览器,访问如下网址https://hub.docker.com/_/mysql?tab=tags进行获取

安装自己需要的版本

docker pull mysql:5.7

docker run -p 3317:3306 --name mysql57 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

说明:
-p 3316:3306 是把容器的3306端口映射到本机的3316端口
–name 是给运行的容器一个别名
-e MYSQL_ROOT_PASSWORD=123456 是初始化 MySQL 的密码

#测试


mysql -h127.0.0.1  -P 3317 -uroot -p123456

目前还不能成功执行,需要进入容器设置权限


docker exec -it mysql57 bash

进入容器后执行

# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql>update mysql.user set host="%" where user="root";
mysql> flush privileges;


OK,这样我们退出容器,在宿主机上执行mysql -h127.0.0.1 -P 3317 -uroot -p123456就没有问题了。

进阶待续

挂载外部配置和数据目录

参考文章:
https://blog.csdn.net/u014542256/article/details/84327903
https://blog.csdn.net/woniu211111/article/details/80968154

你可能感兴趣的:(docker,linux,mysql)