Enable Authentication on MongoDB

Enable Authentication on MongoDB

This article teaches you how to enable the authentication of MongoDB to ensure the security of your data. I hope you would read my previous article which teaches you how to configure MongoDB and how to do CRUD operations. Please walk through the steps are given below to learn how to do it.

  1. Firstly, Start the MongoDB without credential (as usual)
  2. Then create a new DB called “testMongo”
  3. Next, create user and password to that DB
    db.createUser({user : "myuser", pwd : "xyz123", roles : [{role : "readWrite", db : "testMongo"}]});
  4. Then you have to stop the MongoDB
  5. After that login with authentication as below
    mongod --auth --dbpath ~/Mongodata/
    mongo localhost/testMongo -u 'myuser' -p 'xyz123';
  6. Finally, you will be able to create a collection and insert data as usual

In the step 3 role section, you can set various roles like admin roles; read or write likewise

If you have any issue please follow my YouTube video and search for “authentication of MongoDB”. If you are not compatible with the terminal you can use the MongoDB Compass, it provides a UI.