mongodb两个常见报错解决方式

报错:

1
2
3
4
5
6
7
8
root@iZ94t4csjq4Z:~# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
2018-03-21T02:45:58.758+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-03-21T02:45:58.791+0800 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

解决方法:

1
2
3
4
sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --repair --dbpath /var/lib/mongodb
sudo mongod --fork --logpath /var/lib/mongodb/mongodb.log --dbpath /var/lib/mongodb
sudo service mongodb start

报错:

1
2
root@iZ94t4csjq4Z:~# sudo service mongodb start
Failed to start mongodb.service: Unit mongodb.service is masked.
1
2
root@iZ94t4csjq4Z:~# sudo chown -R mongodb:mongodb /var/lib/mongodb
root@iZ94t4csjq4Z:~# sudo systemctl start mongod