0

I'm having connection problems with my local mongodb instance. Its not that it is not working, but there are some things that don't work, which I expected to work.

In my case I'm trying to setup my NestJS app with mongo (this tutorial). It states at some point:

MongooseModule.forRoot('mongodb://localhost/blog')

In my case it would be

MongooseModule.forRoot('mongodb://admin:admin@0.0.0.0:27017/blog')

Now whatever I do, it will not connect as long as I have the database name in that string.

I can reproduce this problem with mongosh too

$> mongosh mongodb://admin:admin@0.0.0.0:27017/blog
Current Mongosh Log ID: 640c5eeaaf2a65c11a7dda25
Connecting to:      mongodb://<credentials>@0.0.0.0:27017/blog? directConnection=true&appName=mongosh+1.8.0
MongoServerError: Authentication failed.

But when I remove the database name it works (also in my NestJs app). I'm not very familiar with Mongo, so maybe I'm missing the point completely, but I think this should work and you have to connect to a database before you can do anything. So my question is, what am I doing wrong here? Any help would be apprecited!

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • 1
    0.0.0.0 is not a valid IP address, try using 127.0.0.1 – Joe Mar 11 '23 at 13:24
  • 1
    IP-Address `0.0.0.0` means "all possible IP addresses". Try `localhost` or `127.0.0.1` or `[::1]` or even better the hostname of your machine. And then [Authentication failure while trying to save to mongodb](https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470) could be relevant – Wernfried Domscheit Mar 11 '23 at 13:40
  • when I change it to 127.0.0.1 I get the following error: Error: ENOENT: no such file or directory, open '/Users/jeanluca/blog' – Jeanluca Scaljeri Mar 11 '23 at 19:31
  • If I try `mongodb://admin:admin@127.0.0.1:27017/blog?authSource=admin` I get an error too: `zsh: no matches found: mongodb://admin:admin@127.0.0.1:27017/blogs?authSource=admin`. Maybe mongosh works differently on a mac? – Jeanluca Scaljeri Mar 11 '23 at 19:45

2 Answers2

3

You can use connection string like this :

mongosh "mongodb://user:pwd@localhost:27017/mybase" --authenticationDatabase admin

This will connect directly to your database (mybase) , and your user, pwd will be checked on admin db

Rick
  • 92
  • 6
0

If you have already lunched mongosh, you are already in, you can use the connect() function

connect("<connection string>")
Abraham
  • 12,140
  • 4
  • 56
  • 92