Tampa
Tampa

Reputation: 78254

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

Upvotes: 228

Views: 635027

Answers (30)

kiyatrowes
kiyatrowes

Reputation: 21

try this one:

get into the bin folder on terminal, then write:

sudo ./mongod --dbpath <write_here_the_path_to_your_database>, click enter. (always do same thing).

then in another terminal window write: sudo ./mongo

new releases of mongodb(6.0.2 and above) will work with the following command: sudo mongosh or if connected to Atlas it would be sudo mongosh "URI OF MONGODB+SERV"

remember to download and install the mongo shell just copying the files inside the bin folder and pasting into the same root you have mongod.

Upvotes: 1

Jcc.Sanabria
Jcc.Sanabria

Reputation: 691

  1. Don't even try to run the journal if you haven't done the basics yet.

  2. Remove the lock file:

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

  3. In the /etc/mongodb.conf edit the bindIp parameter with 0.0.0.0 if the machine ip-address isn't static.

enter image description here

  1. ** Restart the server **, even in ubuntu it does not work just restarting the service (with systemctl), sometimes it is necessary to reboot the machine.

Upvotes: 0

Ali Aljabri
Ali Aljabri

Reputation: 53

Follow this steps in terminal

  1. Stop mongodb by running this command

brew services stop mongodb-community

  1. Start mongodb again

brew services start mongodb-community

mongod

mongo

Upvotes: 1

bensbenj
bensbenj

Reputation: 531

FOR MACOS

Check the status of the services:

brew services

If you have something like:

Name              Status
mongodb-community error

So you can just to stop and restart the service like this:

brew services stop mongodb-community
brew services start mongodb-community

Upvotes: 4

crifan
crifan

Reputation: 14328

For my case:

  • OS: CentOS 7

Error

> mongo
MongoDB shell version v4.4.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
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 Connection refused
connecct@src/mongo/shell/mongo.js 374 17
@(connect) 2 6
exception connect failed 
existing with code 1

Reason

  • mongod not running
    • but if run mongod, will not work for no access authority

Solution

  • use root to run mongod
sudo /data/mongodb/bin/mongod --config /data/mongodb/conf/mongodb.conf &

Upvotes: 0

Ani David
Ani David

Reputation: 141

When we take taskbar manager, go to Service tab. There MongoDB status shows the stopped. Right click for start. Its will connect MongoDB server version. for checking type "db".


db test

It will work fine.

Upvotes: 1

Meriem Bader
Meriem Bader

Reputation: 173

you must access via your cmd to your src C: \ Program Files \ MongoDB \ Server \ 4.4 \ bin and then you must open the task manager -> service -> (select mongoDB) -> start it, and retest via the cmd and it will be solved.

Upvotes: 0

Gabriel Arghire
Gabriel Arghire

Reputation: 2360

For Windows

Instead of

mongodb://localhost:27017/yourDatabase

Write

mongodb://yourComputerName:27017/yourDatabase

Example:

mongodb://Gabriel-Laptop:27017/shop


To find your computer/laptop name write in the search bar View your PC name and the name near Device name is the name you are looking for.


Note: I read somewhere that using localhost keyword in Windows OS for MongoDB is not recommended (most probably here).

Upvotes: 0

Natasha
Natasha

Reputation: 6893

After exhausting lots and lots of sulotions, I did-

sudo brew services start mongodb-community rather than sudo service mongod start and it worked.

So, first try

sudo brew services start mongodb-community

and then to start the mongo shell, do-

mongo

Upvotes: 4

Ali A
Ali A

Reputation: 359

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

Upvotes: 23

Code Tree
Code Tree

Reputation: 2209

OK, this may be strange. My error happened because my log grew exponentially and clogged up server space hence mongo didn't get enough space to store anything.

so I cleared the log

> /var/log/mongod.log

Upvotes: 1

Rohit Gaidhane
Rohit Gaidhane

Reputation: 351

I face same issue after searching a lot I solve this by following :

sudo service mongodb stop 

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

Upvotes: 15

MILLION GASHAW
MILLION GASHAW

Reputation: 119

Start monogdb service using

sudo service mongod start 

then from the new window or terminal start mongo client using

mongo

Upvotes: 4

shakthydoss
shakthydoss

Reputation: 2631

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/

Upvotes: 105

Rahul
Rahul

Reputation: 3349

I was getting this error on ubuntu but you can try with the following command:

sudo service mongod start

Upvotes: 3

in my case in windows and ubuntu, i needed to create /data/db folder in root. In ubuntu, i needed one additional option; to give permissions for directory.

