Wednesday, February 28, 2024

.::: Create User MongoDB role admin and readonly and authenticationRestrictions :::.


1. create user admin

use admin
db.createUser({ user: "admin",pwd: "admin",
roles: [{ role: "readAnyDatabase", db: "admin" },{ role: "readWriteAnyDatabase", db: "admin" }]});
show users;


2. create user readonly

use teguhth
db.createUser({user: "readonly",pwd: "readonly",roles: [ "read" ]});
show users;

-- or

use admin
db.createUser({ user: "readall",pwd: "readall",
roles: [{ role: "readAnyDatabase", db: "admin" }]});
show users;


3. drop password

show users;
db.dropUser("admin");
db.dropUser("readonly");
db.dropUser("readall");
show users;


4. change password

use admin
db.changeUserPassword("readall", "readallv2")


5. update user authenticationRestrictions

use teguhth
db.updateUser("readonly", {
    authenticationRestrictions: [ {
        clientSource: ["127.0.0.1", "::1"],
        serverAddress: ["127.0.0.1", "::1"]
    } ]
})

6. get user dalam bentuk column
db.getUsers().forEach(function(user) { print("User: " + user.user + ", Roles: " + JSON.stringify(user.roles));});
 


7. get user dalam bentuk kyk sql

function showUsers() {
    var users = db.getUsers();

    print("| Username | Database | Roles |");
    print("|----------|----------|-------|");

    users.forEach(function(user) {
        user.roles.forEach(function(role) {
            print("| " + user.user + " | " + role.db + " | " + role.role + " |");
        });
    });
}

showUsers(); 



https://www.prisma.io/dataguide/mongodb/configuring-mongodb-user-accounts-and-authentication

No comments:

Post a Comment

Popular Posts