转自:http://blog.csdn.net/allen_jinjie/article/details/9235073

1. 最开始的时候,我们启动mongodb,但是不包含--auth参数:

  1. E:\MongoDB\bin>mongod --dbpath=E:\mongodb\db
  2. Thu Jul 04 16:31:58.700 [initandlisten] db version v2.4.4
  3. Thu Jul 04 16:31:58.700 [initandlisten] git version: 4ec1fb96702c9d4c57b1e06dd34eb73a16e407d2
  4. Thu Jul 04 16:31:58.700 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, servic
  5. e_pack='Service Pack 2') BOOST_LIB_VERSION=1_49
  6. Thu Jul 04 16:31:58.700 [initandlisten] allocator: system
  7. Thu Jul 04 16:31:58.700 [initandlisten] options: { dbpath: "E:\mongodb\db" }
  8. Thu Jul 04 16:31:58.731 [initandlisten]
  9. Thu Jul 04 16:31:58.731 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.r
  10. eplset
  11. Thu Jul 04 16:31:58.731 [initandlisten] **          Restart with --replSet unless you are doing maintenance and no other clients a
  12. re connected.
  13. Thu Jul 04 16:31:58.731 [initandlisten] **          The TTL collection monitor will not start because of this.
  14. Thu Jul 04 16:31:58.731 [initandlisten] **          For more info see http://dochub.mongodb.org/core/ttlcollections
  15. Thu Jul 04 16:31:58.731 [initandlisten]
  16. Thu Jul 04 16:31:58.981 [initandlisten] waiting for connections on port 27017
  17. Thu Jul 04 16:31:58.981 [websvr] admin web console waiting for connections on port 28017

另开一Dos窗口,直接连接到test数据库上:

  1. E:\MongoDB\bin>mongo
  2. MongoDB shell version: 2.4.4
  3. connecting to: test

2. 连接到admin数据库,在admin数据库上创建一个用户,这个用户保存在admin.system.users中,它的权限比在其它数据库中设置的用户权限更大。(当admin.system.users中一个用户都没有时,即使mongod启动时添加了--auth参数,如果没有在admin数据库中添加用户,此时不进行任何认证还是可以做任何操作,直到在admin.system.users中添加了一个用户。)

  1. > use admin
  2. switched to db admin
  3. > db.system.users.find()
  4. > db.addUser("allenlei","123456")
  5. {
  6. "user" : "allenlei",
  7. "readOnly" : false,
  8. "pwd" : "a9eadc99bab4734b32f5bc4148d866c6",
  9. "_id" : ObjectId("51d534878704a2ac963ed790")
  10. }
  11. > db.system.users.find()
  12. { "_id" : ObjectId("51d534878704a2ac963ed790"), "user" : "allenlei", "readOnly" : false, "pwd" : "a9eadc99bab4734b32f5bc4148d866c6
  13. " }
  14. >

3. 现在admin数据库中已经有用户信息了,我们关掉mongodb, 重新启动,这次带有--auth 参数。

  1. E:\MongoDB\bin>mongod --dbpath=E:\mongodb\db --auth
  2. Thu Jul 04 16:44:57.393 [initandlisten] db version v2.4.4
  3. Thu Jul 04 16:44:57.393 [initandlisten] git version: 4ec1fb96702c9d4c57b1e06dd34eb73a16e407d2
  4. Thu Jul 04 16:44:57.409 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, servic
  5. e_pack='Service Pack 2') BOOST_LIB_VERSION=1_49
  6. Thu Jul 04 16:44:57.409 [initandlisten] allocator: system
  7. Thu Jul 04 16:44:57.409 [initandlisten] options: { auth: true, dbpath: "E:\mongodb\db" }
  8. Thu Jul 04 16:44:57.440 [initandlisten]
  9. Thu Jul 04 16:44:57.440 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.r
  10. eplset
  11. Thu Jul 04 16:44:57.440 [initandlisten] **          Restart with --replSet unless you are doing maintenance and no other clients a
  12. re connected.
  13. Thu Jul 04 16:44:57.440 [initandlisten] **          The TTL collection monitor will not start because of this.
  14. Thu Jul 04 16:44:57.440 [initandlisten] **          For more info see http://dochub.mongodb.org/core/ttlcollections
  15. Thu Jul 04 16:44:57.440 [initandlisten]
  16. Thu Jul 04 16:44:57.549 [websvr] admin web console waiting for connections on port 28017
  17. Thu Jul 04 16:44:57.549 [initandlisten] waiting for connections on port 27017