windows

mkdir c:/data/db

ubuntu:

sudo mkdir -p /data/db
sudo chmod 700 /data/db

then run

sudo service mongod start

check

sudo service mongod status

and then run

mongo

Upvotes: 2

Mandeep Gill
Mandeep Gill

Reputation: 4895

I had same problem to connect mongodb after install using brew on macOS.

The problem was also mongod.lock but before i had mongodb 3.6 installed and database directory was /usr/local/var/mongodb and it have old files and mongod.lock

Simple i moved my old database files to different folder and removed everything from /usr/local/var/mongodb

Then restarted :

brew services restart mongodb

And it's working fine now.

Upvotes: 3

William Hu
William Hu

Reputation: 16149

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

Upvotes: 42

Blasanka
Blasanka

Reputation: 22437

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 :).

Upvotes: 54

GAltelino
GAltelino

Reputation: 95

@shakthydoss answer worked fine once, but after the second issue the .lock wasn't there, I just created a blank one, and it worked fine. So check if yours ain't there.

If you are using the command line:

cd /var/lib/mongodb

ls

"ls" will list all the files in the folder, if the mongod.lock isn't there, then:

sudo touch mongod.lock

Finally run command.

mongod –-repair

sudo service mongod start 

Mongo service should be working fine.

Upvotes: 0

ashish naghate
ashish naghate

Reputation: 71

Steps to solve this issue on Windows OS:

  1. Delete mongod.lock file from C:\Program Files\MongoDB\Server\4.0\data
  2. Click on mongod in C:\Program Files\MongoDB\Server\4.0\bin
  3. Click on mongo in C:\Program Files\MongoDB\Server\4.0\bin to start the mongodb shell

Upvotes: 0

Bahodir  Boydedayev
Bahodir Boydedayev

Reputation: 113

I also have the same problem then I got fixed by these two lines of code. Hope, it helps

systemctl start mongod
systemctl enable mongod

Upvotes: 6

U_R_Naveen UR_Naveen
U_R_Naveen UR_Naveen

Reputation: 788

i had same problem, i have deleted E:\Mongo Data Files\db\mongod.lock file, so it started working. the problem is due to improper shutdown of server. so lock file is not clean.

Upvotes: 0

codeKnightly
codeKnightly

Reputation: 652

if all the solution above doesn't work out perhaps then this might be one to help. Somewhere down the line you may have made connection to your local mongodb database via vagrant boxes.

  1. vagrant up and then ssh into your vagrant machine in which you've made connection with mongodb (vagrant ssh vagrant_box_name)
  2. comment out the bind_ip line from /etc/mongod.conf using # (sudo nano /etc/mongod.conf )
  3. restart your mongodb daemon service (sudo service mongod restart) voila...

Upvotes: 1

Amnesh Goel
Amnesh Goel

Reputation: 2655

This was happening with me today and I resolved it in following way.

Machine: I'm using Windows 10 machine and downloaded the latest MongoDB - Community edition.

So, the problem was, I did not have C:\data\db created.

Without creating C:\data\db, I opened CMD terminal and started the database using mongod command at terminal

C:\YourInstallationPath\bin>mongod

And when I fired mongo command then I was getting the problem.

Twist, I created the necessary folders but still was getting the problem. And this is because mongo server was already running. To fix it, I fired up mongod command again, and it automatically referenced to C:\data\db.

Other users have suggested to add C:\data\db but did not talk about executing mongod again, which exactly solved my problem.

Upvotes: 3

You can try with following command:

sudo service mongod start

Upvotes: 2

Naimish Kher
Naimish Kher

Reputation: 294

First, you have to start mongod server then after in another terminal you have to start mongo shell.

For starting, mongo server

1. If you path is Default path (data/db) simple fired mongod in terminal

2. If your path is not data/db then you command like as follow: mongod --dbpath 'here your path comes' .

For starting mongo shell, Simply fired mongo in another terminal

If you get this waiting for connections on port 27017, then your mongo successfully started.

Hope you can get my point

Upvotes: 0

Rohan pawar
Rohan pawar

Reputation: 31

Simply create a folder named "data" in C drive and inside data folder create another folder named "db". Then execute mongod.exe :)

Upvotes: 0

Jenna
Jenna

Reputation: 2396

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?

Upvotes: 59

Monte Cristo
Monte Cristo

Reputation: 61

First, go to c:\mongodb\bin> to turn on mongoDB, if you see in console that mongo is listening in port 27017, it is OK If not, close console and create folder c:\data\db and start mongod again

Upvotes: 1

Related Questions