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

来源:互联网 发布:ant java 参数 编辑:程序博客网 时间:2024/06/08 12:37

[html] view plain copy
  1. MongoDB默认是不需要输入用户名和密码,客户就可以登录的。但是出于安全性的考虑,我们还是要为其设置用户名和密码。本文主要介绍的是MongoDB权限管理之用户名和密码的操作,希望能对您有所帮助。  
  2.   
  3. AD:  
  4.   
  5.   
  6.   
  7.   
  8. 本文我们介绍MongoDB权限管理,主要介绍的是如何设置用户名和密码。接下来我们就一一介绍。  
  9.   
  10. 添加用户的时候必须满足以下两个条件:  
  11.   
  12. 1.有相关权限的情况下(后面会说)。  
  13.   
  14. 2.mongod没有加--auth的情况下(如果加了,你添加权限的话 会出现下面的情况)。  
  15.   
  16.   
  17. > use admin      
  18.    
  19. switched to db admin      
  20.    
  21. > db.addUser('sa','sa')      
  22.    
  23. Fri Jul 22 14:31:13 uncaught exception: error {      
  24.    
  25. "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",      
  26.    
  27. "code" : 10057      
  28.    
  29. }      
  30.    
  31. >      
  32. 所以我们添加用户时必须先在没有加--auth的时候添加个super  admin。  
  33.   
  34. 服务起来后,进入./mongo。  
  35.   
  36.   
  37. [root@:/usr/local/mongodb/bin]#./mongo      
  38.    
  39. MongoDB shell version: 1.8.2      
  40.    
  41. connecting to: test      
  42.    
  43. > use admin      
  44.    
  45. switched to db admin      
  46.    
  47. > db.adduser('sa','sa')      
  48.    
  49. Fri Jul 22 14:34:24 TypeError: db.adduser is not a function (shell):1      
  50.    
  51. > db.addUser('sa','sa')      
  52.    
  53. {      
  54.    
  55. "_id" : ObjectId("4e2914a585178da4e03a16c3"),      
  56.    
  57. "user" : "sa",      
  58.    
  59. "readOnly" : false,      
  60.    
  61. "pwd" : "75692b1d11c072c6c79332e248c4f699"      
  62.    
  63. }      
  64.    
  65. >      
  66. 这样就说明 已经成功建立了,然后我们试一下权限。  
  67.   
  68.   
  69. > show collections      
  70.    
  71. system.indexes      
  72.    
  73. system.users     
  74. 在没有加--auth的情况下 可以正常访问admin喜爱默认的两个表。  
  75.   
  76.   
  77. > db.system.users.find()      
  78.    
  79. { "_id" : ObjectId("4e2914a585178da4e03a16c3"), "user" : "sa", "readOnly" : false, "pwd" : "75692b1d11c072c6c79332e248c4f699" }>      
  80. 已经成功建立。  
  81.   
  82. 下面把服务加上--auth的选项,再进入./mongo。  
  83.   
  84.   
  85. MongoDB shell version: 1.8.2      
  86.    
  87. connecting to: test      
  88.    
  89. > use admin      
  90.    
  91. switched to db admin      
  92.    
  93. > show collections      
  94.    
  95. Fri Jul 22 14:38:49 uncaught exception: error: {      
  96.    
  97. "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",      
  98.    
  99. "code" : 10057      
  100.    
  101. }      
  102.    
  103. >      
  104. 可以看出已经没有访问权限了。  
  105.   
  106. 我们就用自己的密钥登录下:  
  107.   
  108.   
  109. > db.auth('sa','sa')      
  110.    
  111. 1     
  112. 返回1说明验证成功!  
  113.   
  114. 再show collections下就成功了。  
  115.   
  116. .....  
  117.   
  118. 我们登录其它表试试:  
  119.   
  120.   
  121. [root@:/usr/local/mongodb/bin]#./mongo      
  122.    
  123. MongoDB shell version: 1.8.2      
  124.    
  125. connecting to: test      
  126.    
  127. > use test      
  128.    
  129. switched to db test      
  130.    
  131. > show collections      
  132.    
  133. Fri Jul 22 14:40:47 uncaught exception: error: {      
  134.    
  135. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",      
  136.    
  137. "code" : 10057      
  138.    
  139. }     
  140. 也需要验证,试试super admin登录:  
  141.   
  142.   
  143. [root@:/usr/local/mongodb/bin]#./mongo      
  144.    
  145. MongoDB shell version: 1.8.2      
  146.    
  147. connecting to: test      
  148.    
  149. > use test      
  150.    
  151. switched to db test      
  152.    
  153. > show collections      
  154.    
  155. Fri Jul 22 14:40:47 uncaught exception: error: {      
  156.    
  157. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",      
  158.    
  159. "code" : 10057      
  160.    
  161. }      
  162.    
  163. > db.auth('sa','sa')      
  164.    
  165. 0     
  166. 返回0验证失败。   
  167.   
  168. 好吧,不绕圈子,其实super admin必须从admin那么登录 然后 再use其它表才可以。  
  169.   
  170.   
  171. > use admin      
  172.    
  173. > use admin    
  174.    
  175. switched to db admin      
  176.    
  177. > db.auth('sa','sa')      
  178.    
  179. 1      
  180.    
  181. > use test      
  182.    
  183. switched to db test      
  184.    
  185. > show collections      
  186.    
  187. >      
  188. 如果想单独访问一个表,用独立的用户名,就需要在那个表里面建相应的user。  
  189.   
  190.   
  191. [root@:/usr/local/mongodb/bin]#./mongo      
  192.    
  193. MongoDB shell version: 1.8.2      
  194.    
  195. connecting to: test      
  196.    
  197. > use admin      
  198.    
  199. switched to db admin      
  200.    
  201. > db.auth('sa','sa')      
  202.    
  203. 1      
  204.    
  205. > use test      
  206.    
  207. switched to db test      
  208.    
  209. > db.addUser('test','test')      
  210.    
  211. {      
  212.    
  213. "user" : "test",      
  214.    
  215. "readOnly" : false,      
  216.    
  217. "pwd" : "a6de521abefc2fed4f5876855a3484f5"      
  218.    
  219. }      
  220.    
  221. >      
  222. 当然必须有相关权限才可以建立。  
  223.   
  224. 再登录看看:  
  225.   
  226.   
  227. [root@:/usr/local/mongodb/bin]#./mongo      
  228.    
  229. MongoDB shell version: 1.8.2      
  230.    
  231. connecting to: test      
  232.    
  233. > show collections      
  234.    
  235. Fri Jul 22 14:45:08 uncaught exception: error: {      
  236.    
  237. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",      
  238.    
  239. "code" : 10057      
  240.    
  241. }      
  242.    
  243. > db.auth('test','test')      
  244.    
  245. 1      
  246.    
  247. > show collections      
  248.    
  249. system.indexes      
  250.    
  251. system.users      
  252.    
  253. >      
原文链接:http://blog.csdn.net/wwwyuanliang10000/article/details/38661561


0 0