Can't connect to MongoDB on WinServer 2019

HI im working on a Windows 2019 VM on this machine I’ve installed MongoDB Community I opend the TCP Port 27017 and 27018 as well as the mongod.exe and mongos.exe but I’m still not able to connect to my MongoDB remotly.

I added a admin user this “mechanisms” : [
“SCRAM-SHA-1”,
“SCRAM-SHA-256”
]

not sure if this is important.

Im not 100% sure if i started MongoDB with Authentication but well…

When im on my VM I can only connect to the IP 127.0.0.1:27017 even with name and passwort but I cant connect to the external IP even tho im on the VM, remotly nothing worked so far.

Is there any way to check what is wrong?

Thank you for your time.

Kind regards Björn

Hi @Bjorn_K

mongod will only bind to 127.0.0.1 by default as a secure option as no authentication method is configured by default.

Check out https://docs.mongodb.com/manual/administration/security-checklist/
and
https://docs.mongodb.com/manual/administration/production-notes/
as well as
https://docs.mongodb.com/manual/administration/production-checklist-operations/

MongoDB Atlas is a good choice for DBaaS to remove this Administrative overhead. They do a really good job with it. (Says the person with self hosted)

2 Likes

change bind ip from localhost to IP of your VMs. so mongodb will be accessible remotely.

1 Like

Thanks that helped a lot, at least now I can connect as long as authentication is off, for some reason if I turn authentication on it cant find my user and autehntication failed even tho I use the same login from remote pc as I use in mongo on the VM.

Hope, you are creating one admin user before enabling Auth. Please follow all steps as per below link and share your input if it works or not.

You can create that first admin user with authentication enabled, as long as it is done via a connection on 127.0.0.1, this is the localhost exception.

Yes so first Installed the msi 4.4.4 from the MongoDB site and started it through the console with mongod, then I started mongo through console and entered

use admin
db.createUser(
{
user: “root”,
pwd: “root”,
roles: [ { role: “userAdminAnyDatabase”, db: “admin” }, “readWriteAnyDatabase” ]
}
)

This is the Output:

Successfully added user: {
“user” : “root”,
“roles” : [
{
“role” : “userAdminAnyDatabase”,
“db” : “admin”
},
“readWriteAnyDatabase”
]
}

then I restarted mongod with mondog --auth --bind_ip_all

when im on the VM I can connect with my credentials with mongo -u root -p root

I tried to connect with Compass and with the Visual Studio Code Plugin from my remote pc and it doesnt work I only get the message in mongod in the console:

{“t”:{"$date":“2021-03-31T05:19:52.856-07:00”},“s”:“I”, “c”:“ACCESS”, “id”:xxxxx, “ctx”:“conn2”,“msg”:“Authentication failed”,“attr”:{“mechanism”:“SCRAM-SHA-256”,“principalName”:“root”,“authenticationDatabase”:“admin”,“client”:“xx.xxx.xxx.xxx:xxxx”,“result”:“UserNotFound: Could not find user “root” for db “admin””}}

If im on the VM and I try to connect to localhost or 127.0.0.1 with compas and the credentials it works fine.
Im pretty sure it cant be the Firewall, because If i set up noauth I can connect from my remote pc

Thanks for your answer I descriped my problem a little bit more detailed as you can see, maybe you know an answer. I dont know how this forum works if everybody get informed as soon as somebody answeres but thanks to you both so far i really appreciate it.

Hi @Bjorn_K

Looks pretty straight forward. The only thing I can think of is perhaps use admin was not performed before the db.createUser()

By default mongo connects to the test database. So given that this works I think you created the user in the test db.

i first wrote use admin -> enter, then i entered the rest is that wrong does it need to be all at once?

I take this back. mongo -u root -p root will connect to the localhost and use admin as the authentication database and use the test db.

mongo -u root -p root test will connect to localhost and use test as the db and the authentication.

That is correct. I’m a little stumped, if the only thing you change is the --auth flag. And auth is working on the when on the mongod host.

Yes it is kinda weird.
So basicly what I did wrong first was to create a user in my individual database with only reading rights and only after that i created a admin user on admin db.

Then I tried to deinstall everything, deleted everyfolder, registry everything and installed it new.
This time I created the admin user first in the admin database as mentioned before.

I am able to login from remote pc when --auth is off.
I am able to login on the VM if auth is off or on.
Im not able to connect when auth if on and I try to connect to the IP adress of the server, then mongod tells me the user doesnt exist. It doesnt matter if I try it on the VM or from my remote pc. Is it maybe because im Using mongo community?

I did a little more try and error maybe I did something in gernal wrong but after I did the following it worked:

  1. start mongod --bind_ip_all
  2. start mongo -host ip adress
  3. use admin (no user here even tho i created some before aperently only on local host…)
  4. create admin user as written above.
  5. restart mongod --auth --bind_ip_all
  6. connect from remote pc --> works

Thanks for your help guys propably i need to setup more now but im happy i got this far.

//but now if i connect with compas it tells me in mongod connection accepted but nothing is hown in compas on my remote pc. if i do it in visual studio code i at least get a message that it was successfully

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.