MongoDB Documentation, Release 3.0.4 Step 1: Specify your credentials when starting the mongo instance. When using mongo to connect to a mongod or mongos , enter your username , password , and authenticationDatabase . For example: Step 2: Close the session when your work is complete. To close an authenticated session, use the logout com- mand.: Step 1: Connect to a MongoDB instance. Connect to a mongod or mongos instance. Step 2: Switch to the database to which to authenticate. Step 3: Authenticate. Use either the authenticate command or the db.auth() method to provide your username and password to the database. For example: Step 4: Close the session when your work is complete. To close an authenticated session, use the logout com- mand.: This section describes how to generate a key file to store authentication information. After generating a key file, specify the key file using the keyFile option when starting a mongod or mongos instance. A key’s length must be between 6 and 1024 characters and may only contain characters in the base64 set. The key file must not have group or world permissions on UNIX systems. Key file permissions are not checked on Windows systems. MongoDB strips whitespace characters (e.g. x0d , x09 , and x20 ) for cross-platform convenience. As a result, the following operations produce identical keys: echo -e "my secret key" > key1 echo -e "my secret key
" > key2 echo -e "my secret key" > key3 echo -e "my\r
secret\r
key\r
" > key4 db.runCommand( { logout: 1 } ) db.auth( "prodManager", "cleartextPassword" ) use <database> db.runCommand( { logout: 1 } ) mongo --username "prodManager" --password "cleartextPassword" --authenticationDatabase "products" Authenticate After Connecting to MongoDB Generate a Key File Overview 379 6.3. Security Tutorials