博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongoDB 3.0 安全权限访问控制 -摘自网络
阅读量:6967 次
发布时间:2019-06-27

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

"E:\Program Files\MongoDB\Server\3.0\bin\mongod.exe" --logpath E:\mongodb\log\mongodblog.log --logappend --dbpath E:\mongodb\db --directoryperdb   --config "E:\Program Files\MongoDB\Server\3.0\bin\mongod.cfg"  --service

 

mongod.cfg:

logpath=E:\mongodb\log\mongodblog.log

dbpath=E:\mongodb\db
auth = true

 

MongoDB3.0权限,啥都不说了,谷歌百度出来的全是错的。先安装好盲沟,简单的没法说。

首先,不使用 —auth 参数,启动 mongoDB:

mongodb-linux-i686-3.0.0/bin/mongod -f mongodb-linux-i686-3.0.0/mongodb.conf

此时你 show dbs 会看到只有一个local数据库,那个所谓的admin是不存在的。

mongoDB 没有炒鸡无敌用户root,只有能管理用户的用户 。

打开 mongo shell

mongodb-linux-i686-3.0.0/bin/mongo

添加管理用户:

use admin db.createUser(   { user: "buru", pwd: "12345678", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )

roles 中的 db 参数是必须的,不然会报错:Error: couldn’t add user: Missing expected field “db”。另外,有很多文章记录的是使用 方法,这个方法是旧版的,3.0中已经不存在,详见:。

切换到admin下,查看刚才创建的用户:

show users 或 db.system.users.find()
{ "_id" : "admin.buru", "user" : "buru", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "gwVwuA/dXvxgSHavEnlyvA==", "storedKey" : "l2QEVTEujpkCuqDEKqfIWbSv4ms=", "serverKey" : "M1ofNKXg2sNCsFrBJbX4pXbSgvg=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }

怎么关闭 mongoDB?千万不要 kill -9 pid,可以 kill -2 pid 或 db.shutdownServer()

下面使用 —auth 参 数,重新启动 mongoDB:

mongodb-linux-i686-3.0.0/bin/mongod --auth -f mongodb-linux-i686-3.0.0/mongodb.conf

再次打开 mongo shell

mongodb-linux-i686-3.0.0/bin/mongo use admin db.auth("buru","12345678") #认证,返回1表示成功 或 mongodb-linux-i686-3.0.0/bin/mongo -u buru -p 12345678 --authenticationDatabase admin

此时

show collections

报错

2015-03-17T10:15:56.011+0800 E QUERY    Error: listCollections failed: {   "ok" : 0, "errmsg" : "not authorized on admin to execute command { listCollections: 1.0 }", "code" : 13 } at Error (
) at DB._getCollectionInfosCommand (src/mongo/shell/db.js:643:15) at DB.getCollectionInfos (src/mongo/shell/db.js:655:20) at DB.getCollectionNames (src/mongo/shell/db.js:666:17) at shellHelper.show (src/mongo/shell/utils.js:625:12) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/db.js:643

 

因为,用户buru只有用户管理的权限。

下面创建用户,用户都跟着库走,创建的用户都是

use tianhe db.createUser(  { user: "bao", pwd: "12345678", roles: [ { role: "readWrite", db: "tianhe" }, { role: "read", db: "tianhe2" } ] } )

查看刚刚创建的用户。

show users  {   "_id" : "tianhe.bao", "user" : "bao", "db" : "tianhe", "roles" : [ { "role" : "readWrite", "db" : "tianhe" }, { "role" : "read", "db" : "tianhe2" } ] }

查看整个mongoDB全部的用户:

use admin db.system.users.find()  { "_id" : "admin.buru", "user" : "buru", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "gwVwuA/dXvxgSHavEnlyvA==", "storedKey" : "l2QEVTEujpkCuqDEKqfIWbSv4ms=", "serverKey" : "M1ofNKXg2sNCsFrBJbX4pXbSgvg=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } { "_id" : "tianhe.bao", "user" : "bao", "db" : "tianhe", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "//xy1V1fbqEHC1gzQqZHGQ==", "storedKey" : "ZS/o54zzl/FdcXLQJ98KdAVTfF0=", "serverKey" : "iIpNYz2Gk8KhyK3zgz6muBt0PI4=" } }, "roles" : [ { "role" : "readWrite", "db" : "tianhe" }, { "role" : "read", "db" : "tianhe2" } ] }

创建完毕,验证一下:

use buru show collections  2015-03-17T10:30:06.461+0800 E QUERY Error: listCollections failed: { "ok" : 0, "errmsg" : "not authorized on buru to execute command { listCollections: 1.0 }", "code" : 13 } at Error (
) at DB._getCollectionInfosCommand (src/mongo/shell/db.js:643:15) at DB.getCollectionInfos (src/mongo/shell/db.js:655:20) at DB.getCollectionNames (src/mongo/shell/db.js:666:17) at shellHelper.show (src/mongo/shell/utils.js:625:12) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/db.js:643 `

 

显然没权限,先auth:

db.auth("bao","12345678") 1 show collections news system.indexes wahaha

完毕!

参考: Mongo Shell: Enable Access Control: Add a User to a Database: User Methods: Role Methods:

转载地址:http://njisl.baihongyu.com/

你可能感兴趣的文章
IIS如何设置可以让.aspx后缀的文件直接下载
查看>>
为什么区块链对中小企业至关重要?
查看>>
笨办法学 Linux 中文版 翻译完成
查看>>
js 写入图片Exif信息piexifjs
查看>>
IBM携手三菱东京日联银行 将区块链用于合同管理
查看>>
Mac 10.12安装WebStorm
查看>>
Spring Cloud启动应用时指定IP或忽略某张网卡配置
查看>>
Jenkins配置MSBuild实现自动部署2(项目实践)
查看>>
kafka好文章
查看>>
IBM发布超强量子计算机,可处理50个量子位
查看>>
如何使用Bro IDS和Intel Critical Stack分析网络活动
查看>>
Memcached的Web管理工具MemAdmin(待实践)
查看>>
嵌入式学习难点 嵌入式软件学习
查看>>
11204 ASM 在线存储迁移。
查看>>
eclipse不会自动编译的问题解决
查看>>
linux netstat命令
查看>>
淘宝卖家遭恶退诈骗 阿里一年来协助警方抓获103人
查看>>
拥2180亿美元收入,苹果成全球最大IT企业
查看>>
数据库连接池的工作原理
查看>>
网络抓包工具wireshark and tcpdump 及其实现基于的libpcap
查看>>