前言
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为例,镜像可以在官方镜像上查找
[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进行展示。
- 仅修改端口
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>
- 全面修改(配置文件,数据存放)
查看镜像中配置文件的位置,并按顺序检查具体读了哪个文件(当然最后挂载也可以直接为最优先的):
[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就创建好了。