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

数据库 其他数据库 MongoDB
MongoDB默认是不需要输入用户名和密码,客户就可以登录的。但是出于安全性的考虑,我们还是要为其设置用户名和密码。本文主要介绍的是MongoDB权限管理之用户名和密码的操作,希望能对您有所帮助。

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

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

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数据库回顾之存储过程的创建和应用
责任编辑:赵鹏 来源: CSDN博客
相关推荐

2022-06-24 08:48:47

用户名密码登录

2009-08-18 13:52:57

Ubuntu用户名密码

2020-07-11 09:26:16

数据泄露黑客网络攻击

2010-05-31 09:10:20

Myeclipse S

2011-09-06 10:36:44

2014-09-11 09:25:19

2023-12-26 09:33:47

2011-08-29 10:38:42

用LINQ去除重复菜单

2013-05-29 09:47:45

2010-05-24 14:00:43

Flex Svn

2019-08-26 19:24:55

Podman容器Linux

2009-06-18 15:05:11

2010-09-27 14:48:12

SQL用户名

2009-10-26 16:08:40

Oracle默认用户名

2013-11-21 09:10:27

MongoDB

2009-10-21 17:13:32

Oracle用户名

2013-01-04 17:51:28

Android开发SharedPrefe解析用户名

2019-11-12 09:23:16

Telnet密码抓包

2021-10-04 08:26:10

用户名密码信息

2011-01-11 14:06:39

点赞
收藏

51CTO技术栈公众号