226

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
Tampa
  • 75,446
  • 119
  • 278
  • 425
  • 3
    As IMO this has nothing to do with programming per se, I'd recommend you to ask this question on http://serverfault.com/. – cimnine Nov 09 '12 at 16:52
  • 2
    I was also facing same issue. Its cus mongo Not able to creates folder in C:/data/db I followed this tut http://www.mkyong.com/mongodb/how-to-install-mongodb-on-windows/ – STEEL Mar 12 '14 at 05:35
  • I have faced the same error when using my company laptop and installing MongoDB on C:. Because of administrative privileges and the fact I am prompted with my admin username and password every time I install something or modify anything in C:, that was the reason I could not get mongodb working. After installing it on D:, all worked fine. – panza Nov 17 '16 at 09:36
  • an host Alias could be jamming access to 127.0.0.1; If you are on Ubuntu (or any other linux distro) try opening /etc/hosts and see if there is any Alias for 127.0.0.1 and comment it out. Save the host file, restart your computer and try starting the mongod service again (sudo systemctl start mongod). – Arthur Zennig Dec 29 '20 at 09:15
  • with me the problem was a junk cleaning software 'clean my Mac'. It stopped the mongo server and it's cache. – Faizan Pasha Aug 07 '22 at 19:52

46 Answers46

321

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
Ravi Hamsa
  • 4,721
  • 3
  • 14
  • 14
  • 4
    This case worked out for me, i had to do ~>sudo mongod --dbpath /dbpathlocation and then opened the new terminal to run mongo... – Saji Dec 08 '14 at 03:26
  • 5
    what worked for me on macOS Sierra was `sudo mongod` and then `mongo` – Amin Jafari Nov 17 '16 at 21:14
  • I had to create a /data/db directory before running mongod – College Student Jan 10 '19 at 05:01
  • I can't believe that all I had to do is to run `mongod` in background or another shell before running `mongo`. Shout out to [Ravi Hamsa](https://stackoverflow.com/users/1887798/ravi-hamsa)!. – KareemJ Jun 10 '19 at 09:19
103

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/

shakthydoss
  • 2,551
  • 6
  • 27
  • 36
  • 4
    Just wondering, what is the /var/lib/mongodb/mongod.lock and what went wrong with it? – ozn Mar 26 '18 at 03:20
  • 2
    Item 2 in Ubuntu 18.04 is `mongod --repair` – Aline Matos Sep 05 '19 at 12:08
  • 1
    This answer needs serious updation, both the link are not working and give ``404 not Found``. The 1st step of removing the .lock file is not a **recommended practice**. I just lost all my data. please refer : https://docs.mongodb.com/manual/tutorial/recover-data-following-unexpected-shutdown/#id1 – Afroz Chakure Jul 30 '20 at 05:51
  • didnt work in my case. shows the same error. i have checked using netstat but port 27017 is not in use – Aditya Gaddhyan Oct 01 '20 at 17:04
  • What's the path for MACOS? – Menai Ala Eddine - Aladdin Apr 25 '21 at 01:33
  • @MenaiAlaEddine-Aladdin I guess the issue must be resolved for you but for future visitors, it's generally `/tmp/mongodb-27017.sock`. The path and exact error can be seen by running `mongod`. Hope this helps someone – Fr0zenFyr Jun 15 '21 at 17:15
64

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

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
poorva
  • 1,726
  • 1
  • 17
  • 15
59

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?
Bugs
  • 4,491
  • 9
  • 32
  • 41
Jenna
  • 2,386
  • 17
  • 10
54

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

Blasanka
  • 21,001
  • 12
  • 102
  • 104
42

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

William Hu
  • 15,423
  • 11
  • 100
  • 121
23

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
Ali A
  • 359
  • 3
  • 5
17

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
Finn
  • 2,707
  • 2
  • 19
  • 28
  • Removing the ip/port did not work for me. It actually broke the server even more. The service refused to start back up again... – Cerin Jun 18 '17 at 03:38
  • 2
    Be careful with bind_ip. If you comment that out, it will allow remote connections to your database which is obviously a terrible idea unless you have security and auth setup. I believe MongoDB has no users or password by default, so you need to be aware of that. – agm1984 Jun 27 '17 at 17:19
  • Work for me too, but dont't know about security after the comment of @agm1984, I will just use this in development mode and after I will use the Atlas Cloud of Mongodb, more secure in my case I think (with my poor server's skills :D) – Adrien V Oct 19 '19 at 19:55
  • 1
    @AdrienV sure, it's risky if you remove the local IP binding because it will allows to access from the internet. This is just a work around for the development server. – Finn Oct 21 '19 at 02:01
  • bind_ip was set to an additional unknown ip. Deleting it solved the /db/data error ... ufff – zevero Nov 22 '19 at 21:43
