博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb的用户管理及安全认证
阅读量:7170 次
发布时间:2019-06-29

本文共 8666 字,大约阅读时间需要 28 分钟。

 

1.确认mongodb的版本

> use adminswitched to db admin> db.runCommand({
"buildInfo":1}){ "version" : "2.6.6", "gitVersion" : "608e8bc319627693b04cc7da29ecc300a5f45a1f", "targetMinOS" : "Windows 7/Windows Server 2008 R2", "OpenSSLVersion" : "", "sysInfo" : "windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49", "loaderFlags" : "/nologo /DEBUG /INCREMENTAL:NO /LARGEADDRESSAWARE", "compilerFlags" : "/TP /nologo /EHsc /W3 /wd4355 /wd4800 /wd4267 /wd4244 /wd4290 /we4099 /Z7 /errorReport:none /MT /O2 /Oy-", "allocator" : "system", "versionArray" : [ 2, 6, 6, 0 ], "javascriptEngine" : "V8", "bits" : 64, "debug" : false, "maxBsonObjectSize" : 16777216, "ok" : 1}>

 

2.启用安全认证

默认启动的mongodb是没有启用安全认证的,在安装完成mongodb后想要启动mongodb的安全认证只需要在启动mongod的时候增加--auth参数或在配置文件中增加auth=true,例如:

mongod.exe --dbpath=D:\MongoDB2.6\data --auth

mongod.exe --config D:\MongoDB2.6\up\mongodb.conf

 

3.查看图形界面

mongodb的图形界面端口号为其监听端口号加上1000,如我们默认的启动端口号为27017,那么图形界面的端口就为28017,当默认启动mongodb时,访问28017端口发现会无法访问,这时只需要在启动mongod的时候增加--auth参数或在配置文件中增加auth=true就可以正常访问。

mongod.exe --dbpath=D:\MongoDB2.6\data --rest

mongod.exe --config D:\MongoDB2.6\up\mongodb.conf

 如图:

 

 

4.创建用户并验证认证

> use adminswitched to db admin> show collectionssystem.indexessystem.userssystem.version> db.system.users.find()>

 

经过查询发现目前库中没有任何用户,现在创建用户:

> use adminswitched to db admin> show dbsadmin  0.078GBlocal  0.078GBtest   0.078GB> use mydbswitched to db mydb> show dbsadmin  0.078GBlocal  0.078GBtest   0.078GB> use mydbswitched to db mydb> db.book.insert({
"name":"english"})WriteResult({ "nInserted" : 1 })> show dbsadmin 0.078GBlocal 0.078GBmydb 0.078GBtest 0.078GB

上面的语句我们创建一个数据库mydb

 

> use adminswitched to db admin> db.addUser("root","123456")WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "root", "roles" : [ "root" ] }> show collections      --当我们查看集合的时候提示没有权限2016-04-28T16:24:50.808+0800 error: {        "$err" : "not authorized for query on admin.system.namespaces",        "code" : 13} at src/mongo/shell/query.js:131> db.auth("root","123456")  --进行用户验证后,发现能够使用后面的命令了1> show dbsadmin  0.078GBlocal  0.078GBmydb   0.078GBtest   0.078GB> show collectionssystem.indexessystem.userssystem.version> db.system.users.find()   --查询users集合发现已经有了一个root用户,且角色为root{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "34e5772aa66b703a319641d42a47d696" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }>

以上语句我们在admin库下创建了一个root用户。

 

> use mydb     --切换到mydb库下switched to db mydb> db.addUser("livan","livan123")   WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "livan", "roles" : [ "dbOwner" ] }> show collectionsbooksystem.indexes> use adminswitched to db admin> db.system.users.find(){ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "34e5772aa66b703a319641d42a47d696" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }{ "_id" : "mydb.livan", "user" : "livan", "db" : "mydb", "credentials" : { "MONGODB-CR" : "231eafd8ebf83e43a61d31fc3b9be671" }, "roles" : [ { "role" : "dbOwner", "db" : "mydb" } ] }>

以上语句我们切换到mydb下创建了livan用户,此用户对用的数据库为mydb,在admin库下查询system.users集合能够看到livan用户信息,且对应的角色为dbOwner。

 

 

