How to get the MongoDB connection string?

Viewed 9992
3 Answers

Your fresh standalone installation probably has no access control configured yet. So the connection string is:

mongodb://localhost:27017

Otherwise, you have to provide username, password and auth source database in the connection string, e.g.

mongodb://username:password@localhost:27017/?authSource=admin

Continuing from the same steps on the installation docs, there is a section for Connect and Use MongoDB, for connecting to the database from the shell.

$ mongo
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("....") }
MongoDB server version: 4.4.3
...

It should show the connection string it used (you can disregard the rest of the string after ? unless it is authSource):

connecting to: mongodb://127.0.0.1:27017

If you are already connected via shell/terminal, use db.getMongo() to return the connection string in the shell/terminal.

Related