49

I just want to try MongoDB Compass GUI for MongoDB, but when I try to connect using local host port: 27017 I get this error:

Could not connect to MongoDB on the provided host and port

I followed these instructions on YouTube.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85

15 Answers15

53

I had faced this issue today and later I found that it was a set up problem. If you are also facing this issue while you set up MongoDB for the first time, following steps may help you. Make sure that MongoDB service has been started in services.msc.

  1. Go to C drive and create a folder db in the path C:\data\db, if you don't see data folder in C drive, please create it first and then db folder as @sebastian mentioned in the comment.
  2. Go the folder, where you had installed MongoDB, in my case, it was C:\Program Files\MongoDB\Server\3.6\bin
  3. Type the command C:\Program Files\MongoDB\Server\3.6\bin>mongod
  4. This will set up the MongoDB on your machine, and you will get an output as preceding.

C:\Program Files\MongoDB\Server\3.6\bin>mongod 2018-03-11T07:02:56.558-0700 I CONTROL [initandlisten] MongoDB starting : pid=18720 port=27017 dbpath=C:\data\db\ 64-bit host=SibeeshVenu 2018-03-11T07:02:56.558-0700 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2

  1. Now if you try connecting, you may not be getting the issue.

enter image description here

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
35

Press windows key button and search Services and find MongoDB then start. Then try again. It will work

BIS Tech
  • 17,000
  • 12
  • 99
  • 148
  • 2
    Thanks a lot, this did the trick for me ... i knew it was a mater of a simple restart, cause every thing worked fine before and i didn't made any change on my config –  Aug 10 '18 at 23:59
  • Super. Why did this came to my mind earlier... Thanks a lot – Amit Jul 01 '19 at 15:00
  • Worked well --- It's strange that this works, but dosen't work when restarting from cmd promt... – Avaricious_vulture Dec 13 '19 at 17:44
19

In case other people have this error, here is what worked for me:

  • Edit /etc/mongod.conf
  • Under network comment out the following by placing a # at the beginning of the line:

    bindIp: 127.0.0.1

-- What went wrong: --

Reason: bindIp was causing it to reject external requests, so we need to remove that setting.

My mongo was the standard yum install for Red Hat (RHEL6) and this is how it was configured by default (I had started with mongoDB 2.6 and upgraded in steps to 3.4).

EDIT: For MongoDB 2.4 and earlier use: bind_ip = 127.0.0.1

Bastion
  • 761
  • 10
  • 10
14

Struggled for 3 hours.
Here is full procedure in 3 steps:


On MongoDB server machine

Get the IP adresss:

  1. run CMD or Command prompt
  2. run ipconfig command
    Look for a line that is like below
    IPv4 Address. . . . . . . . . . . : 10.111.11.111
  3. Copy above IP value, which is 10.111.11.111, it can be different for you

Edit the bindIP in MongoDB Server config file:

  1. Open C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg
    Look for the below line:
    bindIp: 127.0.0.1
  2. Add a COMMA at the end, and paste the copied IP, like below:
    bindIp: 127.0.0.1,10.111.11.111

NOTE: If your IP address changes everyday, then best is to write the computer name (type hostname in cmd to get the name of your computer)

Restart the MongoDB Server

  1. run the services.msc command
    scroll down on the right to select MongoDB Server
    on the left-top, Restart the service

On Client machine

Connect to MongoDb server, from another machine

Now, you can connect from another machine. Use a client, like MongoDB Compass or other:

  1. Connection is possible using it's IP 10.111.11.111, give it in Hostname in MongoDB Compass client
  2. Hostname in MongoDB Compass client can also be the server's computer name like W11ABC11A11. To get computer name of your MongoDB server, use hostname in CMD(on the MongoDB server computer).

Troubleshooting/ issues?

After doing all above, if you have issues, then:
- ping each other (client from server, and vice versa) using ipconfig, using command ping IPhere , you should get Reply from .. should not get Request timed out.
- if you see issues in above:
-- Restart the client machine, ping again
-- If you still have issues, have DB owner to restart the server machine once (if it is dev server there should not be any problem to restart when its idle.)
-- issues still? verify all above steps

Hope that helps.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
  • This is the only thing that worked for me. Thank you! – Josh Weston Dec 15 '19 at 16:29
  • 1
    For me, comma separating with 0.0.0.0 worked (instead of entirely replacing the existing IP). So I have bindIp: 127.0.0.1,0.0.0.0. Hoping this means I don't need to change the IP if the host IP is dynamic. – Josh Weston Dec 15 '19 at 20:26
9

I had the same problem

  1. try to enable cmd enter the command -> mongod
  2. then try to connect using the MongoDb Compass program You must have an open console all the time
5

edit /etc/mongod.conf

Under the net settings

bindIp: 0.0.0.0
vezunchik
  • 3,669
  • 3
  • 16
  • 25
Likwidsage
  • 81
  • 2
  • 5
4

It's a common problem if you are working on Windows.

Open your task manager and go to the Services tab. Check if the MongoDB server is stopped or not. If stopped, right click on it and start the server. Then try again. The problem will be solved.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Supriyo Das
  • 111
  • 1
  • 2
  • OP didn't specify that they were working in a Windows environment. Not all operating systems have a Task Manager. – mypetlion Jun 24 '19 at 17:20
1

Wasted a lot of time on this, seemingly tried it all and literally none of these solutions worked for me...

Ultimately, my MongoDb seems to want to ignore my config file, since commenting out the bind_ip field should do the same thing.

Try starting mongo like this

mongod --bind_ip_all

Alternatively, you can specify specific IPs

mongod --bind_ip <IP>
yojake42
  • 109
  • 1
  • 5
0
  • please start mongoDB before use Compass GUI
0

My MongoDB Installed machine firewall was enabled that's why I can not from Remote machine MongoDB compass

After disabled the firewall, I am able to connect from Remote MongoDB compass.

[17:32:51 oracle@test2 bin]$ service firewalld stop

Thanks

Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
0

Check if 27017 port is enabled / Opened in your .conf file .. This is because 27017 port is not opened as default and also you need to add your IP in the TCP IN to make your internet IP connect remotely..

0

It's a common problem if you are working on Windows.

Creating data/db folder on c drive and then running mongod from C:\Program Files\MongoDB\Server\4.2\bin and then connecting from mongodb compass resolves my issue

basicly on c drive you should have data folder which holds db folder that's what i understood by this i just shared someone might get help from this :)

Hanzla Habib
  • 3,457
  • 25
  • 25
0

I was having similar problem while using MongoDB on EC2 instance and was trying to connect to it through Mongodb compass.

On Mongodb Compass authentication screen, changing the port to 443 worked because my mongod service was listening on 443. enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
jitin14
  • 144
  • 5
0

I am on Ubuntu and I simply did it by checking the 'mongod.conf' file. Make sure that following parameters are set:

net:
  port: 27017
  bindIp: 127.0.0.1

And after every change I suggest you go with

sudo systemctl restart mongod.service

Kubra Altun
  • 365
  • 3
  • 12
0

By default IPv6 disabled and this may be a reason of the failed connection by Hostname. Try to add IPv6 support:

# network interfaces
net:
  ipv6: true
  port: 27017
  bindIp: 127.0.0.1, MyDomain.com
EminST
  • 59
  • 5