D:\MongoDB2.6\up>D:\MongoDB2.6\bin\mongo.exe 127.0.0.1:27017MongoDB shell version: 2.6.6connecting to: 127.0.0.1:27017/test> use mydbswitched to db mydb> db.auth("livan","livan123")1> show dbs2016-04-28T16:41:04.434+0800 listDatabases failed:{        "ok" : 0,        "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",        "code" : 13} at src/mongo/shell/mongo.js:47> use adminswitched to db admin> show dbs2016-04-28T16:41:11.915+0800 listDatabases failed:{        "ok" : 0,        "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",        "code" : 13} at src/mongo/shell/mongo.js:47> show collections2016-04-28T16:41:34.197+0800 error: {        "$err" : "not authorized for query on admin.system.namespaces",        "code" : 13} at src/mongo/shell/query.js:131> db.system.users.find()error: { "$err" : "not authorized for query on admin.system.users", "code" : 13 }> use mydbswitched to db mydb> show dbs2016-04-28T16:42:03.305+0800 listDatabases failed:{        "ok" : 0,        "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",        "code" : 13} at src/mongo/shell/mongo.js:47> show collectionsbooksystem.indexes>> db.system.indexes.find(){ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.book" }> db.book.find(){ "_id" : ObjectId("5721c7e579d5865b3c50facd"), "name" : "english" }>

以上语句可以看到当使用livan用户进行验证时,是能够切换到admin库,但用不了show dbs,show collections等命令,更不能到查询system.users集合,当切换回mydb库时,

使用不了show dbs命令,但show collections命令能够使用,且能够正常访问mydb下的集合。

 

D:\MongoDB2.6\up>D:\MongoDB2.6\bin\mongo.exe 127.0.0.1:27017MongoDB shell version: 2.6.6connecting to: 127.0.0.1:27017/test> use adminswitched to db admin> db.auth("root","123456")1> show dbsadmin  0.078GBlocal  0.078GBmydb   0.078GBtest   0.078GB> show collectionssystem.indexessystem.userssystem.version> db.system.users.find(){ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "34e5772aa66b703a319641d42a47d696" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }{ "_id" : "mydb.livan", "user" : "livan", "db" : "mydb", "credentials" : { "MONGODB-CR" : "231eafd8ebf83e43a61d31fc3b9be671" }, "roles" : [ { "role" : "dbOwner", "db" : "mydb" } ] }> use mydbswitched to db mydb> show dbsadmin  0.078GBlocal  0.078GBmydb   0.078GBtest   0.078GB> show collectionsbooksystem.indexes> db.book.find(){ "_id" : ObjectId("5721c7e579d5865b3c50facd"), "name" : "english" }> db.system.indexes()2016-04-28T16:48:15.946+0800 TypeError: Property 'indexes' of object mydb.system is not a function>

以上语句是使用root用户进行登录测试,root用户可以执行所有操作命令,且能够正常访问其他数据库集合,这是因为mongodb在admin库中认证的用户都为管理员用户,

非admin数据库的用户不能使用数据库命令,只能访问本数据库的集合。

 

5.其他创建用户命令

在上面的测试中发现使用db.addUser()创建用户是系统会给出一个警告,提示我们用createUser 创建用户,如下:

 

> db.addUser("livan","livan123")   WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "livan", "roles" : [ "dbOwner" ] }

 

对于db.createUser()的用法可参考

db.createUser()用法:

db.createUser(user, writeConcern)

user 关于用户的身份认证和访问信息(JSON);
writeConcern 这个文档描述保证MongoDB提供写操作的成功报告。

 

user文档的形式:

