在mongodb的启动时,在数据目录下,会生成一个mongod.lock文件。如果在正常退出时,会清除这个mongod.lock文件,若要是异常退出,在下次启动的时候,会禁止启动,从而保留一份干净的一份副本数据。有人可能会想到删除这个文件,
建议请不要这么做。如果这么做,我们也不知道数据文件是否会损坏,如果mongod.lock文件阻止mongod的启动,请对数据文件进行修复,而不是简单的删除该文件。而这里的mongod.lock文件存放的是:启动mongod的进程号.
正常启动:
/home/work/mongodb/4.0.17/bin/mongod --config /home/work/mongodb/mongo_27000/etc/mongodb.conf
#############################
直接运行单实例:
/home/work/mongodb/4.0.17/bin/mongod --replSet myapp --dbpath /home/work/mongodb/mongo_27000/data --port 27017 --logpath /home/work/mongodb/mongo_27000/log/mongodb.log --fork
异常关闭后启动:
比如kill -9关闭实例
正常启动出现以下报错:
waiting until server is ready for connections.
解决:
1, 删除data目录下的lock文件:
[work@ data]$ tree -L 2 . ├── admin │ ├── collection │ └── index ├── apple │ ├── collection │ └── index ├── config │ ├── collection │ └── index ├── diagnostic.data │ ├── metrics.2020-09-03T06-58-33Z-00000 │ ├── metrics.2020-09-04T07-03-33Z-00000 │ ├── metrics.2020-09-05T07-03-33Z-00000 │ ├── metrics.2020-09-06T06-58-33Z-00000 │ ├── metrics.2020-09-07T06-53-33Z-00000 │ ├── metrics.2020-09-08T06-38-33Z-00000 │ ├── metrics.2020-09-09T06-18-33Z-00000 │ ├── metrics.2020-09-10T06-03-33Z-00000 │ ├── metrics.2020-09-11T05-48-33Z-00000 │ ├── metrics.2020-09-12T05-28-33Z-00000 │ ├── metrics.2020-09-13T05-08-33Z-00000 │ ├── metrics.2020-09-14T04-48-33Z-00000 │ ├── metrics.2020-09-15T04-28-33Z-00000 │ ├── metrics.2020-09-16T04-13-33Z-00000 │ ├── metrics.2020-09-17T03-28-33Z-00000 │ ├── metrics.2020-09-18T02-43-33Z-00000 │ ├── metrics.2020-09-19T02-08-33Z-00000 │ ├── metrics.2020-09-20T01-38-33Z-00000 │ ├── metrics.2020-09-21T01-03-33Z-00000 │ ├── metrics.2020-09-22T00-18-33Z-00000 │ ├── metrics.2020-09-22T03-13-43Z-00000 │ ├── metrics.2020-09-22T03-18-43Z-00000 │ └── metrics.interim ├── google │ ├── collection │ └── index ├── journal │ ├── WiredTigerLog.0000000503 │ ├── WiredTigerPreplog.0000000001 │ └── WiredTigerPreplog.0000000002 ├── local │ ├── collection │ └── index ├── _mdb_catalog.wt ├── mongod.lock ├── sizeStorer.wt ├── storage.bson ├── test │ ├── collection │ └── index ├── WiredTiger ├── WiredTigerLAS.wt ├── WiredTiger.lock ├── WiredTiger.turtle └── WiredTiger.wt
2,修复:
./mongod --repair --config /home/work/mongodb/mongo_27000/etc/mongodb.conf
3,开启:
/home/work/mongodb/4.0.17/bin/mongod --config /home/work/mongodb/mongo_27000/etc/mongodb.conf
关闭:
1. 登陆 正常关闭
use admin; db.shutdownServer(); use admin db.shutdownServer();
#########################################
glc-test:PRIMARY> use admin switched to db admin glc-test:PRIMARY> db.shutdownServer(); server should be down... 2020-09-22T14:02:54.171+0800 I NETWORK [js] trying reconnect to XXX:27000 failed 2020-09-22T14:02:55.282+0800 I NETWORK [js] reconnect XXX:28042 failed failed 2020-09-22T14:02:55.285+0800 I NETWORK [js] trying reconnect to XXX:27000 failed 2020-09-22T14:02:55.285+0800 I NETWORK [js] reconnect XXX:28042 failed failed > ^C 2020-09-22T14:04:18.256+0800 I NETWORK [js] trying reconnect to XXX:27000 failed 2020-09-22T14:04:18.258+0800 I NETWORK [js] reconnect XXX:27000 ok exit status 0
2. mongod 命令 正常关闭
[work@xxx ] /home/work/mongodb/4.0.17/bin/mongod --shutdown --dbpath /home/work/mongodb/mongo_27000/data 2020-09-22T14:09:29.221+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' killing process with pid: 371416
3. kill 命令 正常关闭
[work@xxx]$ ps aux|grep mongod work 394902 6.6 0.0 1432792 57620 ? SLl 14:12 0:02 /home/work/mongodb/4.0.17/bin/mongod --config /home/work/mongodb/mongo_27000/etcmongodb.conf work 397490 0.0 0.0 112644 964 pts/0 S+ 14:12 0:00 grep --color=auto mongod [work@xxx mongodb]$ kill 394902
4. kill -9 命令 异常关闭
[work@xxx]$ ps aux|grep mongod work 394902 6.6 0.0 1432792 57620 ? SLl 14:12 0:02 /home/work/mongodb/4.0.17/bin/mongod --config /home/work/mongodb/mongo_27000/etcmongodb.conf work 397490 0.0 0.0 112644 964 pts/0 S+ 14:12 0:00 grep --color=auto mongod [work@xxx mongodb]$ kill -9 394902 # 建议不要使用 ”kill -9 pid“,因为如果 MongoDB 运行在没开启日志(--journal)的情况下, 可能会造成数据损失。