Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo - couldn't connect to server 127.0.0.1:27017

I am coming from riak and redis where I never had an issue with this services starting, or to interact.

This is a pervasive problem with mongo and am rather clueless. Restarting does not help.I am new to mongo.

mongo
MongoDB shell version: 2.2.1
connecting to: test
Fri Nov  9 16:44:06 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed

This is what I see in the logs.

 now open)
Fri Nov  9 16:44:34 [conn47] end connection 10.29.16.208:5306 (1 connection now open)
Fri Nov  9 16:45:04 [initandlisten] connection accepted from 10.29.16.208:5307 #48 (2 connections now open)
Fri Nov  9 16:45:04 [conn48] end connection 10.29.16.208:5307 (1 connection now open)
Fri Nov  9 16:45:04 [initandlisten] connection accepted from 10.29.16.208:5308 #49 (2 connections now open)
Fri Nov  9 16:45:04 [conn49] end connection 10.29.16.208:5308 (1 connection now open)
Fri Nov  9 16:45:34 [initandlisten] connection accepted from 10.29.16.208:5316 #50 (2 connections now open)
Fri Nov  9 16:45:34 [conn50] end connection 10.29.16.208:5316 (1 connection now open)
Fri Nov  9 16:45:34 [initandlisten] connection accepted from 10.29.16.208:5317 #51 (2 connections now open)
Fri Nov  9 16:45:34 [conn51] end connection 10.29.16.208:5317 (1 connection now open)
Fri Nov  9 16:46:04 [initandlisten] connection accepted from 10.29.16.208:5320 #52 (2 connections now open)
Fri Nov  9 16:46:04 [conn52] end connection 10.29.16.208:5320 (1 connection now open)
Fri Nov  9 16:46:04 [initandlisten] connection accepted from 10.29.16.208:5321 #53 (2 connections now open)
Fri Nov  9 16:46:04 [conn53] end connection 10.29.16.208:5321 (1 conn
like image 477
Tampa Avatar asked Nov 09 '12 16:11

Tampa


People also ask

Why MongoDB Cannot connect to server?

These are some of the solutions: Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

Can't connect to found database localhost 27017?

0.1 : 27017 ” is a general error message indicating that your client/driver cannot connect to a server on the specified hostname/IP and port. In this specific example, 127.0. 0.1 is the hostname or IP and 27017 is the port. Your connection from client to server is blocked by firewall or network configuration.

Why is MongoDB not connecting to Atlas?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.


8 Answers

Normally this caused because you didn't start mongod process before you try starting mongo shell.

Start mongod server

mongod

Open another terminal window

Start mongo shell

mongo
like image 106
Ravi Hamsa Avatar answered Sep 20 '22 19:09

Ravi Hamsa


Resolved.

This problem could be solved by the below mentioned 4 steps

1) Remove .lock file

sudo rm /var/lib/mongodb/mongod.lock 

2) repair the mongodb

mongod -–repair

3) start the mongod server

sudo service mongod start 

4) start the mongo client

mongo

For more details take a look at http://shakthydoss.com/error-couldnt-connect-to-server-127-0-0-127017-srcmongoshellmongo-js-exception-connect-failed/

http://shakthydoss.com/technical/error-couldnt-connect-to-server-127-0-0-127017-srcmongoshellmongo-js-exception-connect-failed/

like image 33
shakthydoss Avatar answered Sep 21 '22 19:09

shakthydoss


This method only works if you want to repair your data files without preserving the original files

To find where you dbpath resides- vim /etc/mongodb.conf

check for option dbpath=

(I have dbpath=/var/lib/mongodb)

Default: /data/db/

Typical locations include: /srv/mongodb, /var/lib/mongodb or /opt/mongodb .

Replace the /var/lib/mongodb with your dbpath

sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --dbpath /var/lib/mongodb/ --repair
sudo mongod --dbpath /var/lib/mongodb/ --journal

(Make sure that you leave you terminal running in which you have run above lines, dont press 'Ctrl+c' or quit it.) Type the command to start mongo now in another window.

Hope this works for you ! for those who want to repair your data files while preserving the original files mongo recover

like image 36
poorva Avatar answered Sep 21 '22 19:09

poorva


This error is what you would see if the mongo shell was not able to talk to the mongod server.

This could be because the address was wrong (host or IP) or that it was not running. One thing to note is the log trace provided does not cover the "Fri Nov 9 16:44:06" of your mongo timestamp.

Can you:

  1. Provide the command line arguments (if any) used to start your mongod process
  2. Provide the log file activity from the mongod startup as well as logs during the mongo shell startup attempt?
  3. Confirm that your mongod process is being started on the same machine as the mongo shell?
like image 20
Jenna Avatar answered Sep 18 '22 19:09

Jenna


I am a windows user and I installed MongoDB in November 2018 and I didn't wanted to setup data/db directories. But after few days, when I open, got an error message:

MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
2018-12-05T20:42:40.108+0530 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it. :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

Then I tried to fix using all of above answers and didn't worked. When I tried to run mongod it said

MongoDB starting : pid=12220 port=27017 dbpath=C:\data\db\ 64-bit host=bla

I tried to change the db path(to program files) through file system and also using cmd but those didnot worked.

Solution that worked for me is:

Opened the Task Manager (ctrl + shift + esc) -> Services tab and there was a MongoDB row with the status stopped. Then I right clicked and clicked start and everything working perfectly :).

like image 40
Blasanka Avatar answered Sep 22 '22 19:09

Blasanka


MAC OS

Check status

brew services list

Something like:

Name    Status  User Plist
mongodb stopped  

start it

brew services start mongodb

Try it again

mongo or mongo --port 27017

like image 28
William Hu Avatar answered Sep 18 '22 19:09

William Hu


Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it.

This error caused because Mongo Server been closed

Simple Follow these steps

  1. Open Task Manager
  2. Go to >Services
  3. Find MongoDB
  4. Right click and select Start
like image 42
Ali Ashraf Avatar answered Sep 22 '22 19:09

Ali Ashraf


In my case:

First I open config file

sudo vi /etc/mongodb.conf

Comment IP and Port like this

#bind_ip = 127.0.0.1
#port = 27017

Then restart mongodb

sudo service mongodb restart
sudo service mongod restart

Finally, it's work :D

finn@Finn ~ $ mongo
MongoDB shell version: 2.4.9
connecting to: test
> exit
like image 39
Finn Avatar answered Sep 18 '22 19:09

Finn