目录

1,安装

2,选择镜像

3,下载镜像。

4,准备部署

5,创建并启动

6,测试


使用docker容器部署mysql5.7

1,安装

没有docker,首先安装docker,参照Docker简单实践(一)安装Docker

2,选择镜像

远程搜索docker hub上的mysql镜像。

[root@ecs-7bc6-0001 mysql]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8397                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   2886                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   625                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   438                 [OK]                
centurylink/mysql                 Image containing mysql. Optimized to be link…   60                                      [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   59                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   47                                      
deitch/mysql-backup               Automated and scheduled mysql database dumps…   39                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   32                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   28                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      27                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   21                                      
prom/mysqld-exporter                                                              20                                      [OK]
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   15                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   13                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   12                                      
arey/mysql-client                 Run a MySQL client from a docker container      10                                      [OK]
yloeffler/mysql-backup            This image runs mysqldump to backup data usi…   6                                       [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   4                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
monasca/mysql-init                A minimal decoupled init container for mysql    0                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                0                                       [OK]

3,下载镜像。

在docker hub上查看镜像版本,选择要安装的镜像版本。下载docker镜像,本次示例部署mysql5.7,如下:

[root@ecs-7bc6-0001 mysql]# docker pull mysql:5.7

4,准备部署

本地系统创建部署目录,进入部署目录

创建conf,data,logs目录分别用来存放mysql配置,mysql数据,mysql日志

mkdir conf
mkdir data
mkdir logs

进入conf目录,写入mysql配置如下,mysql配置可根据需要自定义,下文的配置只是一个示例。

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

lower_case_table_names=1

5,创建并启动

退出conf目录,使用如下命令创建并启动mysql容器。

[root@ecs-7bc6-0001 mysql]# docker run -itd --name="mysql" -p3307:3306 -v$PWD/conf/:/etc/mysql/conf.d -v$PWD/data:/var/lib/mysql -v$PWD/logs:/var/log -eMYSQL_ROOT_PASSWORD="mysql"  mysql:5.7

其中命令各部分的意义分别是:

  • docker run:创建并启动应用
  • -i:开启容器的标准输入STDIN
  • -t:容器分配伪终端,用于再终端登录容器
  • -d:后台运行容器
  • --name:容器名称
  • -p3307:3306:打开容器的3306端口并且映射到本机的3307端口
  • -v$PWD/conf/:/etc/mysql/conf.d,挂载($PWD)当前目录下的conf目录到容器的/etc/mysql/conf.d目录
  • v$PWD/data:/var/lib/mysql,挂载($PWD)当前目录下的data目录到容器的/var/lib/mysql目录
  •  -v$PWD/logs:/var/log,挂载($PWD)当前目录下的logs目录到容器的/var/log/目录
  • -eMYSQL_ROOT_PASSWORD="mysql",增加环境变量MYSQL_ROOT_PASSWORD,mysql容器创建并启动时,设置mysql的登录密码
  • mysql:5.7,要创建并启动容器的mysql镜像

命令执行后

[root@ecs-7bc6-0001 mysql]# docker run -itd --name="mysql" -p3307:3306 -v$PWD/conf/:/etc/mysql/conf.d -v$PWD/data:/var/lib/mysql -v$PWD/logs:/var/log -eMYSQL_ROOT_PASSWORD="mysql"  mysql:5.7
e28d933300235197ade2b492f40fc1354c14e9a73d2a56615ddae49886ae5554

查看容器信息

[root@ecs-7bc6-0001 mysql]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
e28d93330023        mysql:5.7           "docker-entrypoint.s…"   46 seconds ago      Up 44 seconds       33060/tcp, 0.0.0.0:3307->3306/tcp   mysql

发现容器已经启动成功

6,测试

进入容器终端测试mysql,并进行远程登录权限管理

[root@ecs-7bc6-0001 mysql]# docker exec -it e28d93330023 /bin/bash

其中e28d93330023为容器ID,查看容器信息可以获得。

执行命令后进入容器终端

执行mysql -uroot -p命令后输入启动容器时设置的mysql密码,我的密码是mysql,连接mysql数据库成功。

mysql远程登录权限管理

mysql> grant all privileges on *.* to 'root'@'%' identified by 'mysql'
    -> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

退出mysql客户端,查看容器的ip地址

root@e28d93330023:/# cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.2	e28d93330023

或者通过 docker container inspect命令查看容器的详细信息

[root@ecs-7bc6-0001 data]# docker container inspect e28d93330023| grep IP
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
                    "IPAMConfig": null,
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,

退出容器

使用容器的IP地址再宿主机登录方才启动的mysqlServer。

[root@ecs-7bc6-0001 mysql]# mysql -h172.17.0.2 -P3306 -uroot -p
Enter password: 
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, 2018, 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>

登录成功,docker部署mysql完成。

查看部署目录下的data目录,生成了mysql的数据。

[root@ecs-7bc6-0001 mysql]# cd data
[root@ecs-7bc6-0001 data]# ls
auto.cnf  ca-key.pem  ca.pem  client-cert.pem  client-key.pem  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  mysql  performance_schema  private_key.pem  public_key.pem  server-cert.pem  server-key.pem  sys

容器停止并且删除后,使用mysql:5.7镜像重新根据上文启动容器,原来产生的mysql数据依然保存完好。

部署完成。

补充:MySQL镜像环境变量

环境变量备注
MYSQL_ROOT_PASSWORD此环境变量是必须的,mysql的root超级用户的初始化密码。
MYSQL_DATABASE此环境变量是可选的,指定容器在创建时要创建的数据库,如果容器创建命令同时指定了用户名/密码,则该用户会被授予该数据库的超级访问权限。
MYSQL_USER,MYSQL_PASSWORD变量可选,用于指定容器在创建时要创建的用户名密码,如果同时指定了数据库,会授予该用户指定数据库的超级访问权限。
MYSQL_ALLOW_EMPTY_PASSWORD可选环境变量,指定mysql初始化root用户可以不需要密码就可以登录。
MYSQL_RANDOM_ROOT_PASSWORD可选环境变量,指定mysql初始化root用户使用随机密码。
MYSQL_ONETIME_PASSWORD可选环境变量,指定root在首次登录时强制修改密码。注意不是MYSQL_USER指定的用户。

(完。)(^_^)

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