最近花了一些时间学习了下MongoDB数据库,感受仍是比较全面系统的,涉及了软件安装、客户端操做、安全认证、副本集和分布式集群搭建,以及使用Spring Data链接MongoDB进行数据操做,收获很大。特此记录,以备查看。html
文章目录:mongodb
MongoDB和Java(1):Linux下的MongoDB安装shell
MongoDB和Java(2):普通用户启动mongod进程数据库
MongoDB和Java(3):Java操做MongoB安全
MongoDB和Java(4):Spring Data整合MongoDB(XML配置)分布式
MongoDB和Java(5):Spring Data整合MongoDB(注解配置)学习
MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群ui
MongoDB和Java(7):MongoDB用户管理spa
本文记录如何开启MongoDB认证、添加用户prototype
MongoDB中的每一个数据库有一些用户(得建立),这些用户有的只能操做本身所属库的表,有的能够操做其余库的表,这取决于它拥有的角色。
一个用户能够有多个角色,角色包含若干权限,权限又拥有资源、操做。
简单来讲就是 用户 — 角色 — 权限 的权限管理体系。
关于角色、权限、资源等官网有文档:
https://docs.mongodb.com/manual/reference/built-in-roles/
https://docs.mongodb.com/manual/reference/resource-document/
https://docs.mongodb.com/manual/reference/privilege-actions/
权限资源就不作详细介绍了,由于建立用户使用内置角色就足够了,不太可能去本身去建立角色
看一下内置角色
Database User Roles
read、readWrite
Database Administration Roles
dbAdmin、dbOwner、userAdmin,其中dbOwner权限最高
Cluster Administration Roles
clusterAdmin、clusterManager、clusterMonitor、hostManager
Backup and Restoration Roles
backup、restore
All-Database Roles
readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
Superuser Roles
root
1 [root@xugf-test4 ~]# mongo 2 > use admin 3 switched to db admin 4 > db.createUser({ 5 ... user: "admin", 6 ... pwd: "123456", 7 ... roles: [{role: "root", db: "admin"}], 8 ... mechanisms: ["SCRAM-SHA-1"] 9 ... }) 10 Successfully added user: { 11 "user" : "admin", 12 "roles" : [ 13 { 14 "role" : "root", 15 "db" : "admin" 16 } 17 ], 18 "mechanisms" : [ 19 "SCRAM-SHA-1" 20 ] 21 }
修改mongo.conf配置文件,开启权限认证功能,auth属性设置true
1 [mongo@xugf-test4 ~]$ cat /etc/mongo.conf 2 dbpath=/data/mongo/db/ 3 logpath=/data/mongo/log/mongodb.log 4 bind_ip_all=true 5 fork=true 6 auth=true
重启mongodb
再使用mongo链接,进行操做时会提示未认证
1 [root@xugf-test4 ~]# mongo 2 > show dbs 3 2018-10-12T10:16:00.683+0800 E QUERY [js] Error: listDatabases failed:{ 4 "ok" : 0, 5 "errmsg" : "command listDatabases requires authentication", 6 "code" : 13, 7 "codeName" : "Unauthorized" 8 } : 9 _getErrorWithCode@src/mongo/shell/utils.js:25:13 10 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1 11 shellHelper.show@src/mongo/shell/utils.js:876:19 12 shellHelper@src/mongo/shell/utils.js:766:15 13 @(shellhelp2):1:1
此时,有两种方式进行客户端认证:
1)在链接时使用--authenticationDatabase选项指定认证数据库,使用-u选项指定用户名,使用-p指定密码
1 [root@xugf-test4 ~]# mongo -u admin -p --authenticationDatabase admin 2 MongoDB shell version v4.0.2 3 Enter password: 4 MongoDB server version: 4.0.2 5 > show dbs 6 admin 0.000GB 7 config 0.000GB 8 local 0.000GB
2)在链接后切换到认证数据库后,使用db.auth("username", "password")进行认证
1 [root@xugf-test4 ~]# mongo 2 MongoDB shell version v4.0.2 3 connecting to: mongodb://127.0.0.1:27017 4 MongoDB server version: 4.0.2 5 > use admin 6 switched to db admin 7 > db.auth("admin", "123456") 8 1 9 > show dbs 10 admin 0.000GB 11 config 0.000GB 12 local 0.000GB
给test库添加一个数据库管理员testAdmin
1 > use test 2 switched to db test 3 > db.createUser({ 4 ... user: "testAdmin", 5 ... pwd: "123456", 6 ... roles: [{role: "dbOwner", db: "test"}], 7 ... mechanisms: ["SCRAM-SHA-1"] 8 ... }) 9 Successfully added user: { 10 "user" : "testAdmin", 11 "roles" : [ 12 { 13 "role" : "dbOwner", 14 "db" : "test" 15 } 16 ], 17 "mechanisms" : [ 18 "SCRAM-SHA-1" 19 ] 20 }
使用testAdmin链接
1 [root@xugf-test4 ~]# mongo -u testAdmin -p --authenticationDatabase test 2 > db 3 test 4 > db.getUsers() 5 [ 6 { 7 "_id" : "test.testAdmin", 8 "user" : "testAdmin", 9 "db" : "test", 10 "roles" : [ 11 { 12 "role" : "dbOwner", 13 "db" : "test" 14 } 15 ], 16 "mechanisms" : [ 17 "SCRAM-SHA-1" 18 ] 19 } 20 ]
给test库添加一个xugf用户
1 > use test 2 switched to db test 3 > db.createUser({ 4 ... user: "xugf", 5 ... pwd: "123456", 6 ... roles: [{role: "readWrite", db: "test"}], 7 ... mechanisms: ["SCRAM-SHA-1"] 8 ... }) 9 Successfully added user: { 10 "user" : "xugf", 11 "roles" : [ 12 { 13 "role" : "readWrite", 14 "db" : "test" 15 } 16 ], 17 "mechanisms" : [ 18 "SCRAM-SHA-1" 19 ] 20 }
使用xugf链接
1 [root@xugf-test4 ~]# mongo -u xugf -p 123456 --authenticationDatabase test 2 MongoDB shell version v4.0.2 3 connecting to: mongodb://127.0.0.1:27017 4 MongoDB server version: 4.0.2 5 > 6 > db 7 test