Docker容器中MongoDB启动失败:WiredTiger.wt权限问题
解决Docker中MongoDB执行--repair后重启报错的问题
问题场景
在Docker容器中运行MongoDB服务,意外停机后执行--repair操作修复,重启容器时持续报错,核心错误信息:
__posix_open_file:805:/data/db/WiredTiger.wt: handle-open: open","error_str":"Operation not permitted","error_code":1
完整错误日志:
{"t":{"$date":"2023-09-17T21:40:08.188+00:00"},"s":"E", "c":"WT", "id":22435, "ctx":"initandlisten","msg":"WiredTiger error message","attr":{"error":1,"message":{"ts_sec":1694986808,"ts_usec":187956,"thread":"1:0xffffa15d8040","session_dhandle_name":"file:WiredTiger.wt","session_name":"connection","category":"WT_VERB_DEFAULT","category_id":9,"verbose_level":"ERROR","verbose_level_id":-3,"msg":"__posix_open_file:805:/data/db/WiredTiger.wt: handle-open: open","error_str":"Operation not permitted","error_code":1}}} {"t":{"$date":"2023-09-17T21:40:08.195+00:00"},"s":"E", "c":"WT", "id":22435, "ctx":"initandlisten","msg":"WiredTiger error message","attr":{"error":1,"message":{"ts_sec":1694986808,"ts_usec":195428,"thread":"1:0xffffa15d8040","session_dhandle_name":"file:WiredTiger.wt","session_name":"connection","category":"WT_VERB_DEFAULT","category_id":9,"verbose_level":"ERROR","verbose_level_id":-3,"msg":"__posix_open_file:805:/data/db/WiredTiger.wt: handle-open: open","error_str":"Operation not permitted","error_code":1}}} {"t":{"$date":"2023-09-17T21:40:08.203+00:00"},"s":"E", "c":"WT", "id":22435, "ctx":"initandlisten","msg":"WiredTiger error message","attr":{"error":1,"message":{"ts_sec":1694986808,"ts_usec":203157,"thread":"1:0xffffa15d8040","session_dhandle_name":"file:WiredTiger.wt","session_name":"connection","category":"WT_VERB_DEFAULT","category_id":9,"verbose_level":"ERROR","verbose_level_id":-3,"msg":"__posix_open_file:805:/data/db/WiredTiger.wt: handle-open: open","error_str":"Operation not permitted","error_code":1}}} {"t":{"$date":"2023-09-17T21:40:08.204+00:00"},"s":"W", "c":"STORAGE", "id":22347, "ctx":"initandlisten","msg":"Failed to start up WiredTiger under any compatibility version. This may be due to an unsupported upgrade or downgrade."} {"t":{"$date":"2023-09-17T21:40:08.204+00:00"},"s":"F", "c":"STORAGE", "id":28595, "ctx":"initandlisten","msg":"Terminating.","attr":{"reason":"1: Operation not permitted"}} {"t":{"$date":"2023-09-17T21:40:08.204+00:00"},"s":"F", "c":"ASSERT", "id":23091, "ctx":"initandlisten","msg":"Fatal assertion","attr":{"msgid":28595,"file":"src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp","line":708}} {"t":{"$date":"2023-09-17T21:40:08.204+00:00"},"s":"F", "c":"ASSERT", "id":23092, "ctx":"initandlisten","msg":"\n\n***aborting after fassert() failure\n\n"}
解决步骤
1. 彻底终止相关进程与容器
- 停止并删除MongoDB容器:
若容器无法正常停止,强制终止:docker stop <mongodb-container-name> docker rm <mongodb-container-name>docker kill <mongodb-container-id> - 检查宿主机是否有进程占用数据文件,如有则终止:
lsof | grep /path/to/your/mongo/data kill -9 <pid>
2. 修复数据目录权限
Docker官方Mongo镜像默认使用UID/GID为999的mongodb用户运行服务,--repair操作可能导致文件权限变更,需手动修正:
# 替换为你的宿主机Mongo数据挂载目录 sudo chown -R 999:999 /opt/mongo/data sudo chmod -R 755 /opt/mongo/data
3. 使用临时容器重新执行修复(推荐)
避免直接在运行的容器内执行--repair,用同版本镜像的临时容器完成修复,确保权限一致:
# 替换为你使用的MongoDB版本,比如mongo:6.0 docker run --rm -v /opt/mongo/data:/data/db mongo:your-version mongod --repair
4. 重新启动MongoDB容器
用标准命令启动容器,确认挂载目录权限正确:
docker run -d -p 27017:27017 -v /opt/mongo/data:/data/db --name mongodb mongo:your-version
查看日志验证启动状态:
docker logs mongodb
其他排查方向
- SELinux限制:临时关闭SELinux测试是否能启动:
若启动成功,需配置SELinux规则允许容器访问数据目录。sudo setenforce 0 - 文件系统损坏:卸载数据目录所在分区后,执行文件系统检查:
sudo umount /opt/mongo/data sudo fsck /dev/your-partition
内容的提问来源于stack exchange,提问作者Bruce Sherin
相关产品推荐
相关产品推荐

