经常遇到开发的同学说搭建MySQL环境失败的问题。今天给大家介绍一种快速搭建MySQL环境的方法。可供大家测试用。
本次测试基于Centos7 + docker的环境。
1.docker安装
yum -y install docker
service start docker
2.修改docker的默认镜像仓库
建议将/etc/docker/daemon.json 改为:{"registry-mirrors": ["https://registry.docker-cn.com"]}
3.拉取MySQL5.5版本
#拉取mysql5.5
docker pull mysql:5.5
#启动 初始化
docker run --name mysql -d -it -p 3306:3306 -e MYSQL_ROOT_PASSWORD=xxx mysql:5.5
#进入docker下mysql5.5容器
docker exec -it mysql bash
#停止MySQL5.5容器
docker stop mysql
4.拉取MySQL8.0版本
#拉取mysql8
docker pull mysql:8.0
# 启动,初始化
docker run --name mysql8 -d -it -p 4306:4306 -e MYSQL_ROOT_PASSWORD=xxx mysql:8.0
#进入docker下mysql8容器
docker exec -it mysql8 bash
# 停止mysql8容器
docker stop mysql8
附录:docker常用操作
- List currently running docker containers:
docker ps
- List all docker containers (running and stopped):
docker ps -a
- Start a container from an image, with a custom name:
docker run --name container_name image
- Start or stop an existing container:
docker start|stop container_name
- Pull an image from a docker registry:
docker pull image
- Open a shell inside of an already running container:
docker exec -it container_name sh
- Remove a stopped container:
docker rm container_name
- Fetch and follow the logs of a container:
docker logs -f container_name