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:
First, open the mongoDB service in
non-authorized mode.
mongod --port 27017 --dbpath /data/dbThen 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 UserWhen 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/dbOpen the mongo.exe client, enter the admin database, and log in with
db.auth().
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 usersAll global accountsFirst, log in to the admin database with the
admin account, and then execute the following command:
db.system.users.find().pretty()
show users
The command to delete ordinary user moddx is as follows:
db.dropUser("moddx", {w: "majority", wtimeout: 5000})Revoke permissionsTo 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 permissionsThe 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 passwordThe 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!

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.

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

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.

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 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.

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

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
