[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$fWPrrUOL_PX9FO1aDpPAEx2Mok2WcKuSDyQitRzyf1Uw":3},{"code":4,"message":5,"data":6},200,"成功",{"id":7,"createdAt":8,"title":9,"content":10,"summary":11,"image":12,"uid":13,"user":14,"categoryId":21,"category":22,"subCategoryId":24,"subCategory":25,"comments":27,"status":17,"reason":28,"notice":28,"visitCount":29,"commentCount":30,"keywords":31},170,"2025-10-26T01:59:59.522Z","mongodb docker error: ERROR: child process failed, exited with 1","## 背景\n这个问题出现的背景是我使用docker compose yml文件来部署mongodb数据库等服务，在自己电脑上使用docker以及yml文件都没有问题，但是部署到云服务器上容器就一直在restart，链接数据库的后端服务也没有启动成功。\n\n## 问题\n> mongodb | {\"t\":{\"$date\":\"2025-10-26T00:01:41.215+00:00\"},\"s\":\"I\",  \"c\":\"CONTROL\",  \"id\":8423404, \"ctx\":\"initandlisten\",\"msg\":\"mongod shutdown complete\",\"attr\":{\"Summary of time elapsed\":{\"Statistics\":{\"enterTerminalShutdownMillis\":0,\"stepDownReplCoordMillis\":0,\"quiesceModeMillis\":0,\"stopFLECrudMillis\":0,\"shutDownMirrorMaestroMillis\":0,\"shutDownWaitForMajorityServiceMillis\":0,\"shutDownGlobalConnectionPoolMillis\":0,\"shutDownSearchTaskExecutorsMillis\":0,\"shutDownFlowControlTicketHolderMillis\":0,\"shutDownReplicaSetMonitorMillis\":0,\"shutDownTransportLayerMillis\":0,\"shutDownTTLMonitorMillis\":0,\"shutDownExpiredDocumentRemoverMillis\":0,\"shutDownOtelMetricsMillis\":0,\"shutDownFTDCMillis\":0,\"shutDownOCSPMillis\":0,\"shutdownTaskTotalMillis\":0}}}}\nmongodb | {\"t\":{\"$date\":\"2025-10-26T00:01:41.215+00:00\"},\"s\":\"I\",  \"c\":\"CONTROL\",  \"id\":23138,   \"ctx\":\"initandlisten\",\"msg\":\"Shutting down\",\"attr\":{\"exitCode\":100}}\nmongodb | ERROR: child process failed, exited with 1\nmongodb | To see additional information in this output, start without the \"--fork\" option.\nmongodb | Take a look at your mongod configuration to see if something is wrong.\nmongodb | Could not init database.\nmongodb | ['/usr/bin/mongod', '--bind_ip', '127.0.0.1', '--port', '27017', '--logappend', '--tlsMode', 'disabled', '--logpath', '/proc/1/fd/1', '--fork']\nmongodb | Subprocess failed with errorcode 1\n\n截图：\n![mongodb docker error](https://image.xinwei.ltd/image1761443050983.png)\n\n我的docker compose yml中关于mongodb的配置部分：\n```docker-compose.yml\n...\nservices:\n  db: \n    image: mongodb/mongodb-community-server\n    container_name: mongodb\n    restart: always\n    ports:\n      - 27017:27017\n    volumes:\n      - ./data:/data/db\n    environment:\n      - MONGO_INITDB_ROOT_USERNAME=root\n      - MONGO_INITDB_ROOT_PASSWORD=xxx\n...\n```\n这部分的mongodb配置是没有问题的，在自己的电脑上docker文件也没有问题，后面部署到服务器上也是可以的。\n\n## 解决方法\n\n根本原因是文件的权限问题，是哪个文件的问题呢？除了上面bind部分的data文件夹，其实还有yml文件的权限问题。\n\n### 1. 首先查看服务器上文件的权限\n```docker-centos-server\n[root@VM-4-14-centos docker]# ll\ntotal 8\ndrwxr-xr-x 2 root root 4096 Oct 26 08:01 data\n-rw-r--r-- 1 root root 1174 Oct 26 07:54 docker-compose.yml\n```\n\n### 2. 修改文件夹和文件的权限\n从上面看的出，挂载的data文件夹以及yml文件，权限并没有完全给予所有group user读写，那么我们需要改写它们的权限。\n```docker-centos-server\n[root@VM-4-14-centos docker]# chmod 777 *\n[root@VM-4-14-centos docker]# ll\ntotal 8\ndrwxrwxrwx 2 root root 4096 Oct 26 08:01 data\n-rwxrwxrwx 1 root root 1174 Oct 26 07:54 docker-compose.yml\n```\n如上所示，使用chmod来给目录下所有文件修改为所有角色都有读写权限。\n\n### 3. 在服务器上执行docker compose命令验证一下\n```docker-centos-server\n[root@VM-4-14-centos docker]# docker-compose up\nCreating mongodb ... done\nAttaching to mongodb\n...\nmongodb | {\"t\":{\"$date\":\"2025-10-26T00:07:47.565+00:00\"},\"s\":\"I\",  \"c\":\"WTCHKPT\",  \"id\":22430,   \"ctx\":\"Checkpointer\",\"msg\":\"WiredTiger message\",\"attr\":{\"message\":{\"ts_sec\":1761437267,\"ts_usec\":565072,\"thread\":\"117:0x7f69ed371640\",\"session_name\":\"WT_SESSION.checkpoint\",\"category\":\"WT_VERB_CHECKPOINT_PROGRESS\",\"log_id\":1000000,\"category_id\":7,\"verbose_level\":\"INFO\",\"verbose_level_id\":0,\"msg\":\"saving checkpoint snapshot min: 3, snapshot max: 3 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 7\"}}}\n...\n```\n没有任何报错，至此我们知道了，需要在自动部署脚本中需要加入修改文件权限的脚本。\n\n我截取部分自己写的部署脚本给一个示例：\n```deploy-shell-script.sh\n...\necho \"执行docker-compose命令，启动容器\"\nssh -i /Users/xxx/ssh_cert/ssh_xxx_visit.pem root@xxx.xxx.xxx.xxx '\necho \"当前目录：\";\npwd;\ncd /root/xxx/docker;\nchmod 777 *;\ndocker-compose up;\n'\n...\n```\n\n好了，至此问题解决了，希望对大家有帮助。\n\n","记录一下使用mongodb的docker镜像遇到的问题，一开始还以为我的yml文件配置问题，还有docker版本问题，后面捣鼓半天。","https://image.xinwei.ltd/image1761443050983.png",499668042977349,{"phone":15,"userId":13,"nickName":16,"vipType":17,"avatar":18,"sign":19,"createdAt":20},"13121171998","全栈老韩",1,"https://image.xinwei.ltd/images/IMG_5430.JPG","全栈工程师，擅长iOS App开发、前端（vue、react、nuxt、小程序&Taro）开发、Flutter、React Native、后端（midwayjs、golang、express、koa）开发、docker容器、seo优化等。","2024-01-01T16:14:30.305Z",2,{"id":21,"name":23},"IT技术",36,{"id":24,"name":26,"parentId":21},"Docker",[],"",84,0,"mongodb,mongoose,mongodb docker,mongo,mongodb error,docker error"]