【Docker了解下】(三) —— 使用Docker搭建MySQL单机服务

系统环境

  • 内核版本
[root@ ~]# uname -a
Linux iZm5ee0h5tkrzzli5jd8z5Z 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
  • 系统版本
[root@ ~]# lsb_release -a
LSB Version:	:core-4.1-amd64:core-4.1-noarch
Distributor ID:	CentOS
Description:	CentOS Linux release 7.4.1708 (Core) 
Release:		7.4.1708
Codename:		Core
  • Docker版本
[root@ ~]# docker version
Client:
 Version:           18.06.1-ce-rc1
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        0928140
 Built:             Wed Aug  8 01:35:58 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce-rc1
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       0928140
  Built:            Wed Aug  8 01:37:27 2018
  OS/Arch:          linux/amd64
  Experimental:     false

搭建MySQL

一、选取可用MySQL镜像

查看可用镜像有两种方式

1、直接在Docker的 官方镜像仓库 搜索可用的镜像(推荐:因为可以看到可用的版本tag,方便选取不同版本)。

2、使用Docker指令搜索可用的镜像。

[root@ ~]# docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   6728                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   2141                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   495                                     [OK]
percona                                                Percona Server is a fork of the MySQL relati…   360                 [OK]                
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       114                                     [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          93                                      
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   62                                      [OK]
centurylink/mysql                                      Image containing mysql. Optimized to be link…   60                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          43                                      [OK]
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   35                                      
mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. Cr…   33                                      
tutum/mysql                                            Base docker image to run a MySQL database se…   32                                      
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic backup…   20                                      [OK]
bitnami/mysql                                          Bitnami MySQL Docker Image                      16                                      [OK]
zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        15                                      [OK]
linuxserver/mysql                                      A Mysql container, brought to you by LinuxSe…   14                                      
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   9                                       
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
circleci/mysql                                         MySQL is a widely used, open-source relation…   6                                       
dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                4                                       [OK]
mysql/mysql-router                                     MySQL Router provides transparent routing be…   2                                       
openzipkin/zipkin-mysql                                Mirror of https://quay.io/repository/openzip…   1                                       
ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                                       [OK]
cloudfoundry/cf-mysql-ci                               Image used in CI of cf-mysql-release            0                                       
cloudposse/mysql                                       Improved `mysql` service with support for `m…   0                                       [OK]

二、拉取镜像

tag表示MySQL不同版本的标签。

$ docker pull mysql:tag
[root@ ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ae821421a7d: Pull complete 
a9e976e3aa6d: Pull complete 
e3735e44a020: Pull complete 
bfd564e9483f: Pull complete 
df705f26e488: Pull complete 
0c5547f73d62: Pull complete 
f437382cf8a1: Pull complete 
4f22805bb6d6: Pull complete 
394f0f652697: Pull complete 
fb068b9b9d1f: Pull complete 
fdfc96ad0937: Pull complete 
Digest: sha256:8c15b2612051244d0a2b6ceb6f9bf82ddc0e909555c1067c098e5f935e2751a7
Status: Downloaded newer image for mysql:5.7

拉取完成后,通过docker images命令查看镜像目录,如下表示下载成功

[root@ ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 e47e309f72c8        13 days ago         372MB

三、启动MySQL服务实例

$ docker run --name some-mysql -p host-port:docker-port -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

–name some-mysql:表示给这个容器命名为some-mysql;
-p host-port:docker-port:表示把主机的端口映射到容器的端口
-e MYSQL_ROOT_PASSWORD=my-secret-pw:表示设置root用户的密码为my-secret-pw;
-d 表示在后台运行容器;
tag表示MySQL的版本;

[root@iZm5ee0h5tkrzzli5jd8z5Z ~]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

接着你可以使用docker container ls -adocker ps -a查看启动的实例。

[root@iZm5ee0h5tkrzzli5jd8z5Z ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
3b78098cb843        mysql:5.7           "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

这样一来再设置好阿里云的安全组就可以使用Navicat链接和操作容器里的MySQL里。

你可能感兴趣的:(Docker)