mongo db 异常关闭后重启报错 && 设置mongodb开机自启动 但无法启动 或不执行

一、解决mongo db 异常关闭后重启报错

报错信息:

about to fork child process, waiting until server is ready for connections.
forked process: 22200
ERROR: child process failed, exited with error number 1
To see additional information in this output, start without the "--fork" option

解决办法:
1、删除dbpath路径下*.lock文件(dbPath在mongod.conf中)
2、修复 /usr/local/bin/mongod --repair --dbpath=/usr/local/mongodb/data
3、再次启动 mongod -f /etc/mongod.conf

二、解决mongodb开机自启动异常

1. 先设置开机自启动 参考链接

由于异常关机导致mongo不能直接通过命令 mongod -f /etc/mongod.conf 启动,因此需要写一个shell脚本:

#!/bin/bash
echo "-------- 开始启动mongo --------"
source /etc/profile
sleep 10
mv /var/lib/mongo/mongod.lock /var/lib/mongo/mongod.lock.$(date +"%Y-%m-%d")

mongod --repair --dbpath=/var/lib/mongo # > /root/mongo-reboot-logs/repair_$(date +"%Y%m%d").log 2>&1 &

mongod -f /etc/mongod.conf # > /root/start_$(date +"%Y%m%d").log 2>&1 &
# 注意:上面重定向的异常日志都没有作用,所以注释掉了,原理暂时不清楚。启动日志在mongod的一个单独日志文件中

然后在/etc/rc.local中添加执行:

sh /usr/local/mongod.sh > /root/mongod_$(date +"%Y%m%d").log 2>&1 &

2. reboot重启机器后,发现服务没有启动

通过开机服务状态命令:

systemctl status rc-local

看到错误:

这里卡了很久,因为没有详细的错误原因。需要把错误日志打印出来,才能定位到问题。最开始尝试在启动命令后面重定向日志,但日志文件内容全是空的(重定向后,systemctl status rc-local会显示执行成功,因为错误被重定向了)。

找了很久发现mongod有自己的输出日志:/var/log/mongodb/mongod.log(如果这个文件不存在,请通过mongd.conf找到该systemLog的配置路径):

#mongd.conf
# mongod.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  
  pidFilePath: /var/run/mongodb/mongod.pid  

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

3. 通过该日志文件定位到是权限问题:

2023-04-17T16:35:52.770+0800 I CONTROL  [main] ***** SERVER RESTARTED *****
2023-04-17T16:35:52.789+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2023-04-17T16:35:52.797+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1093 port=27017 dbpath=/var/lib/mongo 64-bit host=localhost.localdomain
2023-04-17T16:35:52.797+0800 I CONTROL  [initandlisten] db version v4.0.28
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten] git version: af1a9dc12adcfa83cc19571cb3faba26eeddac92
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten] modules: none
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten] build environment:
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten]     distmod: rhel70
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten]     distarch: x86_64
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2023-04-17T16:35:52.798+0800 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "0.0.0.0", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, security: { authorization: "enabled" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2023-04-17T16:35:52.815+0800 I STORAGE  [initandlisten] Detected data files in /var/lib/mongo created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2023-04-17T16:35:52.815+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1378M,cache_overflow=(file_max=0M),session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2023-04-17T16:35:53.629+0800 E STORAGE  [initandlisten] WiredTiger error (13) [1681720553:629588][1093:0x7f385bf1cb80], wiredtiger_open: __posix_open_file, 672: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied Raw: [1681720553:629588][1093:0x7f385bf1cb80], wiredtiger_open: __posix_open_file, 672: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied
2023-04-17T16:35:53.630+0800 E STORAGE  [initandlisten] WiredTiger error (13) [1681720553:630581][1093:0x7f385bf1cb80], wiredtiger_open: __posix_open_file, 672: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied Raw: [1681720553:630581][1093:0x7f385bf1cb80], wiredtiger_open: __posix_open_file, 672: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied
2023-04-17T16:35:53.630+0800 E STORAGE  [initandlisten] WiredTiger error (13) [1681720553:630773][1093:0x7f385bf1cb80], wiredtiger_open: __posix_open_file, 672: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied Raw: [1681720553:630773][1093:0x7f385bf1cb80], wiredtiger_open: __posix_open_file, 672: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied
2023-04-17T16:35:53.630+0800 W STORAGE  [initandlisten] Failed to start up WiredTiger under any compatibility version.
2023-04-17T16:35:53.630+0800 F STORAGE  [initandlisten] Reason: 13: Permission denied
2023-04-17T16:35:53.630+0800 F -        [initandlisten] Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 711
2023-04-17T16:35:53.630+0800 F -        [initandlisten] \n\n***aborting after fassert() failure\n\n

找到原因就解决一大半问题了。到/var/lib/mongo/WiredTiger.turtle的父目录上,通过命令:chown mongod:mongod * -R,将该文件夹下root权限的文件全部改成了mongod的。

再次reboot后,发现mongo已经可以随开机自己启动了。

posted @ 2023-04-17 10:40  流失的痕迹  阅读(1188)  评论(0编辑  收藏  举报