[mongodb] Couldn't connect to server 127.0.0.1:27017

I'm getting the following error:

alex@alex-K43U:/$ mongo
MongoDB shell version: 2.2.0
connecting to: test
Thu Oct 11 11:46:53 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed
alex@alex-K43U:/$ 

This is what happens when I try to start mongodb:

* Starting database mongodb                                             [fail]

I already tried mongo --repair

I made chown and chmod to var, lib, and data/db and log mongodb.

Not sure what else to do. Any suggestions?

mongodb.log:

***** SERVER RESTARTED *****


Thu Oct 11 08:29:40 
Thu Oct 11 08:29:40 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 11 08:29:40 
Thu Oct 11 08:29:41 [initandlisten] MongoDB starting : pid=1052 port=27017 dbpath=/var/lib/mongodb 32-bit host=alex-K43U
Thu Oct 11 08:29:41 [initandlisten] 
Thu Oct 11 08:29:41 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 11 08:29:41 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 11 08:29:41 [initandlisten] **       with --journal, the limit is lower
Thu Oct 11 08:29:41 [initandlisten] 
Thu Oct 11 08:29:41 [initandlisten] db version v2.2.0, pdfile version 4.5
Thu Oct 11 08:29:41 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Thu Oct 11 08:29:41 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Oct 11 08:29:41 [initandlisten] options: { config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", logappend: "true", logpath: "/var/log/mongodb/mongodb.log" }
Thu Oct 11 08:29:41 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/var/lib/mongodb/journal"
************** 
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
Thu Oct 11 08:29:41 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Thu Oct 11 08:29:41 dbexit: 
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to close listening sockets...
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to flush diaglog...
Thu Oct 11 08:29:41 [initandlisten] shutdown: going to close sockets...
Thu Oct 11 08:29:41 [initandlisten] shutdown: waiting for fs preallocator...
Thu Oct 11 08:29:41 [initandlisten] shutdown: closing all files...
Thu Oct 11 08:29:41 [initandlisten] closeAllFiles() finished
Thu Oct 11 08:29:41 dbexit: really exiting now

EDIT:

I removed the lock then did mongod repair and got this error:

Thu Oct 11 12:05:37 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

so I did it with sudo:

alex@alex-K43U:~$ sudo mongod --repair
Thu Oct 11 12:05:42 
Thu Oct 11 12:05:42 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 11 12:05:42 
Thu Oct 11 12:05:42 [initandlisten] MongoDB starting : pid=5129 port=27017 dbpath=/data/db/ 32-bit host=alex-K43U
Thu Oct 11 12:05:42 [initandlisten] 
Thu Oct 11 12:05:42 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 11 12:05:42 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 11 12:05:42 [initandlisten] **       with --journal, the limit is lower
Thu Oct 11 12:05:42 [initandlisten] 
Thu Oct 11 12:05:42 [initandlisten] db version v2.2.0, pdfile version 4.5
Thu Oct 11 12:05:42 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Thu Oct 11 12:05:42 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Oct 11 12:05:42 [initandlisten] options: { repair: true }
Thu Oct 11 12:05:42 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/data/db/journal"
Thu Oct 11 12:05:42 [initandlisten] finished checking dbs
Thu Oct 11 12:05:42 dbexit: 
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to close listening sockets...
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to flush diaglog...
Thu Oct 11 12:05:42 [initandlisten] shutdown: going to close sockets...
Thu Oct 11 12:05:42 [initandlisten] shutdown: waiting for fs preallocator...
Thu Oct 11 12:05:42 [initandlisten] shutdown: closing all files...
Thu Oct 11 12:05:42 [initandlisten] closeAllFiles() finished
Thu Oct 11 12:05:42 [initandlisten] shutdown: removing fs lock...
Thu Oct 11 12:05:42 dbexit: really exiting now

But still having the same problem.

This question is related to mongodb

The answer is


This worked for me:

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

I got a similar error but the root cause was different. After installing the mongodb using homebrew. https://docs.mongodb.com/manual/installation/ I have to start the "mongod" service before giving the "mongo" command on terminal.


If you are using OS X systems, there is no service commands. So you won't be able to execure sudo service mongod start.

Check this answer https://unix.stackexchange.com/a/155746 for more help!


You can check with netstat -anp | grep 27017 to see if the port is in use by another process.


I have mongo version 3.2.1 and had to delete the lock file from /data/db/ and after this, ran mongod and it started successfully.

>rm /data/db/mongod.lock
>mongod

Just try:

sudo service mongod restart 

1.Create new folder in d drive D:/data/db

2.Open terminal on D:/data/db

3.Type mongod and enter.

4.Type mongo and enter.

and your mongodb has strated............


I followed the doc at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/.

After configured and reboot, I executed sudo service mongod start and got ... [FAILED].

At last, I found that mongod had started. I think the yum install added it to auto start.

To check if your mongod is running: service mongod status .

Hope this can help someone has same problem.


Check your filesystem free space and increase it if its less. This also could cause the mongo not to start. Check the /var/log/mongodb/mongodb.log file.

ERROR: Insufficient free space for journal files
Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles

so first you have to remove the mongod.lock file by below command

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

and then restart the mongo service by issuing below command

sudo service mongod restart 

In the terminal run these commands

1)

