Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB Rights Management II

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The mongodb configuration file is as follows:

[root@localhost ~] # cat / usr/local/mongodb/mongod.cnf bind_ip = 0.0.0.0logpath=/data/mongodb/logs/mongodb.loglogappend = truefork = true#auth = trueport = 6068dbpath=/data/mongodb/datapidfilepath=/data/mongodb/mongod.pid

Root Super Admin privileges:

Close mongod.cnf profile parameter: auth

Log in to mongo directly and set the login password:

Mongo-- host 127.0.0.1-- port 6068use admindb.createUser ({user: 'root', pwd:'TdLLQd689', roles: [{role:' root', db: 'admin'}]}); db.auth ("root", "TdLLQSdH689")

Close mongod

Open mongod.cnf profile parameter: auth = true

Mongo-host 127.0.0.1-port 6068use admindb.auth ("root", "TdLLQH689")

Create a test library dbtest001, and the test user testuser has read and write access to the dbtest001 library

Use dbtest001db.createUser ({user: "testuser", pwd: "testuser123", roles: [{role: "readWrite", db: "dbtest001"}]}) create test user testuser to read the dbtest001 library: db.createUser ({user: "testuser01", pwd: "testuser123", roles: [{role: "read", db: "dbtest001"}]})

Insert a collection chenji (so-called table chenji) into the test library:

Db.chenji.insert ({"name": "Xiaohua", "Grade": "sophomore", "gender": "male", "hobby": "Learning"}) WriteResult ({"nInserted": 1}) > db.chenji.insert ({"name": "Huahua", "Grade": "Grade one", "gender": "female" "hobby": "singing"}) WriteResult ({"nInserted": 1}) > db.chenji.insert ({"name": "floret", "grade": "third grade", "gender": "female", "hobby": "playing ball"}) WriteResult ({"nInserted": 1}) > db.chenji.insert ({"name": "floret", "grade": "fourth grade", "gender": "female" "hobby": "drawing"}) WriteResult ({"nInserted": 1})

View the users under the current library:

Db.getUsers () or show users > show users {"_ id": "dbtest001.testuser", "userId": UUID ("f275fad5-ac4f-4dfa-aea6-af34ed93c3e1"), "user": "testuser", "db": "dbtest001", "roles": [{"role": "readWrite", "db": "dbtest001"}]}

View the permissions of a user under the current library:

Db.getUser ("testuser") db.getUser ("testuser01")

Create a role: db.createRole ()

Update role: db.updateRole ()

Delete role: db.dropRole ()

Get a role information: db.getRole ()

> db.getRole ("read") {"role": "read", "db": "dbtest001", "isBuiltin": true, "roles": [], "inheritedRoles": [] > db.getRole ("readWrite") {"role": "readWrite", "db": "dbtest001", "isBuiltin": true, "roles": [] "inheritedRoles": []}

Delete all users: db.dropAllUsers ()

Delete user: db.dropUsers ("xxxxxx")

Db.dropUser ("testuser01")

True

Assign a role to read and write to the user: db.grantRolesToUser ()

Use dbtest001db.grantRolesToUser ('testuser01', [{role:' readWrite', db: 'dbtest001'}])

Revoke a role permission for a user: db.revokeRolesFromUser ()

> db.getUser ("testuser01") {"_ id": "dbtest001.testuser01", "userId": UUID ("d1fefe19-a94f-4300-8855-fd722f14e13f"), "user": "testuser01", "db": "dbtest001", "roles": [{"role": "readWrite", "db": "dbtest001"}]}

Revoke read role read and write permissions:

> db.revokeRolesFromUser ('testuser01', [{role:' readWrite', db: 'dbtest001'}]) > db.getUser ("testuser01") {"_ id": "dbtest001.testuser01", "userId": UUID ("d1fefe19-a94f-4300-8855-fd722f14e13f"), "user": "testuser01", "db": "dbtest001", "roles": []}

Change password: db.changeUserPassword ()

Db.createUser ({user: "testuser01", pwd: "testuser123", roles: [{role: "read", db: "dbtest001"}]})

Change the password:

> db.changeUserPassword ("testuser01", "123456")

Test: login with the original password reported an error as follows:

[root@localhost] # mongo 127.0.0.1:6068/dbtest001-u testuser01-p 'testuser123'MongoDB shell version v3.6.16connecting to: mongodb://127.0.0.1:6068/dbtest001?gssapiServiceName=mongodb2020-01-01T21:51:35.637+0800 E QUERY [thread1] Error: Authentication failed.: connect@src/mongo/shell/mongo.js:263:13@ (connect): 1:6exception: connect failed

Log in successfully with the new password

[root@localhost ~] # mongo 127.0.0.1:6068/dbtest001-u testuser01-p '123456'MongoDB shell version v3.6.16connecting to: mongodb://127.0.0.1:6068/dbtest001?gssapiServiceName=mongodbImplicit session: session {"id": UUID ("966023f0-ff7b-4726-a216-8475bc729971")} MongoDB server version: 3.6.16 > show collections;chenji > db.chenji.find () {"_ id": ObjectId ("5e0c80a8efc383349153386a"), "name": "floret", "grade": "second grade", "gender": "male", "hobby": "learning" {"_ id": ObjectId ("5e0c80c3efc383349153386b"), "name": "Huahua", "grade": "first grade", "gender": "female" "hobby": "singing"} {"_ id": ObjectId ("5e0c83c8efc383349153386c"), "name": "floret", "grade": "third grade", "gender": "female", "hobby": "playing ball" {"_ id": ObjectId ("5e0c83e9efc383349153386d"), "name": "floret", "grade": "fourth grade" "gender": "female", "hobby": "painting"}

View the table under the current library:

Show collections

Chenji

View all the contents of the collection:

> db.chenji.find () {"_ id": ObjectId ("5e0c80a8efc383349153386a"), "name": "floret", "grade": "second grade", "gender": "male", "hobby": "learning" {"_ id": ObjectId ("5e0c80c3efc383349153386b"), "name": "Huahua", "grade": "first grade", "gender": "female" Hobby: singing}

Check the record named Xiaohua:

> db.chenji.find ({"name": "id"}) {"_ id": ObjectId ("5e0c80a8efc383349153386a"), "name": "floret", "grade": "second grade", "gender": "male", "hobby": "learning" {"_ id": ObjectId ("5e0c83c8efc383349153386c"), "name": "floret", "grade": "third grade" "gender": "female", "hobby": "playing ball" {"_ id": ObjectId ("5e0c83e9efc383349153386d"), "name": "floret", "grade": "fourth grade", "gender": "female", "hobby": "drawing"}

Reference:

MongoDB user and role interpretation Series (part I)

Http://www.mongoing.com/archives/26710

This is the end of the summary, and we will continue to learn and supplement later.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report