4. 由于指定了-auth参数,那么连接到数据库上就需要提供登录账户,尽管不提供也可以登录到test这个默认数据库,但是没办法操作:

  1. E:\MongoDB\bin>mongo
  2. MongoDB shell version: 2.4.4
  3. connecting to: test
  4. > show collections
  5. Thu Jul 04 16:53:51.752 JavaScript execution failed: error: {
  6. "$err" : "not authorized for query on test.system.namespaces",
  7. "code" : 16550
  8. } at src/mongo/shell/query.js:L128
  9. >

5. 现在我们指定连接到admin数据库,如果账户不对:

  1. E:\MongoDB\bin>mongo --authenticationDatabase admin -u allenlei -p
  2. MongoDB shell version: 2.4.4
  3. Enter password:
  4. connecting to: test
  5. 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
  6. s:L228
  7. exception: login failed

6. 奇怪的是,就算是账户正确,我的机器上也是显示连接到test数据库而不是admin。我需要转到admin数据库上,(root是建立在test数据库上的账户)

  1. E:\MongoDB\bin>mongo --authenticationDatabase admin -u allenlei -p
  2. MongoDB shell version: 2.4.4
  3. Enter password:
  4. connecting to: test
  5. > db.system.users.find()
  6. { "_id" : ObjectId("51d3e1c94ef3aba14566b889"), "user" : "root", "readOnly" : false, "pwd" : "b3098ef4591719e9f75972a75883726b" }
  7. > use admin
  8. switched to db admin
  9. > db.system.users.find()
  10. { "_id" : ObjectId("51d5378a6a7de1fde965535c"), "user" : "allenlei", "readOnly" : false, "pwd" : "a9eadc99bab4734b32f5bc4148d866c6
  11. " }
  12. >
  13. > show collections
  14. system.indexes
  15. system.users
  16. > use tutorial
  17. switched to db tutorial
  18. > show collections
  19. newCollection_noCapped
  20. numbers
  21. person
  22. personalinfo
  23. photo.chunks
  24. photo.files
  25. student
  26. student_res
  27. system.indexes
  28. system.users
  29. users
  30. >

可以看出,通过admin数据库登入,可以以登录账户进入其他数据库进行操作。

7. 现在用root账号登入test数据库:

  1. E:\MongoDB\bin>mongo -authenticationDatabase test -u root -p
  2. MongoDB shell version: 2.4.4
  3. Enter password:
  4. connecting to: test
  5. > show collections
  6. person
  7. system.indexes
  8. system.users
  9. > db.system.users.find()
  10. { "_id" : ObjectId("51d53a706ce04d74431706b4"), "user" : "root", "readOnly" : false, "pwd" : "34e5772aa66b703a319641d42a47d696" }
  11. > use tutorial
  12. switched to db tutorial
  13. > show collections
  14. Thu Jul 04 17:04:51.186 JavaScript execution failed: error: {
  15. "$err" : "not authorized for query on tutorial.system.namespaces",
  16. "code" : 16550
  17. } at src/mongo/shell/query.js:L128
  18. >

root账户属于test而不是admin数据库,权限只能在本数据库使用,而不像allenlei可以到tutorial数据库操作。

mongoDB authentication的更多相关文章

  1. 用navicat进行身份验证连接出现cannot connect to Mongodb authentication failed

    用navicat进行身份验证连接出现cannot connect to Mongodb authentication failed. 解决办法: 1.打开mongoDB连接 win+r --cmd-- ...

  2. MongoDB authentication failed

    0.随笔摘要: MongoDB  安装配置 MongoDB  权限控制 MongoDB  注意事项 authentication failed 1.MongoDB  下载安装配置 MongoDB官网  ...

  3. [转]mongodb authentication 设置权限之后,新建个管理账户和一般数据库用户,在win 7 64bit 环境下测试使用实例

    如果之前安装mongodb时没有使用 --auth,那么必须要卸载MongoDB服务,进行重新安装,设置账号权限才生效! 主要是解决在测试使用mongo db 时候,总是出现的MongoAuthent ...

  4. windows7下MongoDB(V3.4)的使用及仓储设计

    简单的介绍一下,我使用MongoDB的场景. 我们现在的物联网环境下,有部分数据,采样频率为2000条记录/分钟,这样下来一天24*60*2000=2880000约等于300万条数据,以后必然还会增加 ...

  5. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程

    我们在之前的文章中中已经讲到了正确部署运行cas server 和 在cas client中配置. 在此基础上 我们去掉了https的验证,启用了http访问的模式. 单点登录(七)-----实战-- ...

  6. MongoDB 用户名密码登录

    Mongodb enable authentication MongoDB 默认直接连接,无须身份验证,如果当前机器可以公网访问,且不注意Mongodb 端口(默认 27017)的开放状态,那么Mon ...

  7. RESTheart安装与设置

    作者:Maurizio Turatti, 最后在Feb 25, 2016时被 Andrea Di Cesare更新 安装与设置 1. 快速开始 Docker Vagrant Bare metal 2. ...

  8. 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 ...

  9. mongodb的认证(authentication)与授权(authorization)

    一小白瞎整mongodb,认证部分被折磨的惨不忍睹,看厮可怜,特查了一下文档,浅显地总结一下mongodb认证(authentication)与授权(authorization)的联系. 创建的所有用 ...

随机推荐

  1. asp.net--CRSF

    asp.net使用了token来防止CRSF攻击 前台: 使用@Html.AntiForgeryToken(); 浏览器里面被存了一个cookie值,这个值是asp.net存给浏览器的,是readon ...

  2. CLR与Netframework版本的关系

    CLR有3个版本,详细地址看微软官方文档

  3. P2310 loidc,看看海

    P2310 loidc,看看海 题目背景 loidc喜欢大海.在他放假的时候他经常一个人跑到海边独自玩耍. 在浪花的冲击下,他可以忘记打代码的烦躁,真是惬意极了. 虽然今天是周六,但今天可是11.8号 ...

  4. I - Tunnel Warfare

    I - Tunnel Warfare HDU - 1540 思路:原来以为自己已经完全理解了线段树,现在发现其实还差一些火候,做题的时候太拘泥于格式,思路不是很能放开. #include<cst ...

  5. HDU-4451-Dressing (2012年金华赛区J题)

    Dressing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. pl/sql developer 快捷操作: 显示不可见字符 显示历史sql语句 拷贝整个sql窗口的语句至新的sql窗口

    pl/sql developer 快捷操作: 显示不可见字符 显示历史sql语句 拷贝整个sql窗口的语句至新的sql窗口 显示不可见字符:可以把空格.回车显示出来: 显示历史sql语句:ctrl+e ...

  7. [Java]对字符串中的每一个单词个数进行统计

    这是来自一道电面的题. 单词统计非常easy想到用Map来统计,于是想到了用HashMap. 可是我却没有想到用split来切割单词,想着用遍历字符的方式来推断空格.人家面试官就说了,假设单词之间不止 ...

  8. Codeforces444A_DZY Loves Physics

    DZY Loves Physics time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. iOS 在 ARC 环境下 dealloc 的使用、理解误区

    iOS 在 ARC 环境下 dealloc 的使用.理解误区 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&qu ...

  10. MySQL主从复制和读写分离

    我们知道应用对数据库的訪问通常情况下大部分都是读操作,写仅仅占非常少一部分.因此读写分离(read-write-splitting)能有效减少主库压力,从而解决站点发展过程中遇到的第一次数据库瓶颈. ...