共计 7013 个字符,预计需要花费 18 分钟才能阅读完成。
这篇文章主要介绍了 Mysql 容器启动失败恢复的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让丸趣 TV 小编带着大家一起了解一下。
Mysql 容器启动失败恢复案例
昨天重启完 NAS 后,今天早上发现 NAS 又死机了。只能再次强制关机,重启。
启动 docker 容器之前,我把 mysql 容器的内存调整了一下,调整成了 512M。然后就悲剧的发现启动不了了。后来发现,启动不了不是我调内存的关系。
查看日志,显示如下:
2020-12-27T02:43:53.375776Z 0 mysqld: [Warning] World-writable config file /etc/mysql/my.cnf is ignored.
2020-12-27T02:43:53.375776Z 0 mysqld: Error on realpath() on /var/lib/mysql-files (Error 2 – No such file or directory)
2020-12-27T02:43:53.375776Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for –secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2020-12-27T02:43:53.376005Z 0 [ERROR] [MY-010119] [Server] Aborting
百度了下,解决办法是重新 run 一个 mysql 容器,并且在之前的配置上加上 -v /mnt/md0/User/wzp/home/www/mysql-files:/var/lib/mysql-files/。
现在问题来了,之前 run mysql 容器时,我并没有记录下完整的 run 命令。还好,通过 rekcod 工具可以查看。更喜的是,这个工具可以通过 docker 来运行。因为我的 NAS 的系统不是发行版的 linux,npm,yum 好多工具都没法安装。
关于 rekcod 的使用可以参考 linuxea: 如何复现查看 docker run 参数命令
简单来说,用法如下:
1 docker 安装 rekcod
$ docker pull nexdrew/rekcod
$ alias rekcod= docker run --rm -v /var/run/docker.sock:/var/run/docker.sock nexdrew/rekcod
2 使用方法:
[root@TNAS-012664 ~]# rekcod mysql == mysql 是我的容器名
docker run --name mysql --runtime runc -v /mnt/md0/User/wzp/home/www/mysql/:/var/lib/mysql -p 3306:3306/tcp --net bridge --restart no -h 39964e9e508a --expose 3306/tcp --expose 33060/tcp -e MYSQL_ROOT_PASSWORD=123456 -e PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -e GOSU_VERSION=1.12 -e MYSQL_MAJOR=5.7 -e MYSQL_VERSION=5.7.30-1debian10 -d -t -i --entrypoint docker-entrypoint.sh mysql mysqld
可以看到我的数据库文件都是放在 /mnt/md0/User/wzp/home/www/mysql/ 目录下,我查看了下该目录文件,数据应该是没有丢失的。
于是,我新建了 mysql2 容器,命令如下
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 –name mysql2 -v /mnt/md0/User/wzp/home/www/mysql/:/var/lib/mysql -v /mnt/md0/User/wzp/home/www/mysql-files:/var/lib/mysql-files/ mysql
然后,进入 mysql2,发现没法连接数据库
docker exec -it mysql2 /bin/bash
查看 mysql 状态
root@0e83698acbfb:/# mysqld status
mysqld: [Warning] World-writable config file /etc/mysql/conf.d/docker.cnf is ignored.
mysqld: [Warning] World-writable config file /etc/mysql/conf.d/mysql.cnf is ignored.
2020-12-27T02:39:41.865252Z 0 [Warning] [MY-011070] [Server] Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it is deprecated and will be removed in a future release.
2020-12-27T02:39:41.865455Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 105
2020-12-27T02:39:41.871715Z 0 [ERROR] [MY-010123] [Server] Fatal error: Please read Security section of the manual to find out how to run mysqld as root!
2020-12-27T02:39:41.872541Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-12-27T02:39:41.872776Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20) MySQL Community Server - GPL.
root@0e83698acbfb:/# Fatal error: Please read Security section of the manual to find out how to run mysqld as root!^C
用以下方法以 root 方式启动 mysqld
root@0e83698acbfb:/# mysqld --user=root
mysqld: [Warning] World-writable config file /etc/mysql/conf.d/docker.cnf is ignored.
mysqld: [Warning] World-writable config file /etc/mysql/conf.d/mysql.cnf is ignored.
2020-12-27T02:40:57.169719Z 0 [Warning] [MY-011070] [Server] Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it is deprecated and will be removed in a future release.
2020-12-27T02:40:57.169896Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 116
2020-12-27T02:40:57.184807Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-12-27T02:40:57.241048Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 11
2020-12-27T02:40:58.241783Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 11
2020-12-27T02:40:59.242983Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 11
2020-12-27T02:41:00.244290Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 11
2020-12-27T02:41:01.245762Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 11
2020-12-27T02:41:02.247539Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 11
屏幕一直打印 Unable to lock ./ibdata1 error: 11,只能 CTRL- C 强制中断。百度了下,有两个方式来解决该问题
1 参考 mysql 之 [ERROR] InnoDB: Unable to lock ./ibdata1, error: 11
该文章分析,导致该 error 主要是以下两个原因,很可惜,文章中的方法并不能解决我的问题。
磁盘空间目录不足
ibdata1 文件被其他的进程占用
2 参考 mysqld 报 InnoDB: Unable to lock ./ibdata1 error: 11 这篇文章完美解决了我的问题。主要进行了以下操作。
2.1 首先,进入我的 mysql 数据库所在目录
cd /mnt/md0/User/wzp/home/www/mysql
2.2 然后将 ibdata1 等文件重命名后,再 cp 回来。具体为什么要这么做,我也不太明白。
[root@TNAS-012664 www]# cd /mnt/md0/User/wzp/home/www/mysql/
[root@TNAS-012664 mysql]# ls
auto.cnf binlog.index client-key.pem dci #ib_16384_0.dblwr ib_logfile0 mautic nianbao performance_schema robot shangbiao undo_001 zeng
baike ca-key.pem company_works dianzicaipiao #ib_16384_1.dblwr ib_logfile1 mysql niuwan private_key.pem sara_wiki sjzt_ry6 undo_002
bigdatapaltfrom ca.pem copyrightdata gs_data ib_buffer_pool #innodb_temp mysql.ibd pachong public_key.pem server-cert.pem sys wenshu
binlog.000001 client-cert.pem db_huayun hy_dci_admin ibdata1 integrate mysql_upgrade_info pachong2 qianliu_wiki server-key.pem ucenter_huayunyy yuanqixiaoshuo
[root@TNAS-012664 mysql]# mv ibdata1 ibdata1.bak
[root@TNAS-012664 mysql]# mv ib_logfile0 ib_logfile0.bak
[root@TNAS-012664 mysql]# mv ib_logfile1 ib_logfile1.bak
[root@TNAS-012664 mysql]# cp -a ibdata1.bak ibdata1
[root@TNAS-012664 mysql]# cp -a ib_logfile0.bak ib_logfile0
[root@TNAS-012664 mysql]# cp -a ib_logfile1.bak ib_logfile1
2.3 然后我删除之前的 mysql2 container,用下面的命令再次 run 一个 mysql2 容器,发现就可以连接 mysql 数据库了。
[root@TNAS-012664 ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql2 -v /mnt/md0/User/wzp/home/www/mysql/:/var/lib/mysql -v /mnt/md0/User/wzp/home/www/mysql-files:/var/lib/mysql-files/ mysql
1e031247ea46e82f6205db68e7fb1b55389c87e5e2cb13517f9e1ac17d514509
[root@TNAS-012664 ~]# docker exec -it mysql2 /bin/bash
root@1e031247ea46:/# mysql -uroot -p
mysql: [Warning] World-writable config file /etc/mysql/my.cnf is ignored.
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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;
以为 mysql 容器好了就万事大吉了?No,too naive! 还有一个小插曲等着我呢。这边刚把 mysql 容器解决好,突然又发现我的 wiki 容器莫名其妙没了。不过好在,数据库文件在,一切还能恢复如初。
1 用以下的命令新建两个 wiki
docker run --name sarawiki --link mysql2:mysql -p 8086:80 -d sarawang85/mediawiki:1.0.0
docker run --name qianliuwiki --link mysql2:mysql -p 8083:80 -d sarawang85/mediawiki:1.0.0
2 然后把备份好的 LocalSettings.php 文件和 Logo 图片 copy 进容器,搞定。
PS,如果在 docker gui 界面编辑 stop 状态的容器,比如设置内存限制,实际是会新建一个同名的容器。因为我发现我这样做了后,访问我的 wiki,又提示我重新安装。
再次 PS,mysql 备份得立即提上日程。mysql 内存限制不能单单通过容器来限制,my.cnf 也得做响应更改。
感谢你能够认真阅读完这篇文章,希望丸趣 TV 小编分享的“Mysql 容器启动失败恢复的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持丸趣 TV,关注丸趣 TV 行业资讯频道,更多相关知识等着你来学习!