MongoDB User Management | Rookie Tutorial
Rookie Tutorial --
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
MongoDB Tutorial
MongoDB TutorialNoSQL IntroductionMongoDB IntroductionWindows MongoDBLinux MongoDBOSX MongoDBMongoDB ShellMongoDB Concept AnalysisMongoDB User ManagementMongoDB ConnectionMongoDB Create DatabaseMongoDB Delete DatabaseMongoDB Create CollectionMongoDB Rename CollectionMongoDB Delete CollectionMongoDB Insert DocumentMongoDB Update DocumentMongoDB Delete DocumentMongoDB Query DocumentMongoDB Conditional OperatorsMongoDB $type OperatorMongoDB Limit and Skip MethodsMongoDB SortingMongoDB IndexMongoDB AggregationMongoDB Replication (Replica Set)MongoDB ShardingMongoDB Backup and RestoreMongoDB MonitoringMongoDB JavaMongoDB PHP ExtensionMongoDB PHPMongoDB PHP7Node.js MongoDB
MongoDB Advanced Tutorial
MongoDB RelationshipsMongoDB Database ReferencesMongoDB Covered Index QueryMongoDB Query AnalysisMongoDB Atomic OperationsMongoDB Advanced IndexingMongoDB Index LimitationsMongoDB ObjectIdMongoDB Map ReduceMongoDB Full Text SearchMongoDB Regular ExpressionsMongoDB Management ToolMongoDB GridFSMongoDB Capped CollectionsMongoDB Auto Increment
In-depth Exploration
Web Design and Development
Web Services
Programming
Scripting Languages
Software
Computer Science
Programming Languages
Development Tools
Web Service
Scripts
MongoDB User Management
In MongoDB, user management involves creating users, assigning roles, authentication, and login operations.
Below is a detailed explanation, including how to use MongoDB Shell (mongo) or MongoDB Compass to manage users.
Using MongoDB Shell (mongo) to Manage Users
Here is a detailed explanation of using MongoDB Shell (mongosh) for user management, including specific steps for creating users, assigning roles, authentication, and login.
1. Connect to MongoDB
First, open your terminal and use the mongosh command to connect to the MongoDB server:
mongosh --host <hostname> --port <port>
Explanation:
mongosh: Launch the MongoDB Shell command line tool.
--host <hostname>: Specify the hostname or IP address of the MongoDB server.
<hostname>: The hostname (e.g., localhost) or IP address (e.g., 127.0.0.1) of the MongoDB server.
--port <port>: Specify the port number of the MongoDB server.
<port>: The port number that the MongoDB server listens on, the default port is 27017.
2. Switch to Target Database
In MongoDB, users are created for specific databases. Use the use command to switch to the database where you want to create the user:
use <database_name>
- database_name - The database to switch to.
3. Create User
Use the db.createUser command to create a user and assign roles.
For example, create a user named testuser with password password123, and grant readWrite and dbAdmin roles:
db.createUser({ user: "testuser", pwd: "password123", roles: [ { role:
mongosh --host <hostname> --port <port>mongosh: Launch the MongoDB Shell command line tool.--host <hostname>: Specify the hostname or IP address of the MongoDB server.
<hostname>: The hostname (e.g.,localhost) or IP address (e.g.,127.0.0.1) of the MongoDB server.
--port <port>: Specify the port number of the MongoDB server.
<port>: The port number that the MongoDB server listens on, the default port is27017.
use <database_name>db.createUser({ user: "testuser", pwd: "password123", roles: [ { role:
YouTip