JP.
JP.

Reputation: 1075

Unable to connect to mongodb Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112

When ever I try to connect to mongo db I always get this error as below.

MongoDB shell version: 2.4.3 connecting to: test Fri Apr 26 14:31:46.941 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112 exception: connect failed

I tried various solutions listed in stackoverflow and tried following commands but nothing works.

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

// says such file located at this place.

2) sudo service mongodb start

// error: sudo: service: command not found

3) I've made sure nothing else is running at that port.

4) Even tried uninstalling and installing it again.

5) Also tried kill pid

Upvotes: 18

Views: 114673

Answers (18)

Pdrojack
Pdrojack

Reputation: 11

My problem was solved by changing the port in /etc/mongod.conf file then by running: mongo --port

Upvotes: 0

Prashant Raj
Prashant Raj

Reputation: 192

If you are on unix-like systems.

Starting in MongoDB 4.4, a startup error is generated if the ulimit value for number of open files is under 64000. View the current val

$ ulimit -n

Change value

$ ulimit -n <val>

Upvotes: 0

Muhannad
Muhannad

Reputation: 81

I am using version 4.0.6 ( the current release) on Linux Mint. You need to install Mongodb Compass(mongodb GUI) to interact with your databases. Here is the link: https://docs.mongodb.com/compass/master/install/

I hope this solves your problem.

Upvotes: 0

abrsh
abrsh

Reputation: 525

I had the same issue until i close teamviewer running on my pc. Then it worked fine!

Upvotes: 0

rahul sharma
rahul sharma

Reputation: 111

Quick Fix: I have the same problem to start the MongoDB and it was easily fixed by running the file mongod.exe (C:\Program Files\MongoDB\Server\3.2\bin) then run the file mongo.exe (C:\Program Files\MongoDB\Server\3.2\bin)..Problem fixed

Upvotes: 0

Esakkiappan .E
Esakkiappan .E

Reputation: 565

Start mongod server first

mongod

Open another terminal window

Start mongo shell

mongo

Upvotes: 5

JP.
JP.

Reputation: 1075

with new version of mongodb, this issue got resolved.

Upvotes: 0

Jessie Zhu
Jessie Zhu

Reputation: 21

I met with this problem too. And I figured it out by the following way:

First, open the terminal and write

mongod --dbpath {your project's path}

Second, open a new terminal and enter your project's path, write

mongo

Then it runs successfully

Upvotes: 0

chandraprakash
chandraprakash

Reputation: 1

Mongo DB requires space to store it files. So you should create folder structure for Mongo DB before starting the Mongodb server/client.

for e.g. MongoDb/Dbfiles where Mongo DB is installed.

Then in cmd promt exe mongod.exe and mongo.exe for client and done.

Upvotes: 0

ketankk
ketankk

Reputation: 2664

First start mongod service then mongo or mongos

Upvotes: 1

Juan Serrats
Juan Serrats

Reputation: 1358

Same error here, this worked for me:

sudo mongod --repair

followed by

sudo mongod

(forget about service and start)

Then on a different tab/terminal:

mongo

Upvotes: 38

Debasis Mondal
Debasis Mondal

Reputation: 65

If any one is facing the problem in windows machine then follow the steps.

  1. Add the path of mongodb in the environmental variable.
  2. Create a directory C:\Data\db.
  3. Open a terminal(cmd) and write mongod. (it will initiate the server).
  4. Open another terminal and write your own code.

Upvotes: 3

Buskeek
Buskeek

Reputation: 59

If you create data\db folder, firstly you must delete this folder then execute:

c:\mongodb\bin\mongo.exe 

Upvotes: 0

Vikas Kukreti
Vikas Kukreti

Reputation: 329

mongod --configsvr --smallfiles --nojournal --dbpath cfg/1 --port 26052 --fork --logpath cfg/3.log >/dev/null

Create a single mongos server on the default port

sleep 2000
mongos --configdb ${HOSTNAME}:26050,${HOSTNAME}:26051,${HOSTNAME}:26052 --fork --logpath mongos.log >/dev/null

Sleep for some time before starting the mongos you will not face the problem

Upvotes: 1

Sathish
Sathish

Reputation: 3557

I had the same issue, In my case there was a mismatch of ipaddress in mongo config

~# mongo
MongoDB shell version: 2.6.7
connecting to: test
2015-02-02T17:48:52.302+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-02-02T17:48:52.303+0530 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

When I checked my configuration file, I have specified the bind address to eth0 ip(10.8.10.111) not loopback address(127.0.0.1), So I have changed the ip address to loopback as like bind_ip = 127.0.0.1 in /etc/mongod.conf file and restart it with service mongod restart, finally works

# mongo 
MongoDB shell version: 2.6.7
connecting to: test
> 

Upvotes: 2

yogesh singh
yogesh singh

Reputation: 623

This is due to locking of Mongodb
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart

Upvotes: 4

sunitj
sunitj

Reputation: 1245

Faced same issue, my understanding is(it could be wrong)
1. Make sure mongodb is up and running
2. For linux access as sudo and for windows if connecting localhost turning off firewall may help but its not necessary
3. Just type mongo, it will try to connect to localhost by default. You need to specify IP if you are connecting to a remote server. By default test db will be used.

Upvotes: 1

MADHAIYAN M
MADHAIYAN M

Reputation: 2068

Please Start the MongoDB server first and try to connect.

Pre-requisite:

Create space for storing DB and tables in any directory Example (windows): D:\mongodbdata\

Steps:

  1. Start server

    mongod --port 5000 --dbpath D:\mongodbdata\ (please mention above created path)

  2. Connect mongodb server (Run in another terminal/console)

    mongo --port 5000

Upvotes: 3

Related Questions