一、使用背景
我们在使用docker拉取mysql命令时,数据库服务器,网络未开通外网,拉取镜像失败
但是我们还是想用docker部署则可以通过以下方式获取
前提:环境网络通可以pull mysql镜像
[root@VM-20-10-centos opt]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae0a0e631153 mysql:5.7.37 "docker-entrypoint.s…" 9 hours ago Up About an hour 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@VM-20-10-centos opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 7ce93a845a8a 4 weeks ago 586MB
mysql 5.7.37 82d2d47667cf 2 years ago 450MB
[root@VM-20-10-centos opt]# docker save -o 1.tar mysql:5.7.37
说明:其中 docker save -o 1.tar mysql:5.7.37 来自IMAGE
1、先保存镜像为tar文件
docker save -o 1.tar mysql:5.7.37
2、从tar文件加载镜像、上传至目标服务器
docker load -i 1.tar
[root@VM-20-10-centos backup]# docker load -i 1.tar
0ad3ddf4a4ce: Loading layer [==================================================>] 72.54MB/72.54MB
894a6e83a96f: Loading layer [==================================================>] 338.4kB/338.4kB
e9303ab783fe: Loading layer [==================================================>] 9.557MB/9.557MB
96759eecc124: Loading layer [==================================================>] 4.087MB/4.087MB
b4dac6cae747: Loading layer [==================================================>] 2.048kB/2.048kB
765a4b7dea37: Loading layer [==================================================>] 55.16MB/55.16MB
1b09b37f4930: Loading layer [==================================================>] 6.656kB/6.656kB
ae74b19e2947: Loading layer [==================================================>] 3.584kB/3.584kB
9172c7574e83: Loading layer [==================================================>] 313.2MB/313.2MB
79e0c651efb2: Loading layer [==================================================>] 16.9kB/16.9kB
a1f00f3b2b70: Loading layer [==================================================>] 1.536kB/1.536kB
Loaded image: mysql:5.7.37
3、初始化mysql
sudo docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7.37
说明:其中root至的是密码、mysql: 5.7.37拉去取镜像版本
4、执行结果
[root@VM-20-10-centos backup]# sudo docker run -p 3306:3306 --name mysql \
> -v /mydata/mysql/log:/var/log/mysql \
> -v /mydata/mysql/data:/var/lib/mysql \
> -v /mydata/mysql/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7.37
74f1da1fcf877a835e47afd248a05ad08a3ce39b94b8156365e0dcf790edf716
[root@VM-20-10-centos backup]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74f1da1fcf87 mysql:5.7.37 "docker-entrypoint.s…" 10 seconds ago Up 10 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
二、FAQ拉取镜像密码
-e MYSQL_ROOT_PASSWORD=root \ 未生效 使用root登陆未生效,从其他服务器拉取的mysql镜像密码是123456使用123456登陆msyql成功进入
mysql> exit
Bye
root@74f1da1fcf87:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
root@74f1da1fcf87:/# mysql -uroot -p123456
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 6
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>
好了,至于为什么不重要 重新修改个密码吧
还有的小伙伴甚至docker都下载失败看过来
这位博主已经踩好坑进来坐