HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMajor

How to specify authentication database and target database separately on mongodb connection uri?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
separatelyuritargetmongodbauthenticationdatabasehowandspecifyconnection

Problem

I am using this connection uri to connect to mongodb: mongodb://user:password@localhost/admin. It will use admin as bot authentication and target database. How can I make the uri to use admin as authentication but allow me to connect to a different database? Please see below command as an example:

mongo --host localhost -u user -p password --authentication admin test

the above command will use admin as the authentcation database but connect to test database. how can I do the same thing on uri?

Solution

Yes.. Easily!

mongo "mongodb://test:testpwd@localhost/test?authSource=admin"
MongoDB shell version v3.4.6
connecting to: mongodb://test:testpwd@localhost/test?authSource=admin
MongoDB server version: 3.4.6
MongoDB> db
test
MongoDB>


You put your destination DB at end of "base" string and add authentication db to parameter authSource

Code Snippets

mongo "mongodb://test:testpwd@localhost/test?authSource=admin"
MongoDB shell version v3.4.6
connecting to: mongodb://test:testpwd@localhost/test?authSource=admin
MongoDB server version: 3.4.6
MongoDB> db
test
MongoDB>

Context

StackExchange Database Administrators Q#185783, answer score: 34

Revisions (0)

No revisions yet.