环境说明
Docker | 18.09.6 |
Mysql | 5.7 |
安装
1. 拉取mysql镜像
[root@localhost ~]# docker pull mysql:5.7.21
5.7.21: Pulling from library/mysql
2a72cbf407d6: Pull complete
38680a9b47a8: Pull complete
4c732aa0eb1b: Pull complete
c5317a34eddd: Pull complete
f92be680366c: Pull complete
e8ecd8bec5ab: Pull complete
2a650284a6a8: Pull complete
5b5108d08c6d: Pull complete
beaff1261757: Pull complete
c1a55c6375b5: Pull complete
8181cde51c65: Pull complete
Digest: sha256:691c55aabb3c4e3b89b953dd2f022f7ea845e5443954767d321d5f5fa394e28c
Status: Downloaded newer image for mysql:5.7.21
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.21 5195076672a7 14 months ago 371MB
2. 创建container
[root@localhost ~]# docker run --name mysql_master -p 3301:3306 -d mysql:5.7.21
fd732d08d0914f1a3c7623c31e54f3902eb6ad8df2469b40c942bacf48b2b31e
如上container并未启动成功,原因:查看logs
[root@localhost ~]# docker logs mysql:5.7.21
Error: No such container: mysql:5.7.21
[root@localhost ~]# docker logs mysql_master
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
指定环境变量:MYSQL_ROOT_PASSWORD,重新创建container
[root@localhost ~]# docker run --name mysql_master -p 3301:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21
01d8b16246ec93be2bf723b8ce7c71039c46826310bd1920dc48cc9c6e8feea4
//通过镜像 mysql:5.7.21 启动一个名为 mysql_master 的 MySQL 服务器,端口号是3306,映射的宿主机端口号是3301,root 账号密码是123456,-d 后台运行容器,并返回容器ID
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01d8b16246ec mysql:5.7.21 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:3301->3306/tcp mysql_master
测试连接成功