MongoDB权限管理之用户名和密码的操作

本文我们介绍MongoDB权限管理,主要介绍的是如何设置用户名和密码。接下来我们就一一介绍。

创新互联专业为企业提供铜梁网站建设、铜梁做网站、铜梁网站设计、铜梁网站制作等企业网站建设、网页设计与制作、铜梁企业网站模板建站服务,10年铜梁做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

添加用户的时候必须满足以下两个条件:

1.有相关权限的情况下(后面会说)。

2.mongod没有加--auth的情况下(如果加了,你添加权限的话 会出现下面的情况)。

 

 
 
 
  1. > use admin    
  2.  
  3. switched to db admin    
  4.  
  5. > db.addUser('sa','sa')    
  6.  
  7. Fri Jul 22 14:31:13 uncaught exception: error {    
  8.  
  9. "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",    
  10.  
  11. "code" : 10057    
  12.  
  13. }    
  14.  
  15. >    

 

所以我们添加用户时必须先在没有加--auth的时候添加个super  admin。

服务起来后,进入./mongo。

 

 
 
 
  1. [root@:/usr/local/mongodb/bin]#./mongo    
  2.  
  3. MongoDB shell version: 1.8.2    
  4.  
  5. connecting to: test    
  6.  
  7. > use admin    
  8.  
  9. switched to db admin    
  10.  
  11. > db.adduser('sa','sa')    
  12.  
  13. Fri Jul 22 14:34:24 TypeError: db.adduser is not a function (shell):1    
  14.  
  15. > db.addUser('sa','sa')    
  16.  
  17. {    
  18.  
  19. "_id" : ObjectId("4e2914a585178da4e03a16c3"),    
  20.  
  21. "user" : "sa",    
  22.  
  23. "readOnly" : false,    
  24.  
  25. "pwd" : "75692b1d11c072c6c79332e248c4f699"    
  26.  
  27. }    
  28.  
  29. >    

 

这样就说明 已经成功建立了,然后我们试一下权限。

 

 
 
 
  1. > show collections    
  2.  
  3. system.indexes    
  4.  
  5. system.users   

 

在没有加--auth的情况下 可以正常访问admin喜爱默认的两个表。

 

 
 
 
  1. > db.system.users.find()    
  2.  
  3. { "_id" : ObjectId("4e2914a585178da4e03a16c3"), "user" : "sa", "readOnly" : false, "pwd" : "75692b1d11c072c6c79332e248c4f699" }>    

 

已经成功建立。

下面把服务加上--auth的选项,再进入./mongo。

 

 
 
 
  1. MongoDB shell version: 1.8.2    
  2.  
  3. connecting to: test    
  4.  
  5. > use admin    
  6.  
  7. switched to db admin    
  8.  
  9. > show collections    
  10.  
  11. Fri Jul 22 14:38:49 uncaught exception: error: {    
  12.  
  13. "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",    
  14.  
  15. "code" : 10057    
  16.  
  17. }    
  18.  
  19. >    

 

可以看出已经没有访问权限了。

我们就用自己的密钥登录下:

 

 
 
 
  1. > db.auth('sa','sa')    
  2.  
  3. 1   

 

返回1说明验证成功!

再show collections下就成功了。

.....

我们登录其它表试试:

 

 
 
 
  1. [root@:/usr/local/mongodb/bin]#./mongo    
  2.  
  3. MongoDB shell version: 1.8.2    
  4.  
  5. connecting to: test    
  6.  
  7. > use test    
  8.  
  9. switched to db test    
  10.  
  11. > show collections    
  12.  
  13. Fri Jul 22 14:40:47 uncaught exception: error: {    
  14.  
  15. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",    
  16.  
  17. "code" : 10057    
  18.  
  19. }   

 

也需要验证,试试super admin登录:

 
 
 
  1. [root@:/usr/local/mongodb/bin]#./mongo    
  2.  
  3. MongoDB shell version: 1.8.2    
  4.  
  5. connecting to: test    
  6.  
  7. > use test    
  8.  
  9. switched to db test    
  10.  
  11. > show collections    
  12.  
  13. Fri Jul 22 14:40:47 uncaught exception: error: {    
  14.  
  15. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",    
  16.  
  17. "code" : 10057    
  18.  
  19. }    
  20.  
  21. > db.auth('sa','sa')    
  22.  
  23. 0   

 

返回0验证失败。 

好吧,不绕圈子,其实super admin必须从admin那么登录 然后 再use其它表才可以。

 

 
 
 
  1. > use admin    
  2.  
  3. > use admin  
  4.  
  5. switched to db admin    
  6.  
  7. > db.auth('sa','sa')    
  8.  
  9. 1    
  10.  
  11. > use test    
  12.  
  13. switched to db test    
  14.  
  15. > show collections    
  16.  
  17. >    

 

如果想单独访问一个表,用独立的用户名,就需要在那个表里面建相应的user。

 

 
 
 
  1. [root@:/usr/local/mongodb/bin]#./mongo    
  2.  
  3. MongoDB shell version: 1.8.2    
  4.  
  5. connecting to: test    
  6.  
  7. > use admin    
  8.  
  9. switched to db admin    
  10.  
  11. > db.auth('sa','sa')    
  12.  
  13. 1    
  14.  
  15. > use test    
  16.  
  17. switched to db test    
  18.  
  19. > db.addUser('test','test')    
  20.  
  21. {    
  22.  
  23. "user" : "test",    
  24.  
  25. "readOnly" : false,    
  26.  
  27. "pwd" : "a6de521abefc2fed4f5876855a3484f5"    
  28.  
  29. }    
  30.  
  31. >    

 

当然必须有相关权限才可以建立。

再登录看看:

 

 
 
 
  1. [root@:/usr/local/mongodb/bin]#./mongo    
  2.  
  3. MongoDB shell version: 1.8.2    
  4.  
  5. connecting to: test    
  6.  
  7. > show collections    
  8.  
  9. Fri Jul 22 14:45:08 uncaught exception: error: {    
  10.  
  11. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",    
  12.  
  13. "code" : 10057    
  14.  
  15. }    
  16.  
  17. > db.auth('test','test')    
  18.  
  19. 1    
  20.  
  21. > show collections    
  22.  
  23. system.indexes    
  24.  
  25. system.users    
  26.  
  27. >    

 

 关于MongoDB权限管理就介绍到这里,希望能对各位有所帮助。

【编辑推荐】

  1. 浅谈Oracle Buffer Cache的优化思路
  2. 不同数据库对blob字段的处理代码演示
  3. RedHat Linux的Oracle 10g安装配置详解
  4. Oracle数据库使用存储过程创建自动增长列
  5. SQL Server数据库回顾之存储过程的创建和应用

当前标题:MongoDB权限管理之用户名和密码的操作
文章分享:http://www.zyruijie.cn/qtweb/news26/9376.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联