Jubair Haider
Jubair Haider

Reputation: 41

MongoDB: expection: connect failed, exiting with code 1

So after installing mongodb in my Ubuntu, I tried to run "mongo", but it said,

MongoDB shell version v4.4.1
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 :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

So I enabled mongod service and started it, then ran the command,

sudo systemctl status mongod

And It said,

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2020-09-17 00:23:08 +06; 8min ago
       Docs: https://docs.mongodb.org/manual
    Process: 45414 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, sta>
   Main PID: 45414 (code=exited, status=1/FAILURE)

Sep 17 00:23:08 john systemd[1]: Started MongoDB Database Server.
Sep 17 00:23:08 john mongod[45414]: about to fork child process, waiting until server is>
Sep 17 00:23:08 john mongod[45427]: forked process: 45428
Sep 17 00:23:08 john mongod[45414]: ERROR: child process failed, exited with error numbe>
Sep 17 00:23:08 john mongod[45414]: To see additional information in this output, start >
Sep 17 00:23:08 john systemd[1]: mongod.service: Main process exited, code=exited, statu>
Sep 17 00:23:08 john systemd[1]: mongod.service: Failed with result 'exit-code'.

And I can't run the mongodb shell. What should I do?

Upvotes: 4

Views: 10465

Answers (5)

Shalu
Shalu

Reputation: 11

  • Create a folder data in root C: directory.
  • Create another folder db inside data folder.
  • Now run mongod in cmd in path C:\Program Files\MongoDB\Server\5.0\bin>mongo
  • Don't close this command prompt.
  • Open another cmd in same path C:\ProgramFiles\MongoDB\Server\5.0\bin>mongo
  • Run mongo command.

Now it will connect.

Upvotes: 1

Alfredo Diaz Claro
Alfredo Diaz Claro

Reputation: 1

You have yo go /etc , modify the mongod.conf, because: "By default, MongoDB launches with bindIp set to 127.0.0.1,", which binds to the localhost network interface. This means that the mongod can only accept connections from clients that are running on the same machine.

Then could sudo nano mongod.conf and change 127.0.0.1 to 0.0.0.0

You must restart mongo.

Upvotes: 0

OsamaD
OsamaD

Reputation: 471

I came across this issue yesterday and I was able to resolve it by:

  • removing the mongod.lockfile.
  • running the config fork command.

remove .lock file:

sudo rm /usr/local/var/mongodb/mongod.lock

Run:

mongod --config /usr/local/etc/mongod.conf --fork.

and use the mongo command again.

Upvotes: 1

Mohamed Murshidh
Mohamed Murshidh

Reputation: 1

I also got that same error , I think this may happened because of some updation in our PC (like .NET framework updation something)

then I uninstalled and reinstalled MongoDB again and its working

Upvotes: 0

Ewoks
Ewoks

Reputation: 12435

mongod need to be running before you can run mongo without that error.

p.s. here is the answer for others who stumble upon original question from the title

Upvotes: 1

Related Questions