Docker学习三 Docker安装Mysql

Docker安装Mysql

前言

mysql数据库是后端开发经常用到的,而且有时候还需要装多个版本,docker进行安装就会十分省事。

查看docker镜像

[docker@gxp5 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB

docker镜像中只有安装时跑的hello-world

查看mysql镜像版本

[docker@gxp5 ~]$ docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9520                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3456                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   698                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   75                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   69                                      

从OFFICIAL列标记为[OK]的行可以看出是官方提供的,截取前几个高星的镜像,可以看出mysql以及他的分支mariadb的星数最高。

下载镜像

下载最新版本的镜像,不打Tag默认为latest

[docker@gxp5 ~]$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
...漫长的下载过程

下载指定版本镜像,以5.7为例,镜像可以在官方镜像上查找 https://hub.docker.com/_/mysql?tab=tags

[docker@gxp5 ~]$ docker pull mysql:5.7
5.7: Pulling from library/mysql
...漫长的下载过程

ps: 下载太慢了,通过国内镜像加速吧,参考这篇文章

下载完毕后查看镜像

[root@gxp5 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 413be204e9c3        7 weeks ago         456MB
mysql               latest              9228ee8bac7a        7 weeks ago         547MB
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB

运行mysql

docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

查看运行实例

[docker@gxp5 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
2b44b9ca3cb9        mysql               "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-test

在windows上去连接该实例 – 发现mysql版本是mysql 8.0.19

PS C:\Program Files\MySQL\MySQL Server 8.0\bin> .\mysql.exe mysql -h 112.168.136.130 -P 3306 -u root -p                 Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 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> show engines;

这样一看,最新版本的mysql实例就运行起来了,但是似乎并不合理,我们没法配置其文件,数据也不知道存储在哪儿!

接下来就用mysql5.7进行展示。

  1. 仅修改端口
docker run -itd --name mysql-5.7 -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

在windows上可以看到结果 – 实际版本为5.7

PS C:\Program Files\MySQL\MySQL Server 8.0\bin> .\mysql.exe mysql -h 112.168.136.130 -P 3308 -u root -p                 Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 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>                                                                       
  1. 全面修改(配置文件,数据存放)

    查看镜像中配置文件的位置,并按顺序检查具体读了哪个文件(当然最后挂载也可以直接为最优先的):

[root@gxp5 ~]# docker exec -it mysql-5.7 bash
root@bcede734e65d:/# mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf 
stop,rm掉刚建立的mysql-5.7
[docker@gxp5 ~]$ docker stop mysql-5.7 
mysql-5.7
[docker@gxp5 ~]$ docker rm mysql-5.7 
mysql-5.7

​ 创建本地准备挂载的配置文件文件夹以及文件

mkdir -p ~/docker/mysql/conf && mkdir -p ~/docker/mysql/data

​ 在其配置文件my.cnf中指定端口

[mysqld]
port=3307

​ 绑定挂载配置文件

docker run --name mysql5.7-server \
-p 3307:3307 -e MYSQL_ROOT_PASSWORD=123456 \
--mount type=bind,src=/home/docker/docker/mysql/conf/my.cnf,dst=/etc/mysql/my.cnf \
--mount type=bind,src=/home/docker/docker/mysql/data,dst=/var/lib/mysql \
--restart=on-failure:3 \
-d mysql:5.7

​ 同样经过docker ps 检查其已经启动后,通过mysql客户端进行连接。

PS C:\Program Files\MySQL\MySQL Server 8.0\bin> .\mysql.exe mysql -h 112.168.136.130 -P 3307 -u root -p                 Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 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>                                             

​ 另外,检查data文件夹,是否真的产生了数据:

[docker@gxp5 data]$ pwd
/home/docker/docker/mysql/data
[docker@gxp5 data]$ ls
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql               private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema  public_key.pem   server-key.pem

这样一个可以在docker外配置文件,查看数据文件夹的mysql就创建好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值