Use SCRAM to Authenticate Clients
On this page本页内容
The following procedure sets up SCRAM for client authentication on a standalone mongod instance.
To use SCRAM authentication for replica sets or sharded clusters, see Deploy Replica Set With Keyfile Authentication.
Procedure
Start MongoDB without access control
Start a standalone mongod instance without access control.
Open a terminal and run the following command as the mongod user:
mongod --port 27017 --dbpath /var/lib/mongodb
The mongod instance in this tutorial uses port 27017 and the /var/lib/mongodb data directory.
The tutorial assumes that the /var/lib/mongodb directory exists and is the default dbPath. You may specify a different data directory or port as needed.
Create the user administrator
Localhost Exception
You can create the user administrator either before or after enabling access control. If you enable access control before creating any user, MongoDB provides a localhost exception which allows you to create a user administrator in the admin database. Once created, you must authenticate as the user administrator to create additional users.
Using mongosh:
- switch to the
admindatabase - add the
myUserAdminuser with theuserAdminAnyDatabaseandreadWriteAnyDatabaseroles":
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: passwordPrompt(), // or cleartext password
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" }
]
}
)
The passwordPrompt() method prompts you to enter the password. You can also specify your password directly as a string. We recommend to use the passwordPrompt() method to avoid the password being visible on your screen and potentially leaking the password to your shell history.
The userAdminAnyDatabase role allows this user to:
- create users
- grant or revoke roles from users
- create or modify customs roles
You can assign your user additional built-in roles or user-defined roles as needed.
The database where you create the user, in this example admin, is the user's authentication database. Although the user needs to authenticate to this database, the user can have roles in other databases. The user's authentication database doesn't limit the user's privileges.
Re-start the MongoDB instance with access control
Shut down the mongod instance. Using mongosh, issue the following command:
db.adminCommand( { shutdown: 1 } )
Exit mongosh.
Start the mongod with access control enabled.
- If you start the
mongodfrom the command line, add the--authcommand line option:mongod --auth --port 27017 --dbpath /var/lib/mongodb
- If you start the
mongodusing a configuration file, add thesecurity.authorizationconfiguration file setting:security:
authorization: enabled
Clients that connect to this instance must now authenticate themselves and can only perform actions as determined by their assigned roles.
Localhost Exception
You can create users either before or after enabling access control. If you enable access control before creating any user, MongoDB provides a localhost exception which allows you to create a user administrator in the admin database. Once created, you must authenticate as the user administrator to create additional users.
Connect and authenticate as the user administrator
Using mongosh, you can:
Next Steps
To use SCRAM authentication for replica sets or sharded clusters, see Deploy Replica Set With Keyfile Authentication.