Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. Users & Roles
  2. ================
  3.  
  4. There are a number of predefined roles:
  5.  
  6. root - All powerful. Use with caution
  7. userAdminAnyDatabase - Can create users and assign roles on any database. Use with caution
  8. userAdmin - Can only create users and assign roles in a specific database
  9. read - Read collections in a specific database.
  10. readWrite - Read and Write to a specific database
  11.  
  12.  
  13. > db.getUsers()
  14. or
  15. > db.system.users.find()
  16.  
  17. STEP - 1
  18. ========
  19.  
  20. Add Users Before Enabling Access Control
  21.  
  22. Create Admin User :: -
  23.  
  24. The first thing is to create an admin user, go to the mongo shell
  25. connect to the `admin' database
  26.  
  27. The first user should be an admin user that can manage the database.
  28.  
  29. create a user and assign him the role userAdminAnyDatabase
  30.  
  31. use admin
  32.  
  33. var user = {
  34. "user" : "root",
  35. "pwd" : "toor",
  36. roles : [
  37. {
  38. "role" : "userAdminAnyDatabase",
  39. "db" : "admin"
  40. }
  41. ]
  42. }
  43.  
  44. db.createUser(user);
  45.  
  46. How to check user created or not ?
  47. -----------------------------------
  48.  
  49. db.getUsers()
  50. [
  51. {
  52. "_id" : "admin.root",
  53. "user" : "root",
  54. "db" : "admin",
  55. "roles" : [
  56. {
  57. "role" : "userAdminAnyDatabase",
  58. "db" : "admin"
  59. }
  60. ]
  61. }
  62. ]
  63.  
  64.  
  65. STEP - 2
  66. ========
  67. Enabling Access Control ::
  68.  
  69. in /etc/mongod.conf
  70.  
  71. security:
  72. authorization: enabled
  73.  
  74. after updating config file we need to restart the mongo instance.
  75.  
  76. STEP - 3
  77. ========
  78. Here after we can use user name and pass for access database.
  79.  
  80. If you enter with out user and pass, you will see these kind erros,
  81.  
  82. > show databases;
  83. 2016-06-05T08:05:22.960+0530 E QUERY [thread1] Error: listDatabases failed:{
  84. "ok" : 0,
  85. "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
  86. "code" : 13
  87. } :
  88. _getErrorWithCode@src/mongo/shell/utils.js:25:13
  89. Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
  90. shellHelper.show@src/mongo/shell/utils.js:760:19
  91. shellHelper@src/mongo/shell/utils.js:650:15
  92. @(shellhelp2):1:1
  93. >
  94.  
  95. $mongo admin -u root -p
  96. MongoDB shell version: 3.2.5
  97. Enter password:
  98. connecting to: admin
  99. >
  100.  
  101. STEP - 4
  102. ========
  103.  
  104. let's create application User for read/Write
  105.  
  106. Before we need to create application user, we need to go the perticular database
  107.  
  108. > use hermes;
  109.  
  110. var user = {
  111. "user" : "appuser",
  112. "pwd" : "app123",
  113. roles : [
  114. {
  115. "role" : "readWrite",
  116. "db" : "hermes"
  117. }
  118. ]
  119. }
  120.  
  121. db.createUser(user);
  122.  
  123. let's verify
  124.  
  125. > db.getUsers()
  126. [
  127. {
  128. "_id" : "hermes.appuser",
  129. "user" : "appuser",
  130. "db" : "hermes",
  131. "roles" : [
  132. {
  133. "role" : "readWrite",
  134. "db" : "hermes"
  135. }
  136. ]
  137. }
  138. ]
  139. >
  140.  
  141. STEP - 5
  142. ========
  143.  
  144. let's create readonly user to read any database
  145.  
  146. $mongo admin -u admin -p
  147.  
  148. var user = {
  149. "user" : "reporting",
  150. "pwd" : "abc123",
  151. roles : [
  152. {
  153. "role" : "readAnyDatabase",
  154. "db" : "admin"
  155.  
  156. }
  157. ]
  158. }
  159.  
  160. db.createUser(user);
  161. exit
  162.  
  163. > db.products.insert({ "title" : "MongoDB in Action" });
  164. WriteResult({
  165. "writeError" : {
  166. "code" : 13,
  167. "errmsg" : "not authorized on hermes to execute command { insert: \"products\", documents: [ { _id: ObjectId('5753d9af680d6e283c83138f'), title: \"MongoDB in Action\" } ], ordered: true }"
  168. }
  169. })
  170. >
  171.  
  172. If you try to insert/update/delete document you will receive an exception.
  173.  
  174. How to update the user role:
  175. =============================
  176. use admin
  177.  
  178. db.updateUser( "admin",
  179. {
  180.  
  181. roles : [
  182. { role : "root", db : "admin" }
  183. ]
  184. }
  185. )
  186.  
  187.  
  188. Enforce-keyfile-access-control
  189. ===================================
  190. cd /var/lib/mongo
  191. openssl rand -base64 755 > dv_mongo.key
  192. chmod 400 dv_mongo.key
  193. chown mongod: dv_mongo.key
  194.  
  195. security:
  196. authorization: enabled
  197. keyFile: /var/lib/mongo/dv_mongo.key
  198.  
  199. NOTE :: dv_mongo.key file copy to all slave machine.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement