ddlddx0000 发表于 2018-10-26 10:43:13

mongoDB之用户及权限设置

  转自 http://www.tuicool.com/articles/nE7vaqj
  亲测有用
  之前用MongoDB没有设置用户和权限,一直都是本机应用程序连接MongoDB。在服务器上部署后对外没有开数据库连接端口,本机应用程序连接再开放应用程序端口供外部访问。想想很不对劲还是设置下用户授权吧。
  我部署的环境是windows下MongoDB2.4.8版本。首先要开启安全模式的话,在启动MongoDB服务的时候就要加上--auth参数,命令如下:
D:\mongodb>mongod.exe --dbpath D:\mongodb\data --logpath=D:\mongodb\logs\mongodb.log --auth  这样的话再连接MongoDB操作的时候就需要一定的权限。
  一、首先我们需要在没有加"--auth"权限参数启动服务进行用户权限管理
  我们这样启动MongoDB服务:
D:\mongodb>mongod.exe --dbpath D:\mongodb\data --logpath=D:\mongodb\logs\mongodb.log  这时不指定用户连接mongodb,是可以登录并操作的,我的操作如下:
D:\>mongo  
MongoDB shell version: 2.4.8connecting to: test> show dbs
  
admin   (empty)local   0.078125GB>
  可以看到默认显示两个库,我们需要进入admin库进行权限设置(这个时候因为服务没有添加权限参数,所以默认是有权限进行相关设置的)
  > use admin
  switched to db admin
  > db.addUser('sa','sa')
  {
  "user" : "sa",
  "readOnly" : false,
  "pwd" : "75692b1d11c072c6c79332e248c4f699",
  "_id" : ObjectId("53af835ada88ac42a40917a0")
  }
  > db.system.users.find()
  { "_id" : ObjectId("53af835ada88ac42a40917a0"), "user" : "sa", "readOnly" : false, "pwd" : "75692b1d11c072c6c79332e248c4f699" }
  二、我们关掉之前开启的服务,添加"--auth"权限参数,重新启动MongoDB服务
D:\>mongod --dbpath D:\mongodb\data --logpath=D:\mongodb\logs\mongodb.log --auth  我们再次连接并操作:
  D:\>mongo
  MongoDB shell version: 2.4.8
  connecting to: test
  > use admin
  switched to db admin
  > show collections
  Sun Jun 29 11:17:27.103 error: {
  "$err" : "not authorized for query on admin.system.namespaces",
  "code" : 16550
  } at src/mongo/shell/query.js:128
  发现如果不加身份信息默认连接的话,是没有权限操作的。我们验证下之前添加的用户,再操作试下:
> db.auth('sa','sa')1            //返回1表示验证成功,返回0表示验证失败  
> show collections
  
system.indexes
  
system.users
  发现验证成功可以对admin库进行操作了,我们再连接其他库试试:
D:\>mongo  
MongoDB shell version: 2.4.8connecting to: test> show collections
  
Sun Jun 29 11:20:17.996 error: {"$err" : "not authorized for query on test.system.namespaces","code" : 16550} at src/mongo/shell/query.js:128> db.auth('sa','sa')
  
Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" }0
  发现这里验证失败了,之前设置的admin用户验证不管用。查了资料知道必须先从admin登录再use其他库才能被验证通过:
D:\>mongo  
MongoDB shell version: 2.4.8connecting to: test> use admin
  
switched to db admin> db.auth('sa','sa')1> use test
  
switched to db test> show collections
  三、添加完顶层admin用户,可以用admin账户进行其他用户的设置
  如果想让单独的库有单独的用户名就得先从admin登录然后设置相应的用户信息,具体操作如下:
D:\>mongo  
MongoDB shell version: 2.4.8connecting to: test> use admin
  
switched to db admin> db.auth('sa','sa')1                              //先从admin登录> use test
  
switched to db test> show collections> db.addUser('test','test') //添加test库的用户{      "user" : "test",      "readOnly" : false,      "pwd" : "a6de521abefc2fed4f5876855a3484f5",      "_id" : ObjectId("53af874c5017b6747e68da2a")
  
}
  再次单独登录test试试:
D:\>mongo  
MongoDB shell version: 2.4.8connecting to: test> show collections       //未登录没有权限Sun Jun 29 11:27:52.899 error: {"$err" : "not authorized for query on test.system.namespaces","code" : 16550} at src/mongo/shell/query.js:128> db.auth('test','test')   //之前设置的账户1    //登录成功> show collections
  
system.indexes
  
system.users
  四、设置完admin账户后开启带权限参数的MongoDB服务在可视化管理器中管理用户权限
  在windows下我使用了MongoVUE可视化管理工具。建立新连接:
http://img0.tuicool.com/Bn2mAjY.jpg
  之前启动不带auth参数的服务时,不添加用户名密码连接是可以连接的,因为设置了auth参数所以这里必须添加用户名和密码;把上面设置的admin用户sa填入就可以连接上了。 连接上之后打开一个库,里面有个Users设置
http://img2.tuicool.com/6VbINj.jpg
  这里可以对某一个库进行用户的添加删除修改等操作,这是最高权限的admin视图,如果用test库的test用户登录的话,视图就只能看见test库了http://img0.tuicool.com/JFbARzM.jpg
  以上就是windows下使用MongoDB在shell和可视化工具中对用户权限设置的简单操作方法。


页: [1]
查看完整版本: mongoDB之用户及权限设置