Docker环境 安装 运行 Mysql
1.拉取mysql镜像
[root@localhost jzj]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
b4d181a07f80: Pull complete
a462b60610f5: Pull complete
578fafb77ab8: Pull complete
524046006037: Pull complete
d0cbe54c8855: Pull complete
aa18e05cc46d: Pull complete
32ca814c833f: Pull complete
9ecc8abdb7f5: Pull complete
ad042b682e0f: Pull complete
71d327c6bb78: Pull complete
165d1d10a3fa: Pull complete
2f40c47d0626: Pull complete
Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
2.查看镜像
[root@localhost jzj]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 5c62e459e087 13 days ago 556MB
3.查看镜像配置目录
可以看到 mysql的镜像仓库信息
[root@localhost image]# cat /var/lib/docker/image/overlay2/repositories.json
{"Repositories":{"mysql":{"mysql:latest":"sha256:5c62e459e087e3bd3d963092b58e50ae2af881076b43c29e38e2b5db253e0287","mysql@sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b":"sha256:5c62e459e087e3bd3d963092b58e50ae2af881076b43c29e38e2b5db253e0287"}}}
4.运行mysql docker容器
我这里指定版本 5.6 然后去下载
//创建容器时,最后mysql:5.6表示mysql镜像的版本,可以写,表示指定该版本;如果不写也可以,docker会自动在本地检测,没有会更新
[root@localhost image]# docker run -p 3306:3306 --name jzj-mysql -v ~/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6
Unable to find image 'mysql:5.6' locally
5.6: Pulling from library/mysql
aed007321795: Pull complete
3e3907de0616: Pull complete
e2ca0dc4c85b: Pull complete
2dfa7dcb610e: Pull complete
ca864d5ff496: Pull complete
50ae77af12a2: Pull complete
9bd9276345f7: Pull complete
1d2d48bdf8e3: Pull complete
ff8b3b343d10: Pull complete
a3efd105a920: Pull complete
7d4864c82f8a: Pull complete
Digest: sha256:879efe96447702f18945f10607e736b844ebe9cfef6f29c7a47f6b972212a81e
Status: Downloaded newer image for mysql:5.6
d999f96fcc19b403b325ae4cde1e478a196442e627f784a9cddb7f84c8076739
[root@localhost image]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 e1aa75e199d7 13 days ago 303MB
mysql latest 5c62e459e087 13 days ago 556MB
[root@localhost image]#
5.查看mysql 启动状态
[root@localhost image]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d999f96fcc19 mysql:5.6 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp jzj-mysql
[root@localhost image
6.进入docker 容器,执行数据库命令
进入容器 docker exec -it xxxxxx bash
执行mysql命令 mysql -u root -p
[root@localhost jzj]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d999f96fcc19 mysql:5.6 "docker-entrypoint.s…" 36 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp jzj-mysql
[root@localhost jzj]#
[root@localhost jzj]# docker exec -it jzj-mysql bash
root@d999f96fcc19:/#
root@d999f96fcc19:/# mysql -u root -p
Enter password:
xxx
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.51 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>