{ user: "
", pwd: "
", customData: {
}, roles: [ { role: "
", db: "
" } | "
", ... ]}

user字段,用户的名字;

pwd字段,用户的密码;
cusomData字段,为任意内容,例如可以为用户全名介绍;
roles字段,指定用户的角色,可以用一个空数组给新用户设定空角色; roles 字段,可以指定内置角色和用户定义的角色。

 

示例创建一个管理员,直接给几个所有数据库权限即可:

db.createUser({user:"test",pwd:"test.com",roles:[   {    role:"userAdminAnyDatabase",    db:"admin"  },  {    role:"readWriteAnyDatabase",    db:"admin"  },  {    role:"dbAdminAnyDatabase",    db:"admin"  }]})

 

示例创建某个用户对数据库只赋予只读权限:

db.createUser({user:"test",pwd:"test.com",roles:[   {    role:"read",    db:"test"  }]

 

以上创建的用户,我们可以通过 db.auth("username","password") 或者mongo -u test -p test.com --authenticationDatabase test 来验证。

 

 

6.mongodb的常用角色

MongoDB提供了很多内建角色,用户通用的数据库管理。内建角色的文档在这里。

MongoDB提供了数据库管理权限和数据库用户权限两种类型,其他的权限只能作用于admin数据库上。具体如下:

 

(1).数据库用户角色

针对每一个数据库进行控制。

read :提供了读取所有非系统集合,以及系统集合中的system.indexes, system.js, system.namespaces

readWrite: 包含了所有read权限,以及修改所有非系统集合的和系统集合中的system.js的权.

 

(2).数据库管理角色

每一个数据库包含了下面的数据库管理角色。

dbOwner:该数据库的所有者,具有该数据库的全部权限。
dbAdmin:一些数据库对象的管理操作,但是没有数据库的读写权限。(参考:)
userAdmin:为当前用户创建、修改用户和角色。拥有userAdmin权限的用户可以将该数据库的任意权限赋予任意的用户。

 

(3).集群管理权限

admin数据库包含了下面的角色,用户管理整个系统,而非单个数据库。这些权限包含了复制集和共享集群的管理函数。

clusterAdmin:提供了最大的集群管理功能。相当于clusterManager, clusterMonitor, and hostManagerdropDatabase的权限组合。
clusterManager:提供了集群和复制集管理和监控操作。拥有该权限的用户可以操作configlocal数据库(即分片和复制功能)
clusterMonitor:仅仅监控集群和复制集。
hostManager:提供了监控和管理服务器的权限,包括shutdown节点,logrotate, repairDatabase等。
备份恢复权限:admin数据库中包含了备份恢复数据的角色。包括backup、restore等等。

 

(4).所有数据库角色

admin数据库提供了一个mongod实例中所有数据库的权限角色:

readAnyDatabase:具有read每一个数据库权限。但是不包括应用到集群中的数据库。
readWriteAnyDatabase:具有readWrite每一个数据库权限。但是不包括应用到集群中的数据库。
userAdminAnyDatabase:具有userAdmin每一个数据库权限,但是不包括应用到集群中的数据库。
dbAdminAnyDatabase:提供了dbAdmin每一个数据库权限,但是不包括应用到集群中的数据库。

 

 

(5). 超级管理员权限

root: dbadminadmin数据库、useradminadmin数据库以及UserAdminAnyDatabase。但它不具有备份恢复、直接操作system.*集合的权限,但是拥有root权限的超级用户可以自己给自己赋予这些权限。

 

 

本文最后部分来源于

 

转载于:https://www.cnblogs.com/myrunning/p/5444170.html

你可能感兴趣的文章
JavaWeb-11 (JSP&EL表达)
查看>>
WebAPI用法
查看>>
【读书】领导力的5个层次-概述
查看>>
windows 80端口被占用的解决方法
查看>>
C# WinForm开发系列 - Regular Expression
查看>>
windows7下硬盘安装ubuntu14.04
查看>>
CRM不止是一套软件 还需要运营
查看>>
中兴通讯与德国三城市共同发布智慧城市合作愿景
查看>>
ERP平台 如何在企业管理界大放异彩?
查看>>
光伏逆变器主电路及电力电子器件
查看>>
补天白帽大会:安全不只是防御 是持续运营的过程
查看>>
特朗普遭抗议:美国投资人仍看好可再生能源
查看>>
[译] 通过后台数据预获取技术实现性能提升
查看>>
iOS实现多继承的几种方式
查看>>
我要做 Android 之要点总结
查看>>
Http请求与响应
查看>>
白山云科技校招:系统研发、机器学习、数据挖掘工程师
查看>>
SpringBoot实现定时任务的几种方式(常用)
查看>>
Web请求过程
查看>>
LRU 缓存淘汰算法的两种实现
查看>>