$ sudo service mongod start

2)

 $ mongo

Try running mongod before mongo.

sudo /usr/sbin/mongod on my opensuse

This solved my problem,


After frequent attempt finally I got to troubleshoot the problem...

Step 1: ps aux | grep mongo
Step 2: sudo rm /var/lib/mongodb/mongod.lock
Step 3: sudo mongod --repair
Step 4: mongo

Did you run mongod before running mongo?

I followed installation instructions for mongodb from http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/ and I had the same error as you only when I ran mongo before actually running the mongo process with mongod. I thought installing mongodb would also launch it but you need to launch it manually with mongod before you do anything else that needs mongodb.


Adding the bin to PATH in Environment Variables helped.

GOTO Installation Path and copy the ../bin to the PATH variables in environment variables in Windows


After removing mongod.lock which was inside the data directory in my windows OS,it was still showing the same error message. I had to run mongod with --dbpath to make the mongo command run without errors.


type windows+r and enter the following

services.msc

start MongoDB

now type "mongo" in cmd in the respective path where the mongo.exe is present, it will start working.


In windows run cmd as Admin:

  1. Create directory:

    mkdir c:\mongo\data\db

  2. Install service:

    mongod.exe --install --logpath c:\mongo\logs --logappend --bind_ip 127.0.0.1 --dbpath c:\mongo\data\db --directoryperdb

  3. Start MongoDB:

    net start MongoDB

4.Start Mongo Shell:

c:\mongo\bin\mongo.exe

This solution work fine for me


Sample solution Press Window+R and type services.msc to view all the services on machine Then find MongoDBServer right click it and start it. Then go to C:\Program Files\MongoDB\Server\4.2\bin and type mongo on command promt.


Just run mongod --repair from C:\Program Files\MongoDB\Server\4.0\bin

Here is the doc https://docs.mongodb.com/manual/tutorial/recover-data-following-unexpected-shutdown/


MAC OS

if you have mac and useing mongodb4.4, you can run:

brew services start [email protected]

simple Run Two commends

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

This it's because the mongod process it's down, you must to run the commands bellow in order to get up the mongod process:

~$ 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

Hope this helps you.


Although the answers are received, I would wish to discuss about network errors in MongoDB.

Network errors MongoDB

Setting the safe write concerns is not the full proof method to make sure that we're safe. Let's assume that w=1 & j=true are set, what if the write acknowledgement didn't received from the server? Well, the likelyhood is it didn't happened, but it might have happened. The reason why this might have happened is that there are network errors - there are reasons that we may not receive an affirmative response. So, we can send the request from the application through a driver of language of choice. mongod can complete it successfully and then there could be a TCP reset, and the network actually can get reset in a way that we never receive response. So, we could get an error and on the error, we might assume that we got an error. It didn't happened, but it may happen.

For an insert, it's possible to guard against it. It's possible because if we let the driver create the _id and we do an insert - then we could do that insert multiple times and it would be any harm. Because, if we do this 1st time and we get an error and we're not sure whether or not that insert completed because it's a network error, then we could just do it again. And provided we perform it again, tyr to perform it with the exact _id. The worst case scenario is we'll get a duplicate key error when we try to insert it.

