Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB user Management

Shulou Source: shulou.com Published: 2022-06-01 09:56:12 09月27日 Update

Create a user

Syntax:

Db.createUser (

{

User:, # string

Pwd:, # string

Roles: [{role:,db:}] # Array

}

)

Create a user:

> db.createUser (. {. User: "root", Pwd: "admin", Roles: [{role: "root", db: "admin"}]. }.)

User authentication:

> db.auth ("root", "admin") 1

View users:

Delete user

Db.dropUser () # Delete a user and grant a string parameter

Db.dropAllUsers () # Delete all users of the current library

Query all users > db.getUsers () [{"_ id": "admin.root", "user": "root", "db": "admin", "roles": [{"role": "root", "db": "admin"}]} {"_ id": "admin.sysadmin", "user": "sysadmin", "db": "admin", "roles": [{"role": "root", "db": "admin"}]} {"_ id": "admin.test", "user": "test", "db": "admin", "roles": [{"role": "root" "db": "admin"}]] Delete test user > db.dropUser ("test") true confirm whether test user exists > db.getUser ("test") null Delete all users > db.dropAllUsers () 2 modify user password

To change a user's password, a user is required to have changePassword or changeOwnPassword privileges. There are two ways to change a user's password:

Db.changeUserPassword (,)

Db.updateUser (, {update_object})

Db.changeUserPassword () example:

Admin@undefined$ db.auth ("root", "admin") 1

Admin@undefined$ db.changeUserPassword ("root", "123456")

Admin@undefined$ db.auth ("root", "admin") Error: Authentication failed.0admin@undefined$ db.auth ("root", "123456") 1admings undefined $show dbsadmin 0.000GBlocal 0.000GBtest 0.000GBtest1 0.000GB

You can see that after changing the root user's password, the original password authentication failed, but the current session can still operate normally, and the new session needs to be verified with the modified password.

Db.updateUser () example:

Admin@undefined$ db.auth ("root", "123456") 1

Admin@undefined$ db.updateUser ("root", {pwd: "admin123"})

Admin@undefined$ db.auth ("root", "admin123") 1admingly undefined $db.auth ("root", "123456") Error: Authentication failed.0

Modify user permissions (roles):

The db.updateUser () function is also used to modify the user role.

Let's first create a test user, readtest, which only has read access to the test library:

Db.createUser (

{

User: "readtest"

Pwd: "123456"

Roles: [{role: "read", db: "test"}]

}

)

Admin@undefined$ db.auth ("readtest", "123456") 1admings undefined $use testswitched to db testtest@undefined$ show tablesgoodsuserstest@undefined$ db.goods.find () {"_ id": ObjectId ("5a7c5b7e83dba596ccad3ac0"), "sn": "fhbowhnlerio12o47", "category": "food"} test@undefined$ db.goods.insert ({"sn": "04t68gjsoe076", "category": "beauty"}) WriteResult ({"writeError": {"code": 13) "errmsg": "not authorized on test to execute command {insert:\" goods\ ", documents: [{_ id: ObjectId ('5a8ef5aa3cdd503ad3903fcc'), sn:\" 04t68gjsoe076\ ", category:\" beauty\ "}], ordered: true}"}})

You can see that this user can perform read operations, write operations do not have permission, now we use db.updateUser () to expand its permissions, remember that it has read and write permissions.

Admin@undefined$ db.updateUser ("readtest", {"roles": [{role: "readWrite", db: "test"}]})

Admin@undefined$ db.auth ("readtest", "123456") 1admings undefined $use testswitched to db testtest@undefined$ db.goods.insert ({"sn": "04t68gjsoe076", "category": "beauty"}) WriteResult ({"nInserted": 1})

As you can see, when we change the role of the readtest user from read to readWrite, it has write access to the test library. Through db.updateUser (), we can realize the enlargement and reduction of user rights.

Tags: Users permissions passwords characters strings roles authentication examples functions parameters arrays methods syntax or Xinhui queries tests management Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS NVidia MySQL Shulou Technology