17

A little to late with the answer but I've encountered the same issue

Following are the steps which helped me.

  1. Go to C:\
  2. Create "data" folder
  3. In "data" create "db" folder
  4. open terminal (CMD) -> go to EX:"c:\MongoDB\Server\3.4\bin"
  5. type in mongod => this will start mongo server (leave it opened)

Run using a GUI, EX "robomongo"

Or

open new terminal (CMD) -> go to EX:"c:\MongoDB\Server\3.4\bin" type in "mongo" command

Metin Atalay
  • 1,375
  • 18
  • 28
16

Make sure that your mongo is running. I fixed this problem by trying to repair the mongodb, there I found that the directory required for the db to run was not created. It shows this error

Type: mongod, it will show the error

exception in initAndListen: 29 Data directory /data/db not found., terminating

Error happen because dbpath /data/db/ (default config) does not exist. You need to create data folder and set permission for it.

Then I create a folder on my system by the command

sudo mkdir -p /data/db/ and sudo chown id -u /data/db

then I run the mongo again and it worked.

Finn
  • 2,707
  • 2
  • 19
  • 28
15

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
RobC
  • 22,977
  • 20
  • 73
  • 80
Rohit Gaidhane
  • 321
  • 2
  • 12
6

If all the above solution does not work: go to service (start>search>services) and start mongodb service. Then, in a cmd prompt, after going to bin, type :/>mongo

John Powell
  • 12,253
  • 6
  • 59
  • 67
Khalid
  • 664
  • 7
  • 16
6

To connect with mongo we have to first start 'mongod' services. following output you can see :

