Authenticate to a MongoDB Instance or Cluster¶
To authenticate to a running mongod or mongos instance, you must have user credentials for a resource on that instance. When you authenticate to MongoDB, you authenticate either to a database or to a cluster. Your user privileges determine the resource you can authenticate to.
You authenticate to a resource either by:
- using the authentication options when connecting to the mongod or mongos instance, or
- connecting first and then authenticating to the resource with the authenticate command or the db.auth() method.
This section describes both approaches.
In general, always use a trusted channel (VPN, SSL, trusted wired network) for connecting to a MongoDB instance.
You must have user credentials on the database or cluster to which you are authenticating.
Authenticate When First Connecting to MongoDB¶
Authenticate After Connecting to MongoDB¶
Switch to the database to which to authenticate.¶