docker 安装 MySQL 8.0
1.查找镜像
查找Docker Hub上的mysql镜像
X230:~$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 7904 [OK]
mariadb MariaDB is a community-developed fork of MyS… 2632 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 595 [OK]
zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 173 [OK]
2.pull镜像
拉取官方镜像文件:
X230:~$ docker pull mysql:8.0
查看镜像文件:
X230:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0 91dadee7afee 6 days ago 477MB
3.使用镜像
创建目录:
X230:~$ mkdir -p ~/Project/Docker/mysql/data ~/Project/Docker/mysql/log ~/Project/Docker/mysql/conf
X230:~$ cd ~/Project/Docker/mysql/
X230:~/Project/Docker/mysql$ ls
conf data log
运行容器:
X230:~/Project/Docker/mysql$ docker run -p 3306:3306 --name mysql8 -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=1234 -d mysql:8.0
命令说明:
- -p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。
- -v -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。
- -v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。
- -v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。
- -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
查看容器启动状态
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f65a9dd48af mysql:8.0 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql8
测试连接数据库:
X230:~/Project/Docker/mysql$ mysql -h127.0.0.1 -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.15 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>
进入容器
X230:~/Project/Docker/mysql$ docker exec -it mysql8 /bin/bash
root@7f65a9dd48af:/#
启动/停止容器
启用:
docker start mysql8
停止:
docker stop mysql8
删除容器与镜像
删除容器:
docker rm mysql8
删除镜像:
docker rmi mysql:8.0