Atikh's DBA blog
  • Home
  • Oracle
  • MySQL
  • MongoDB
  • PostgreSQL
  • Snowflake
  • About Me
  • Contact Us

Creating User Account in MongoDB

 Atikh Shaikh     MongoDB     No comments   

For any database whether it is RDBMS or NoSQL, the user account is a very important aspect and is a must to connect to a database
There are different types of users depending on usage like, admin user account, read-only account, read write only.
Users can be created or added to the database using db.createUser() method available with MongoDB
The db.createUser() method accepts a document object that enables you to specify the username, roles and password for user to be created

Definition of db.createUser() method
db.createUser(user, writeConcern)

Field
Type
Description
User
Document
The document with authentication and access information about user
writeConcern
Document
Optional, the level of writeConcern for creation operation

The user document has the following fields

Field
Type
Description
user
String
The name of the new user
pwd
String
The user password
customData
Document
Optional, any information about user
roles
Array
This defines roles, The roles granted to a user can be an empty array[] to create a user without any roles
AuthenticationRestrictions
Array
Optional, the authentication restrictions the server enforces on the user. Specifies a list of IP addresses and CIDR ranges from which user will be able to connect
Mechanisms
Array
Optional, specify the specific SCRAM mechanism for creating SCRAM user creation
passwordDigester
string
Optional, Indicates whether the server or the client digest the password

Roles
In the role field, you can specify the build-in roles and user-defined roles. You can specify directly role name or in the form of a document as well
i.e.
"readWrite" or {role : "" : db ""}

External Credentials
Users created on $external database should have credentials stored externally to MongoDB
Local database
user can not be created on local database
Examples
Create account with roles assigned
> db.createUser({ user : "tech_owner",
... pwd : "techo!23",
... customData : {user : "blogUser"},
... roles :[{role: "clusterAdmin",
... db : "admin" },
... "readWrite"]}
... );
Successfully added user: {
        "user" : "tech_owner",
        "customData" : {
                "user" : "blogUser"
        },
        "roles" : [
                {
                        "role" : "clusterAdmin",
                        "db" : "admin"
                },
                "readWrite"
        ]
}
> 

Create an account with roles

> db.createUser({ user : "tech_user",
... pwd : "techo!23",
... roles :["readWrite", "dbAdmin"]}
... );
Successfully added user: { "user" : "tech_user", "roles" : [ "readWrite", "dbAdmin" ] }
> 
> 

Create user without any roles

> db.createUser({ user : "tech_wo_roles",
... pwd : "techo!23",
... roles :[]
... }
... );
Successfully added user: { "user" : "tech_wo_roles", "roles" : [ ] }
> 
> 

Listing users created

> db.getUsers();
[
        {
                "_id" : "techno_db.tech_owner",
                "user" : "tech_owner",
                "db" : "techno_db",
                "customData" : {
                        "user" : "blogUser"
                },
                "roles" : [
                        {
                                "role" : "clusterAdmin",
                                "db" : "admin"
                        },
                        {
                                "role" : "readWrite",
                                "db" : "techno_db"
                        }
                ],
                "mechanisms" : [
                        "SCRAM-SHA-1"
                ]
        },
        {
                "_id" : "techno_db.tech_user",
                "user" : "tech_user",
                "db" : "techno_db",
                "roles" : [
                        {
                                "role" : "readWrite",
                                "db" : "techno_db"
                        },
                        {
                                "role" : "dbAdmin",
                                "db" : "techno_db"
                        }
                ],
                "mechanisms" : [
                        "SCRAM-SHA-1"
                ]
        },
        {
                "_id" : "techno_db.tech_wo_roles",
                "user" : "tech_wo_roles",
                "db" : "techno_db",
                "roles" : [ ],
                "mechanisms" : [
                        "SCRAM-SHA-1"
                ]
        }
]
> 
> 

Removing/Dropping User in MongoDB
First list the users using db.getUsers() method and decide which one to drop
then use db.dropUser() method to drop user, this dropUser() method takes username as argument
We will drop tech_wo_roles user 

> db.dropUser("tech_wo_roles");
true
> 

If you want to drop all the users if you can user db.dropAllUsers() method.

More will come on MongoDB. follow this space for more and explore more.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Instagram
  •  Pin
  •  linkedin
  •  reddit
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Author

Atikh Shaikh
View my complete profile

Categories

  • MongoDB (18)
  • Oracle 12c (30)
  • Oracle12cR2 New Feature (3)
  • PostgreSQL (20)
  • RMAN (10)
  • Snowflake (8)
  • mysql (23)
  • oracle (74)

Blog Archive

  • ►  2018 (38)
    • ►  November (25)
    • ►  December (13)
  • ▼  2019 (33)
    • ►  January (15)
    • ►  February (6)
    • ►  March (2)
    • ▼  April (5)
      • Data Types in PostgreSQL
      • Database Creation in PostgreSQL
      • Different Server Logs in MySQL
      • Table Creation in PostgreSQL
      • Creating User Account in MongoDB
    • ►  May (5)
  • ►  2020 (5)
    • ►  April (1)
    • ►  May (2)
    • ►  July (2)
  • ►  2021 (8)
    • ►  June (3)
    • ►  July (3)
    • ►  August (1)
    • ►  December (1)
  • ►  2022 (33)
    • ►  May (3)
    • ►  June (10)
    • ►  July (3)
    • ►  August (4)
    • ►  September (8)
    • ►  October (3)
    • ►  November (2)
  • ►  2023 (14)
    • ►  February (1)
    • ►  April (5)
    • ►  May (2)
    • ►  June (1)
    • ►  September (1)
    • ►  October (1)
    • ►  December (3)
  • ►  2024 (5)
    • ►  January (2)
    • ►  March (3)
  • ►  2025 (6)
    • ►  March (1)
    • ►  April (3)
    • ►  May (2)

Popular Posts

  • ORA-29283: invalid file operation: unexpected "LFI" error (1509)[29437]
    I was trying to export the schema in my windows PC, it got stuck with below error    C:\Users\shaik\Videos\technodba exp>expdp userid...
  • Oracle 23ai : Use of NOVALIDATE Constraints in IMPDP
    While performing impdp operations in the Oracle database, Oracle performs validation checks for every constraint on the imported table, that...
  • PostgreSQL : How to get data directory location for PostgreSQL instance
    Sometimes, you start working on a PostgreSQL instance but forget about the data directory, here we will discuss different methods to know th...
  • ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
    In previous articles, we have learned about user creation and grants  in MySQL in detail, but there are a few privileges called global priv...
  • Oracle Dataguard Broker Configuration (DGMGRL)
    Data Guard Broker is a command-line interface that makes managing primary and standby databases easy. DBA can use a single command to switch...

Labels

oracle Oracle 12c mysql PostgreSQL MongoDB oracle 19c Oracle23c oracle19c Orale PDB-CDB oracle12c python AWS Oracle ASM Virtualbox pluggable database storage engine

Pages

  • Disclaimer
  • Privacy Policy

Follow TechnoDBA

Copyright © Atikh's DBA blog | Powered by Blogger