However, an update is where the problem occurs. Especially, the update which is not item potent, that for instance included a $ink command. So, we're telling the database to increment a certain field. Well in that case, if we get a network error and we don't know whether or not the update occurred. Now, maybe we know enough about the values that we can check with them that the update occurred, which is fine. But if we don't know the starting value in the database for that field, then it's not possible for us to know whether or not it occurred or not in case of network error. This kind of issues are extremely rare with a fine network.

And if we really need to avoid it at all costs, what we need to do is turn on all our updates into inserts, by reading the full value of the document out of the database and then potentially deleting it and inserting it again or just inserting a new one.

The reasons why an application may receive an error back even if the write was successful:

  • The network TCP connection between the application and the server was reset after the server received a write but before a response could be sent.
  • The MongoDB server terminates between receiving the write and responding to it.
  • The network fails between the time of the write and the time the client receives a response to the write.

Err : Couldn't connect to server 127.0.0.1:27017

This is solution for WINDOWS users enter code here 1. Create directory:

mkdir c:\mongo\data\db
  1. Install service: first go to your bin directory in programs files of MongoDB with cd

mongod.exe --install --logpath

  1. Start MongoDB:

    net start MongoDB

4.Start Mongo Shell:

`c:\mongo\bin\mongo.exe` or

go to until bin and enter mongo

Note : Open terminal in Administrator mode


This error could be caused by the MongoDB's bind IP setting. You can check MongoDB's config file by

$ sudo vi /etc/mongodb.conf

In my case, the bind IP is set to server's intranet address, just as following:

bind_ip = 10.10.1.14 
#port = 27017

So I have give mongo an IP parameter to connect to shell by type:

$ mongo 10.10.1.14

Don't forget to restart mongodb service if you changed the config.


Try

sudo service mongodb start

This solved my issue.


Ubuntu 18.04LTS: The problem arises when I uninstalled my previous version completely and installed 4.2.6

After hr's of googling, I solved another problem

MongoDB Failing to Start - ***aborting after fassert() failure

I was hopeless about the problem couldn't connect to server, because everything seems ok.

Finally, I decided to restart the operating system and guess what... BINGO

sudo mongo // works like a charm

This Works for me To stop the mongodb use:

use admin
db.shutdownServer()

And to Restart:

sudo service mongod restart

Step 1: Remove lock file.
sudo rm /var/lib/mongodb/mongod.lock

Step 2: Repair mongodb. 
sudo mongod --repair 

Step 3: start mongodb.
sudo start mongodb 
or
sudo service mongodb start

Step 4: Check status of mongodb.
sudo status mongodb 
or   
sudo service mongodb status

Step 5: Start mongo console.
mongo 

For future reference,Follow these steps to avoid similar errors:

1.Download MondoDB https://www.mongodb.com/

2.Open a terminal and cd into your downloads folder or whatever folder youve saved your mondodb download(Ensure you extract your mongodb folder before you cd into it)

cd Downloads

3.Move mongodb to your usr/local path

sudo mv mongodb-osx-... /usr/local/mongodb

4.cd into your local folder

cd /usr/local/mongodb

5.make a new directory

sudo mkdir -p /data/db

6.cd into the new directory just created above

cd /data/db

7.give mongo permisions

sudo chown YourMacUserName /data/db

8.Then go/open your .bash_profile

To do so,follow these steps:

In your a new terminal

1.cd 2.pwd 3.ls -l

Check if the .bash_profile appears on your list of files on your terminal

if not create the -bash_profile

Creating .bash_profile:

In your terminal

touch .bash_profile

//skip this step if you already have a .bash_profile

Step8:

Next in your terminal:

open .bash_profile

And in your bash file that pops open add the following:

MONGO_PATH=/usr/local/mongodb
export PATH=$PATH:$MONGO_PATH/bin

And then save.(File Save or command S/CMD+S)

Step9:back in your terminal:

source .bash_profile

Now open two terminals.One will for your mondo daemon the other for your mongo.

Terminal 1: in your terminal type:mongod

mongodb

Output: Mongod Terminal

Terminal 2:

mongo

Output: Mongo terminal

Also make sure you dont make the following typo error when starting your mongod in your terminal: This is incorrect

mongo d

gives off the following error: Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused

This is Correct:

mongod

(There should be no space between the words mongo and d..mondod

Finally always keep in mind that you have to run mondod before you run mongo on your terminals.