Mongodb exception: connect failed
-
08-10-2020 - |
Question
Here's the environment:
- Linux(x64): 16.04
- MongoDB: 3.2.15
Error:
root@system-test:~# mongo
MongoDB shell version: 3.2.15
connecting to: test
2017-07-25T07:31:28.528+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2017-07-25T07:31:28.530+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:229:14
@(connect):1:6
exception: connect failed
root@system-test:~#
When I tried to see the status I get this:
root@system-test:~# sudo service mongodb status
● mongodb.service - High-performance, schema-free document-oriented database
Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2017-07-25 07:45:32 UTC; 1min 8s ago
Process: 1721 ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf (code=exited, status=14)
Main PID: 1721 (code=exited, status=14)
Jul 25 07:45:32 system-test systemd[1]: Started High-performance, schema-free document-oriented database.
Jul 25 07:45:32 system-test systemd[1]: mongodb.service: Main process exited, code=exited, status=14/n/a
Jul 25 07:45:32 system-test systemd[1]: mongodb.service: Unit entered failed state.
Jul 25 07:45:32 system-test systemd[1]: mongodb.service: Failed with result 'exit-code'.
How it happened:
I tried to stop MongoDB and did the following
sudo service mongo stop
[This didn't work].
Then i tried kill - 2 <PID>
[Seem didn't worked too]
But after this command #mongo
stopped working and getting the above error.
Please help guys
Solution
sudo service mongo stop [This didn't work]
As MongoDB BOL mongo is an interactive JavaScript shell interface to MongoDB
, which provides a powerful interface for systems administrators as well as a way for developers to test queries and operations directly with the database. mongo
also provides a fully functional JavaScript environment for use with a MongoDB
.
And mongod is the primary daemon process for the MongoDB
system. It handles data requests, manages data access, and performs background management operations.
As simple language we can say that mongo is shell interface for MongoDB
. And mongod is server , which handles data requests, manages data access, and performs background management operations.
So, in place sudo service mongo stop
it should be the below mention command
sudo service mongod stop
As MongoDB documented Here Some of basic procedure to check MongoDB
services in RHEL
is as mention below:
Start MongoDB
sudo service mongod start
Stop MongoDB
sudo service mongod stop
Restart MongoDB
sudo service mongod restart
Verify that MongoDB has started successfully
sudo chkconfig mongod on
Begin using MongoDB
Start a mongo
shell on the same host machine as the mongod
mongo --host 127.0.0.1:27017
OTHER TIPS
As your service mongodb status tells, your mongod process don't exists. So, if you try to connect (with mongo -program) to your mongod, you cannot.
So, restart your mongod sudo service mongodb restart
, try now to connect it mongo
.
If you want to see what happens when you shutdown, start following mongodb.log tail -f /var/log/mongodb/mongodb.log
and then in other terminal say sudo service mongodb stop
, some times shutdown process can take quite long time.. As startup process too.
I also ran into same issue. But was able to resolve by completely removing mongo first
sudo apt-get purge mongodb-org*
Then performing following
sudo apt update
sudo apt install -y mongodb
sudo systemctl status mongodb
You can refer to following link for the same: https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-18-04
This question is for Linux, but for fellow windows users who found their way here and want a windows solution...
the problem is your Mongodb service is stopped. So do the following steps:
1) cntrl-alt-del and open your task manager
2) click on 'services'
3) scroll down to MongoDB
4) right click and hit 'start' (or 'restart')
go back to your mongo.exe file and your mongo shell should work now
If the problem is related to a remote access, here is the solution for anyone who has the problem
show iptable rules line numbers
sudo iptables -L --line-numbers
The output will be something like this
...
num target prot opt source destination
1 ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
2 ACCEPT all -- anywhere anywhere
3 INPUT_direct all -- anywhere anywhere
4 INPUT_ZONES_SOURCE all -- anywhere anywhere
5 INPUT_ZONES all -- anywhere anywhere
6 DROP all -- anywhere anywhere ctstate INVALID
7 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
8 ACCEPT tcp -- 172.16.84.102 anywhere tcp dpt:27017 state NEW,ESTABLISHED
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
2 ACCEPT all -- anywhere anywhere
3 FORWARD_direct all -- anywhere anywhere
4 FORWARD_IN_ZONES_SOURCE all -- anywhere anywhere
5 FORWARD_IN_ZONES all -- anywhere anywhere
6 FORWARD_OUT_ZONES_SOURCE all -- anywhere anywhere
7 FORWARD_OUT_ZONES all -- anywhere anywhere
8 DROP all -- anywhere anywhere ctstate INVALID
9 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
...
The problem is caused by the rule
7 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Delete it but keep security team informed about this operation
Here is how to delete a rule using the type ( input in out case ) and line number ( 7 in out case )
sudo iptables -D INPUT 7
Now test your connection to mongo db from other machines and it has to be working now.
Note: This solution will work if you already configured the /etc/mongod.conf by:
commenting
bindIp: 127.0.0.1
Adding
bindIpAll: true
Uncommenting security and adding
authorization: 'enabled'
Enjoy it :)