Setting Up User Authentication in MongoDB

Tutorial 2 of 5

1. Introduction

This tutorial aims to guide you through the process of setting up user authentication in MongoDB. User authentication is a critical security feature that protects your data by ensuring only authorized users can access specific resources.

What you will learn:

  • Setting up user authentication in MongoDB
  • Creating users with specific roles and permissions
  • Configuring MongoDB to require authentication

Prerequisites:

  • Basic understanding of MongoDB
  • MongoDB installed on your machine
  • Basic knowledge of JavaScript

2. Step-by-Step Guide

User authentication in MongoDB is implemented via role-based access control. Here, we will create a new user with a specific role and then configure MongoDB to require authentication.

Step 1: Start MongoDB without access control

Start the MongoDB instance without access control to create new users.

mongod --port 27017 --dbpath /data/db1

Step 2: Connect to MongoDB

Connect to the MongoDB instance that you have started.

mongo --port 27017

Step 3: Create the user administrator

In the admin database, add a user with the userAdminAnyDatabase role.

use admin
db.createUser(
  {
    user: "adminUser",
    pwd: "adminPassword",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

3. Code Examples

Here are some examples that show how to create users with different roles.

Example 1: Creating a user with read and write access to a specific database

use records
db.createUser(
  {
    user: "recordsUser",
    pwd: "recordsPassword",
    roles: [
      { role: "readWrite", db: "records" }
    ]
  }
)

In the above example, we first switch to the records database using use records. We then create a new user named recordsUser with the password recordsPassword and assign the readWrite role to the user on the records database.

Example 2: Creating a user with read access to all databases

use admin
db.createUser(
  {
    user: "readUser",
    pwd: "readPassword",
    roles: [
      { role: "readAnyDatabase", db: "admin" }
    ]
  }
)

4. Summary

In this tutorial, we learned how to set up user authentication in MongoDB. We looked at how to create users with specific roles and permissions and how to configure MongoDB to require authentication.

5. Practice Exercises

Here are some exercises for you to try:

  1. Create a user with read and write access to two databases.
  2. Create a user with write access to all databases.

Solutions:

  1. Create a user with read and write access to two databases
use admin
db.createUser(
  {
    user: "doubleDBUser",
    pwd: "doubleDBPassword",
    roles: [
      { role: "readWrite", db: "records" },
      { role: "readWrite", db: "inventory" }
    ]
  }
)
  1. Create a user with write access to all databases
use admin
db.createUser(
  {
    user: "writeUser",
    pwd: "writePassword",
    roles: [
      { role: "writeAnyDatabase", db: "admin" }
    ]
  }
)

For further practice, try creating users with different combinations of roles and permissions.