mongoDB authentication
转自:http://blog.csdn.net/allen_jinjie/article/details/9235073
1. 最开始的时候,我们启动mongodb,但是不包含--auth参数:
- E:\MongoDB\bin>mongod --dbpath=E:\mongodb\db
- Thu Jul 04 16:31:58.700 [initandlisten] db version v2.4.4
- Thu Jul 04 16:31:58.700 [initandlisten] git version: 4ec1fb96702c9d4c57b1e06dd34eb73a16e407d2
- Thu Jul 04 16:31:58.700 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, servic
- e_pack='Service Pack 2') BOOST_LIB_VERSION=1_49
- Thu Jul 04 16:31:58.700 [initandlisten] allocator: system
- Thu Jul 04 16:31:58.700 [initandlisten] options: { dbpath: "E:\mongodb\db" }
- Thu Jul 04 16:31:58.731 [initandlisten]
- Thu Jul 04 16:31:58.731 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.r
- eplset
- Thu Jul 04 16:31:58.731 [initandlisten] ** Restart with --replSet unless you are doing maintenance and no other clients a
- re connected.
- Thu Jul 04 16:31:58.731 [initandlisten] ** The TTL collection monitor will not start because of this.
- Thu Jul 04 16:31:58.731 [initandlisten] ** For more info see http://dochub.mongodb.org/core/ttlcollections
- Thu Jul 04 16:31:58.731 [initandlisten]
- Thu Jul 04 16:31:58.981 [initandlisten] waiting for connections on port 27017
- Thu Jul 04 16:31:58.981 [websvr] admin web console waiting for connections on port 28017
另开一Dos窗口,直接连接到test数据库上:
- E:\MongoDB\bin>mongo
- MongoDB shell version: 2.4.4
- connecting to: test
2. 连接到admin数据库,在admin数据库上创建一个用户,这个用户保存在admin.system.users中,它的权限比在其它数据库中设置的用户权限更大。(当admin.system.users中一个用户都没有时,即使mongod启动时添加了--auth参数,如果没有在admin数据库中添加用户,此时不进行任何认证还是可以做任何操作,直到在admin.system.users中添加了一个用户。)
- > use admin
- switched to db admin
- > db.system.users.find()
- > db.addUser("allenlei","123456")
- {
- "user" : "allenlei",
- "readOnly" : false,
- "pwd" : "a9eadc99bab4734b32f5bc4148d866c6",
- "_id" : ObjectId("51d534878704a2ac963ed790")
- }
- > db.system.users.find()
- { "_id" : ObjectId("51d534878704a2ac963ed790"), "user" : "allenlei", "readOnly" : false, "pwd" : "a9eadc99bab4734b32f5bc4148d866c6
- " }
- >
3. 现在admin数据库中已经有用户信息了,我们关掉mongodb, 重新启动,这次带有--auth 参数。
- E:\MongoDB\bin>mongod --dbpath=E:\mongodb\db --auth
- Thu Jul 04 16:44:57.393 [initandlisten] db version v2.4.4
- Thu Jul 04 16:44:57.393 [initandlisten] git version: 4ec1fb96702c9d4c57b1e06dd34eb73a16e407d2
- Thu Jul 04 16:44:57.409 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, servic
- e_pack='Service Pack 2') BOOST_LIB_VERSION=1_49
- Thu Jul 04 16:44:57.409 [initandlisten] allocator: system
- Thu Jul 04 16:44:57.409 [initandlisten] options: { auth: true, dbpath: "E:\mongodb\db" }
- Thu Jul 04 16:44:57.440 [initandlisten]
- Thu Jul 04 16:44:57.440 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.r
- eplset
- Thu Jul 04 16:44:57.440 [initandlisten] ** Restart with --replSet unless you are doing maintenance and no other clients a
- re connected.
- Thu Jul 04 16:44:57.440 [initandlisten] ** The TTL collection monitor will not start because of this.
- Thu Jul 04 16:44:57.440 [initandlisten] ** For more info see http://dochub.mongodb.org/core/ttlcollections
- Thu Jul 04 16:44:57.440 [initandlisten]
- Thu Jul 04 16:44:57.549 [websvr] admin web console waiting for connections on port 28017
- Thu Jul 04 16:44:57.549 [initandlisten] waiting for connections on port 27017
4. 由于指定了-auth参数,那么连接到数据库上就需要提供登录账户,尽管不提供也可以登录到test这个默认数据库,但是没办法操作:
- E:\MongoDB\bin>mongo
- MongoDB shell version: 2.4.4
- connecting to: test
- > show collections
- Thu Jul 04 16:53:51.752 JavaScript execution failed: error: {
- "$err" : "not authorized for query on test.system.namespaces",
- "code" : 16550
- } at src/mongo/shell/query.js:L128
- >
5. 现在我们指定连接到admin数据库,如果账户不对:
- E:\MongoDB\bin>mongo --authenticationDatabase admin -u allenlei -p
- MongoDB shell version: 2.4.4
- Enter password:
- connecting to: test
- Thu Jul 04 16:56:55.569 JavaScript execution failed: Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.j
- s:L228
- exception: login failed
6. 奇怪的是,就算是账户正确,我的机器上也是显示连接到test数据库而不是admin。我需要转到admin数据库上,(root是建立在test数据库上的账户)
- E:\MongoDB\bin>mongo --authenticationDatabase admin -u allenlei -p
- MongoDB shell version: 2.4.4
- Enter password:
- connecting to: test
- > db.system.users.find()
- { "_id" : ObjectId("51d3e1c94ef3aba14566b889"), "user" : "root", "readOnly" : false, "pwd" : "b3098ef4591719e9f75972a75883726b" }
- > use admin
- switched to db admin
- > db.system.users.find()
- { "_id" : ObjectId("51d5378a6a7de1fde965535c"), "user" : "allenlei", "readOnly" : false, "pwd" : "a9eadc99bab4734b32f5bc4148d866c6
- " }
- >
- > show collections
- system.indexes
- system.users
- > use tutorial
- switched to db tutorial
- > show collections
- newCollection_noCapped
- numbers
- person
- personalinfo
- photo.chunks
- photo.files
- student
- student_res
- system.indexes
- system.users
- users
- >
可以看出,通过admin数据库登入,可以以登录账户进入其他数据库进行操作。
7. 现在用root账号登入test数据库:
- E:\MongoDB\bin>mongo -authenticationDatabase test -u root -p
- MongoDB shell version: 2.4.4
- Enter password:
- connecting to: test
- > show collections
- person
- system.indexes
- system.users
- > db.system.users.find()
- { "_id" : ObjectId("51d53a706ce04d74431706b4"), "user" : "root", "readOnly" : false, "pwd" : "34e5772aa66b703a319641d42a47d696" }
- > use tutorial
- switched to db tutorial
- > show collections
- Thu Jul 04 17:04:51.186 JavaScript execution failed: error: {
- "$err" : "not authorized for query on tutorial.system.namespaces",
- "code" : 16550
- } at src/mongo/shell/query.js:L128
- >
root账户属于test而不是admin数据库,权限只能在本数据库使用,而不像allenlei可以到tutorial数据库操作。
mongoDB authentication的更多相关文章
- 用navicat进行身份验证连接出现cannot connect to Mongodb authentication failed
用navicat进行身份验证连接出现cannot connect to Mongodb authentication failed. 解决办法: 1.打开mongoDB连接 win+r --cmd-- ...
- MongoDB authentication failed
0.随笔摘要: MongoDB 安装配置 MongoDB 权限控制 MongoDB 注意事项 authentication failed 1.MongoDB 下载安装配置 MongoDB官网 ...
- [转]mongodb authentication 设置权限之后,新建个管理账户和一般数据库用户,在win 7 64bit 环境下测试使用实例
如果之前安装mongodb时没有使用 --auth,那么必须要卸载MongoDB服务,进行重新安装,设置账号权限才生效! 主要是解决在测试使用mongo db 时候,总是出现的MongoAuthent ...
- windows7下MongoDB(V3.4)的使用及仓储设计
简单的介绍一下,我使用MongoDB的场景. 我们现在的物联网环境下,有部分数据,采样频率为2000条记录/分钟,这样下来一天24*60*2000=2880000约等于300万条数据,以后必然还会增加 ...
- 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程
我们在之前的文章中中已经讲到了正确部署运行cas server 和 在cas client中配置. 在此基础上 我们去掉了https的验证,启用了http访问的模式. 单点登录(七)-----实战-- ...
- MongoDB 用户名密码登录
Mongodb enable authentication MongoDB 默认直接连接,无须身份验证,如果当前机器可以公网访问,且不注意Mongodb 端口(默认 27017)的开放状态,那么Mon ...
- RESTheart安装与设置
作者:Maurizio Turatti, 最后在Feb 25, 2016时被 Andrea Di Cesare更新 安装与设置 1. 快速开始 Docker Vagrant Bare metal 2. ...
- mongodb 使用mongodump备份 指定用户名密码 出现错误 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed
mongodb 使用mongodump备份 指定用户名密码 出现错误 [root@MongoDB ~]# mongodump --host -u admin -p -d db1 -o /root/ F ...
- mongodb的认证(authentication)与授权(authorization)
一小白瞎整mongodb,认证部分被折磨的惨不忍睹,看厮可怜,特查了一下文档,浅显地总结一下mongodb认证(authentication)与授权(authorization)的联系. 创建的所有用 ...
随机推荐
- (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...
- jvm学习-垃圾回收器(四)
说明 各种垃圾回收算法都有各自的优缺点.jvm也并没有只采用一种垃圾算法.并提供几种组合供我根据场景进行选择. jvm内存结构 Person p=new Person(); 1.程序里面创建一个对象会 ...
- 洛谷 P1640 BZOJ 1854 [SCOI2010]连续攻击游戏
题目描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备 ...
- 【ACM】poj_2080_Calendar_201307311043
CalendarTime Limit: 1000MS Memory Limit: 30000K Total Submissions: 9787 Accepted: 3677 Description ...
- POJ 2002 Squares【值得摸索的一道二分+点旋转】
id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...
- 通过UrlRewriter配置MVC4伪静态
有些项目须要设置静态.这样能够被站点收录了,提高站点的排名.内容. 假设地址后面有www.a.com/xx.html?id=1是不行,还是不能达到一些需求.怎么才干实现www.a.com/1/xx.h ...
- HDU 5291(Candy Distribution-差值dp)
Candy Distribution Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- sikuli+eclipse实例
设置sikuli环境变量 如果在执行脚本的时候出现以下错误: Getting the VisionProxy.dll: Can not find dependent libraries... 把Sik ...
- JavaScript 和Ajax跨域问题
json格式: { "message":"获取成功", "state":"1", "result": ...
- 深度学习利器:TensorFlow在智能终端中的应用——智能边缘计算,云端生成模型给移动端下载,然后用该模型进行预测
前言 深度学习在图像处理.语音识别.自然语言处理领域的应用取得了巨大成功,但是它通常在功能强大的服务器端进行运算.如果智能手机通过网络远程连接服务器,也可以利用深度学习技术,但这样可能会很慢,而且只有 ...