天天看点

docker修复MySQL踩坑记录

上周实验室有几台服务器中了勒索病毒,然后领导安排专人断了实验室网络,把每台服务器扫描杀毒,结果导致我开发的网站MySQL无法启动,

查看docker日志:docker container logs ci-mysql

[[email protected]_cicd ~]# docker container logs ci-mysql
           

报错如下:

docker-entrypoint.sh: line 72: mysqld: command not found,感觉应该是container被损坏了,要重新从备份导出一份过来。

docker修复MySQL踩坑记录

排查发现扫毒还把MySQL的持久化数据库删除了,还好我有备份,不幸中的万幸,啊啊啊啊啊啊!

今天来公司加班恢复数据库,先把原来的损坏的image和container删除,记得先删除container再删除image

(下文具体的命令参数请参考docker中文文档网址:http://www.dockerinfo.net/document)

[[email protected]_cicd ~]# docker container ls -a
[[email protected]_cicd ~]# docker container rm c9e6b46a93b0

[[email protected]_cicd ~]# docker images
[[email protected]_cicd ~]# docker rmi fd9439c232ff
           

先从另一个docker导出一份镜像(记得从image导出,别从container导出)

我最开始从容器导出的备份,步骤如下:

最开始的方法是打算从container导出容器快照:
停掉container再导出,
[[email protected]_cicd ~]# docker ps
[[email protected]_cicd ~]# docker container stop 131942a66b1c 
[[email protected]_cicd ~]# docker container ls -a
[[email protected]_cicd ~]# docker export 131942a66b1c > mysql12.tar

压缩包传输到服务器指定目录(IP是你自己的):
[[email protected]_cicd ~]# scp mysql12.tar [email protected]:/root/mysql/

从容器快照导入image:
[[email protected]_cicd ~]# cd /root/mysql
[[email protected]_cicd mysql]# cat mysql12.tar | docker import - mysql:5.7
查看导入的image:
[[email protected]_cicd mysql]# docker images

从image启动container(密码我瞎写的,记得改成复杂密码):
[[email protected]_cicd mysql]# docker run -d -p 3306:3306 --name ci-mysql --restart unless-stopped -v "/srv/mysql/cloud/data:/var/lib/mysql" -e MYSQL_ROOT_PASSWORD="123456" -e PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" mysql:5.7 "docker-entrypoint.sh mysqld"
           

一直报错:

starting container process caused "exec: \"docker-entrypoint.sh mysqld\": executable file not found in $PATH": unknown.

starting container process caused "exec: \"docker-entrypoint.sh mysqld\": executable file not found in $PATH": unknown.
           

想了很多招儿没法子,后来我觉得可能是从容器导出的镜像丢掉了某些配置,导致找不到环境变量中的文件,我又从docker images里面导出了一个备份,具体命令如下:

从docker image导出image压缩包:
[[email protected]_cicd ~]# docker images
[[email protected]_cicd ~]# docker save -o mysql1234.tar mysql:5.7

压缩包传输到服务器指定目录:
[[email protected]_cicd ~]# scp mysql1234.tar [email protected]:/root/mysql/

两个压缩包的大小也不一样,后面这个包有420M,稍微大一点。
[[email protected]_cicd ~]# cd /root/mysql
[[email protected]_cicd mysql]# ll -h
total 834M
-rwx--x--x 1 root root 420M Nov 28 15:58 mysql1234.tar
-rwxr-xr-x 1 root root 414M Nov 28 15:44 mysql12.tar

导入image:
[[email protected]_cicd mysql]# docker load < mysql1234.tar
查看导入的image:
[[email protected]_cicd mysql]# docker images

从image启动container:
[[email protected]_cicd mysql]# docker run -d -p 3306:3306 --name ci-mysql --restart unless-stopped -v "/srv/mysql/cloud/data:/var/lib/mysql" -e MYSQL_ROOT_PASSWORD="123456" -e PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" mysql:5.7
           

container启动OK,再配置一下远程访问MySQL就搞定:

进入container:
[[email protected]_cicd mysql]# docker container exec -it a3fc1cabb6f6 bash
[email protected]:/# mysql -u root -p

查看mysql用户详情
mysql> select host,user,plugin,authentication_string from mysql.user;

修改权限
mysql> grant all privileges on *.* to [email protected]"%" identified by "123456" with grant option;
mysql> flush privileges;
mysql> exit
[email protected]:/# exit
           

用HeidiSQL工具远程访问MySQL测试了一下,连接成功。