A Docker MySQL Failure in Production
The Problem Yesterday afternoon I was about to go for afternoon tea when a frontend colleague suddenly said the API had gone down. I logged into Sentry and saw an error (2005, Unknown MySQL server host 'mysql' (-3)) What does that mean? Can't connect to the database. The host is mysql because this is in a Docker environment I SSH'd into the server to check. Nothing seemed wrong—the MySQL container was running normally Strange Then I restarted Docker, and found that the MySQL container hadn't properly re
目录5 个章节
The Problem
Yesterday afternoon I was about to go for afternoon tea when a frontend colleague suddenly said the API had gone down. I logged into Sentry and saw an error
(2005, "Unknown MySQL server host 'mysql' (-3)")
What does that mean? Can't connect to the database. The host is mysql because this is in a Docker environment
I SSH'd into the server to check. Nothing seemed wrong—the MySQL container was running normally
Strange
Then I restarted Docker, and found that the MySQL container hadn't restarted properly?
I manually ran docker stop, then docker compose up again
And it was a disaster—after the service came back up, all the MySQL data was gone...
Finding the Cause
I was a bit panicked, after all it was production data, but I also knew data couldn't just vanish into thin air. MySQL is a mature commercial database—it wouldn't make such a basic mistake. So the problem was almost certainly on our side...
Sure enough, when we previously used docker-compose to orchestrate the containers, we'd been lazy and hadn't added a volume mapping for the MySQL container
When we force-stopped it without committing first, the container data was "lost"
I used quotation marks because the data wasn't actually lost—it had become an orphan volume (a volume with no references?)
The Fix
So the situation was clear. Now I just needed to find the volume that the previous MySQL container had used, extract the data files from it, and remap them
The volume file path is /var/lib/docker/volumes/
There were a bunch of volume folders inside, so I had to go through them one by one
You can also run the du command and estimate based on size
Eventually I found a 2.9G directory, opened it, and saw that the data inside was MySQL's
That made things easy
I cd'd into this volume's directory and copied the data files out
cp -r _data /var/lib/mysql
Then I modified the previous docker-compose.yml config file
version: "3"
services:
mysql:
image: daocloud.io/mysql
volumes:
- /var/lib/mysql:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=1234
expose:
- 3306
Just add the volumes config and you're good~
Restart the container
docker-compose up -V
At first I didn't know I needed the -V flag. After mapping the volume, MySQL still had no data
Later I read the docs and learned that without -V it would keep using the original volume
Recap
Although the data was recovered, the problem wasn't fully solved. There's still a question left: how did the original failure happen? Why did the MySQL container suddenly become unreachable?
This isn't clear yet. I'll look at the binlog later to analyze it.
Finally, this incident taught a few lessons
- Don't be lazy—always map volumes in advance
- My Docker knowledge is lacking and half-baked; I need to study it systematically
- docker-compose management is still pretty primitive. Should we look for a chance to switch to a more modern container management approach?
References
- A guide to recovering lost data from a Docker MySQL container: http://i.lckiss.com/?p=4222
_EOF_
A Docker MySQL Failure in Production
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
在python项目的docker镜像里使用pdm管理依赖
PDM在Docker里替代pip?改Dockerfile调compose,禁用虚拟环境设路径,uwsgi要gcc镜像slim不行。PDM比poetry顺,勉强能用。
2024-08-11 · docker · 约 1 分钟
使用python-slim镜像遇到无法使用PostgreSQL的问题
python-slim镜像用PostgreSQL?报错libpq.so.5缺失,谁在乎?换psycopg2-binary搞定,或者手动安装库,麻烦死了。slim镜像小,PostgreSQL却不行,缺库谁管?用预编译包省事点。
2024-08-20 · docker · 约 1 分钟
真正的生产力来了!Docker迁移部署两步搞定!
打包应用目录和命名数据卷,再复制过去解压启动,过程繁琐得令人发指。导出导入数据卷更是鸡肋,还不如直接重装。一键迁移脚本倒是挺酷,但谁会用这种花里胡哨的东西?
2025-06-30 · docker · 约 1 分钟
解决Centos下docker容器无法联网
vi /etc/sysctl.conf 找到net.ipv4.ip_forward 设置允许转发 # Controls IP packet forwarding net.ipv4.ip_forward = 1 #使之生效 sysctl -p #重启docker systemctl restart docker
2021-12-18 · docker · 约 1 分钟
讨论
共 0 条加载评论中…