$ mongod
2017-03-05T00:31:39.055+0530 I CONTROL  [initandlisten] MongoDB starting : pid=1481 port=27017 dbpath=/data/db 64-bit host=Prabhu-Nandans-Mac.local
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten] db version v3.4.2
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten] git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2k  26 Jan 2017
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten] allocator: system
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten] modules: none
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten] build environment:
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten]     distarch: x86_64
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten]     target_arch: x86_64
2017-03-05T00:31:39.056+0530 I CONTROL  [initandlisten] options: {}
2017-03-05T00:31:39.056+0530 W -        [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
2017-03-05T00:31:39.057+0530 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-03-05T00:31:39.057+0530 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
2017-03-05T00:31:39.057+0530 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-03-05T00:31:39.620+0530 I CONTROL  [initandlisten] 
2017-03-05T00:31:39.620+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-03-05T00:31:39.620+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-03-05T00:31:39.620+0530 I CONTROL  [initandlisten] 
2017-03-05T00:31:39.620+0530 I CONTROL  [initandlisten] 
2017-03-05T00:31:39.620+0530 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2017-03-05T00:31:39.643+0530 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-03-05T00:31:39.643+0530 I NETWORK  [thread1] waiting for connections on port 27017
2017-03-05T00:31:40.008+0530 I FTDC     [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
2017-03-05T00:32:03.832+0530 I NETWORK  [thread1] connection accepted from 127.0.0.1:49806 #1 (1 connection now open)
2017-03-05T00:32:03.833+0530 I NETWORK  [conn1] received client metadata from 127.0.0.1:49806 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.2" }, os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "16.4.0" } }
2017-03-05T00:32:08.376+0530 I -        [conn1] end connection 127.0.0.1:49806 (1 connection now open)

After that open another terminal and just type 'mongo'.

Following is output you can see :

$ mongo
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2

Your issue solved :)

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Prabhu Nandan Kumar
  • 1,205
  • 12
  • 22
6

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
4

Start monogdb service using

sudo service mongod start 

then from the new window or terminal start mongo client using

mongo
4

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
Natasha
  • 6,651
  • 3
  • 36
  • 58
4

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
bensbenj
  • 391
  • 3
  • 7
3

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.

Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
  • 2
    For those that installed mongodb-community using brew on Mac-OS Try this out. `brew services start mongodb-community` or `brew services restart mongodb-community` for those that had started the service already – Fahd Jamy Sep 07 '20 at 09:30
3

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.

Mandeep Gill
  • 4,577
  • 1
  • 28
  • 34
  • I don't know the cause of the issue but it doesn't work for me, I had to do 'sudo brew services start mongodb-community'. – Natasha Jan 27 '21 at 09:53
  • 1
    Mostly you don't use sudo on brew and if you are using sudo then it means you have permissions issue. – Mandeep Gill Jan 27 '21 at 16:40
3

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

sudo service mongod start
Rahul
  • 3,293
  • 2
  • 31
  • 43
2

You can try with following command:

sudo service mongod start

  • almost there, but if you have installed with brew then it won't work. You gotta do 'sudo brew services start mongodb-community' – Natasha Jan 27 '21 at 09:51
2

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
ysf
  • 4,634
  • 3
  • 27
  • 29
1

Remove mongod.lock file. And run "mongod -repair". uninstall/reinstall mongod service fixed that problem for me.

Thanks.

Tun
  • 824
  • 3
  • 16
  • 30
1

Another solution that resolved this same error for me, though this one might only be if you are accessing mongo (locally) via a SSH connection via a virtual machine (at least that's my setup), and may be a linux specific environment variable issue:

export LC_ALL=C

Also, I have had more success running mongo as a daemon service then with sudo service start, my understanding is this uses command line arguments rather then the config file so it likely is an issue with my config file:

mongod --fork --logpath /var/log/mongodb.log --auth --port 27017 --dbpath /var/lib/mongodb/admin
edencorbin
  • 2,569
  • 5
  • 29
  • 44
1

Create the folder C:data/db if it doesn't already exist on your computer. Turns out MongoDB requires the existence of this folder structure 'data/db' to run. I hope this helps someone.

Amin Balogun
  • 151
  • 2
  • 5
1

The cause by me was the space - run this in the console:

df -h

or more specifically:

du -hs /var/lib/mongodb/ 

to check he disk usage. If it's ~ 99% - just clean up some space and try again!

tsveti_iko
  • 6,834
  • 3
  • 47
  • 39
1

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

1

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...
Ashwin J
  • 662
  • 7
  • 12
1

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
Code Tree
  • 2,160
  • 4
  • 26
  • 39
1

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.

kiyatrowes
  • 21
  • 3
1

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.

Ani David
  • 131
  • 1
  • 7
1

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

0

I had the same error when connecting to Mongo on AWS EC2 . This is what solved it https://serverfault.com/a/347159/62381 Remove the bind_ip from the config file

Community
  • 1
  • 1
Chenna V
  • 10,185
  • 11
  • 77
  • 104
0

This work me:-

In Mongo 3.2 for window, you should start Mongodb service. So, run the Command Prompt as Administrator then net start MongoDB

vineet
  • 13,832
  • 10
  • 56
  • 76
0

Finally I got this is in a simple way..

open terminal and go to the mongodb location. In my case it is

e:\mongodb\bin> 

and type the following command and press enter..

mongod --config e:\mongodb\mongo.config

Open another terminal and start the mongodb using

mongo.exe

Thats it.. you can use mongo

0

This is an old question, but, as a newbie I figured I'd add that (using Webstorm) you need to click on Mongo Explorer (tab on far right hand side of your window). From there:

-Click settings. -Click browse button (looks like a button with "..." on it). -Go to Program Files > MongoDB > Server > 3.4 > bin > mongo.exe (your version maybe different than 3.4) -After that, click the green plus sign on the right. A window will open -Enter a name in "Label" slot. -Click apply.

You should now be connected. Entering "mongod" in the terminal isn't enough. You have to do the above steps also.

My apologies in advance to those of you who aren't using Webstorm! Not sure about the steps using other IDE's.

0

if u install by brew (on osx) first run sudo mkdir /data/db start mondoDB Daemon by typing mongod (leave it open) and then run mongo by typing mongo in new terminal tab

majid
  • 1
0

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

0

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

Naimish Kher
  • 284
  • 1
  • 9
0

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.

0

@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.

Mandeep Gill
  • 4,577
  • 1
  • 28
  • 34
GAltelino
  • 95
  • 2
  • 12
0

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
Unheilig
  • 16,196
  • 193
  • 68
  • 98
0

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

Gabriel Arghire
  • 1,992
  • 1
  • 21
  • 34
0

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.

Meriem Bader
  • 112
  • 2
  • 15
0

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 &
crifan
  • 12,947
  • 1
  • 71
  • 56
0
  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.
Jcc.Sanabria
  • 629
  • 1
  • 12
  • 22