search
HomeWeb Front-endJS TutorialIntroduction to users and permissions in mongoDB

The content of this article is an introduction to users and permissions in mongoDB. It has a good reference value and I hope it can help friends in need.

Preface

For databases, users and permissions are a very important part, because they involve security. So what are the users and permissions of mongoDB?

Instructions

Environment Description

The mongoDB version used in this article is 3.6, and the operating system is windows.

Other instructions

Due to space limitations, this article will not introduce the process from downloading to installing the database. Regarding installation tutorials, there are a large number of tutorials on the Internet. You can follow these tutorials to install and run it. This article will focus on the users and permissions part of mongoDB.

Server and client

For mongoDB, it is divided into server and client.
In the installation directory of the windows environment, double-click to open mongod.exe to start the mongoDB service.
When the service is started, you can double-click mongo.exe to open the client to connect to the mongoDB service.

Enable authorization mode

After mongoDB is installed, if you directly use mongod.exe to start the service, the default is not to enable authorization mode, if your mongoDB does not enable authorization mode , then anyone can log in to the mongoDB server without a username and password, and do whatever they want with your database, or even directly delete the database and run away. Therefore, in a production environment, please make sure you remember to turn on authorization mode.

So, how to enable authorization mode?
Open cmd, enter the bin directory of the installation directory, and execute the following command:

mongod --auth --port 27017 --dbpath /data/db

After turning on the authorization mode, open mongo.exe, and under the admin database, execute show dbs, at this time, the database will report an error, reminding you that there is no authorization. As follows:

Introduction to users and permissions in mongoDB

##User type

mongoDB database is roughly divided into two types of users, one is the administrator user, the other is is an ordinary user.

Administrator

We create an administrator user (userAdmin or userAdminAnyDatabase role) in the admin database. The administrator user can manage ordinary users.

First, open the mongoDB service in
non-authorized mode.

mongod --port 27017 --dbpath /data/db
Then enter the admin database and execute the following command:

use admin
db.createUser(
  {
    user: "larry",
    pwd: "123456",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
When prompted Successfully added user, it proves that the administrator user has been added successfully.

Normal User

When the administrator user is successfully created, we can use this administrator user to create a normal user for each database.

First, close all mongo shell windows above.
Then open the mongoDB service in
authorization mode.

mongod --auth --port 27017 --dbpath /data/db
Open the mongo.exe client, enter the admin database, and log in with

db.auth().

Introduction to users and permissions in mongoDB

The first parameter is the administrator user name larry created above, and the second parameter is the password of the administrator user larry.

The result returns 1, indicating that the administrator larry logged in successfully.
Next, use this administrator to create a normal user moddx for the photo_app database, and specify its permissions as readWrite.

use photo_app
db.createUser(
{
  user: "moddx",
  pwd: "123456",
  roles: [{ role: "readWrite", db: "photo_app"}]
}
)
View users

All global accounts

First, log in to the admin database with the

admin account, and then execute the following command:

db.system.users.find().pretty()

Introduction to users and permissions in mongoDB

Accounts under the current library

View all global accounts, only administrators can view them, and view the accounts in the current library, Both ordinary users and administrator users can view it. The command to view the accounts under the current library is as follows:

show users

Introduction to users and permissions in mongoDB

Delete user

Required An administrator account with dropUser rights can delete a user, so you need to log in with an administrator account to perform the operation.

The command to delete ordinary user moddx is as follows:

db.dropUser("moddx", {w: "majority", wtimeout: 5000})
Revoke permissions

To revoke the permissions of a user, the command is as follows:

db.revokeRolesFromUser(
    "moddx",
    [
      { role: "readWrite", db: "photo_app" }
    ]
)
Note: Although the above command revokes moddx The user has read and write permissions in the photo_app database. However, the user has not been deleted and can still log in.

Grant permissions

The following command gives user moddx read and write permissions in photo_app, and at the same time, gives him read permissions in the demodb database

use photo_app
db.grantRolesToUser(
   "moddx",
   [ "readWrite" , { role: "read", db: "demodb" } ],
   { w: "majority" , wtimeout: 4000 }
)
Change password

The following command changes the password of user moddx in photo_app:

use photo_app
db.changeUserPassword("moddx", "newpwd")
Summary

Regarding users and permissions, these are the common shell operation commands. I hope it can help you use mongoDB. Come for convenience.

Related recommendations:

[MongoDB] mongodb and php php mongodb update php connection mongodb php mongodb not authorize

MongoDB Journey (2) Basic Operations (MongoDB Javascript Shell)

The above is the detailed content of Introduction to users and permissions in mongoDB. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.