The mongodb configuration file is as follows:
[root@localhost ~]# cat /usr/local/mongodb/mongod.cnf bind_ip = 0.0.0.0 logpath=/data/mongodb/logs/mongodb.log logappend = true fork = true #auth = true port = 6068 dbpath=/data/mongodb/data pidfilepath=/data/mongodb/mongod.pid
root super administrator privileges:
Close mongod.cnf profile parameter: auth
Log in to mongo directly and set the login password:
mongo --host 127.0.0.1 --port 6068 use admin db.createUser({user: 'root', pwd:'TdLLQSdHgyn#6689', 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 6068 use admin db.auth("root","TdLLQSdH689")
Create test library dbtest001, and test user testuser has read and write permission to dbtest001
use dbtest001 db.createUser({user:"testuser",pwd:"testuser123",roles:[{role:"readWrite",db:"dbtest001"}]}) //Create test user testuser with read permission to dbtest001 Library: db.createUser({user:"testuser01",pwd:"testuser123",roles:[{role:"read",db:"dbtest001"}]})
Insert a collection (so-called table) into the test library:
db.chenji.insert({"name":"Floret","grade":"second grade","Gender":"male","hobby":"Study"}) WriteResult({ "nInserted" : 1 }) > db.chenji.insert({"name":"tearful","grade":"first grade","Gender":"female","hobby":"Sing"}) WriteResult({ "nInserted" : 1 }) > db.chenji.insert({"name":"Floret","grade":"Grade three","Gender":"female","hobby":"Play a ball"}) WriteResult({ "nInserted" : 1 }) > db.chenji.insert({"name":"Floret","grade":"fourth grade","Gender":"female","hobby":"draw"}) WriteResult({ "nInserted" : 1 })
To view users under the current library:
db.getUsers() perhaps show users > show users { "_id" : "dbtest001.testuser", "userId" : UUID("f275fad5-ac4f-4dfa-aea6-af34ed93c3e1"), "user" : "testuser", "db" : "dbtest001", "roles" : [ { "role" : "readWrite", "db" : "dbtest001" } ] }
To view the permissions of a user under the current library:
db.getUser("testuser") db.getUser("testuser01")
Create 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 users: db.dropusers ("xxxxx")
db.dropUser("testuser01")
true
Assign a role to read and write to the user: db.grantRolesToUser()
use dbtest001 db.grantRolesToUser('testuser01',[ { role : 'readWrite', db : 'dbtest001' } ] )
Revoke a role permission of 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 and write access to read role:
> 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 Password:
> db.changeUserPassword("testuser01","123456")
Test: use the original password to log in and report an error as follows:
[root@localhost ~]# mongo 127.0.0.1:6068/dbtest001 -u testuser01 -p 'testuser123' MongoDB shell version v3.6.16 connecting to: mongodb://127.0.0.1:6068/dbtest001?gssapiServiceName=mongodb 2020-01-01T21:51:35.637+0800 E QUERY [thread1] Error: Authentication failed. : connect@src/mongo/shell/mongo.js:263:13 @(connect):1:6 exception: connect failed
Login succeeded with new password
[root@localhost ~]# mongo 127.0.0.1:6068/dbtest001 -u testuser01 -p '123456' MongoDB shell version v3.6.16 connecting to: mongodb://127.0.0.1:6068/dbtest001?gssapiServiceName=mongodb Implicit 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" : "Study" } { "_id" : ObjectId("5e0c80c3efc383349153386b"), "name" : "tearful", "grade" : "first grade", "Gender" : "female", "hobby" : "Sing" } { "_id" : ObjectId("5e0c83c8efc383349153386c"), "name" : "Floret", "grade" : "Grade three", "Gender" : "female", "hobby" : "Play a ball" } { "_id" : ObjectId("5e0c83e9efc383349153386d"), "name" : "Floret", "grade" : "fourth grade", "Gender" : "female", "hobby" : "draw" }
View the table under the current library:
show collections
chenji
To view everything in the collection:
> db.chenji.find(); { "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "Floret", "grade" : "second grade", "Gender" : "male", "hobby" : "Study" } { "_id" : ObjectId("5e0c80c3efc383349153386b"), "name" : "tearful", "grade" : "first grade", "Gender" : "female", "hobby" : "Sing" }
To view the record named Xiaohua:
> db.chenji.find({"name":"Floret"}) { "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "Floret", "grade" : "second grade", "Gender" : "male", "hobby" : "Study" } { "_id" : ObjectId("5e0c83c8efc383349153386c"), "name" : "Floret", "grade" : "Grade three", "Gender" : "female", "hobby" : "Play a ball" } { "_id" : ObjectId("5e0c83e9efc383349153386d"), "name" : "Floret", "grade" : "fourth grade", "Gender" : "female", "hobby" : "draw" }
reference material:
MongoDB user and role Interpretation Series (I)
http://www.mongoing.com/archives/26710
Summarize here first, and continue to learn and supplement later