docker快速搭建数据库测试环境
背景: 不仅仅是开发人员,对于DBA来说,重复的安装部署也是烦的一笔。还好有docker这样的神器,免去了很多安装部署的烦恼。
三条命令快速安装mysql数据库:
前提是安装好docker并且把docker hub配置为国内阿里云。参考:https://blog.csdn.net/u010033674/article/details/105345211
1)、搜索mysql官方镜像
docker search mysql
2)、获取对应版本镜像
docker pull mysql:8.0.19
版本可以自己指定,也是可以mysql:5.7.25
3)、启动mysql容器
docker run --name mysql8.0.19 --restart=always -p 13306:3306 -e MYSQL_ROOT_PASSWORD=1234.C0m -d mysql:8.0.19
解释:–name mysql8.0.19 #指定容器名字为mysql8.0.19
–restart=always #容器策略,失败后就重启
-p 13306:3306 #容器的端口3306映射到宿主机端口13306
-d mysql:8.0.19 #容器后台运行,镜像是刚才pull下来的mysql:8.0.19
4)、访问mysql数据库
宿主机访问:
[aaa@qq.com ~]# mysql -h127.0.0.1 -uroot -p1234.C0m -P13306
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 8
Server version: 8.0.19 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>
容器内访问:
[aaa@qq.com ~]# docker exec -it mysql8.0.19 bash
aaa@qq.com:/# mysql -uroot -p1234.C0m
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 9
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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容器
docker run -itd --name mysql01 \
--hostname mysql01 \
--network mynet \
--ip 192.168.0.10 \
--restart=always \
-v /data/mysql01/data:/var/lib/mysql \
-v /data/mysql01/conf:/etc/mysql \
-p 3309:3306 \
-e MYSQL_ROOT_PASSWORD=1234.C0m \
mysql:8.0.20
命令说明:
–name mysql01 #指定容器名
–hostname mysql01 #指定容器主机名
–network mynet #指定自己创建的docker网络
–ip 192.168.0.10 #从docker网络中为容器固定IP
–restart=always #重启策略
-v /data/mysql01/data:/var/lib/mysql #把mysql 的数据文件目录映射到宿主机/data/xxx目录下
-v /data/mysql01/conf:/etc/mysql #同上,把容器中mysql的配置文件映射到宿主机目录,可以把自己的my.cnf配置文件提前放进宿主机对应目录下
-p 3309:3306 #映射端口
-e MYSQL_ROOT_PASSWORD=1234.C0m #设置root密码
mysql:8.0.20 #镜像名