Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. ### Problem: I installed MongoDB using the official method but now I need to secure it via username/password
  2. ####Environment: MongoDB Enterprise 3.4 on Ubuntu 16.04
  3.  
  4. **Solution** (there is more than 1 way, but this is the one I prefer):
  5.  
  6. *Step 1 - Create username/password:*
  7.  
  8. Start mongo shell
  9.  
  10. mongo
  11.  
  12. Create a new user in admin database
  13.  
  14. use admin
  15. db.createUser(
  16. {
  17. user: "myUserAdmin",
  18. pwd: "abc123",
  19. roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  20. }
  21. )
  22.  
  23. Stop the DB service
  24.  
  25. sudo service mongod stop
  26. sudo vim /etc/mongod.conf
  27.  
  28. In mongod.conf file, uncomment this line, so it looks like this:
  29.  
  30. security:
  31. authorization: 'enabled'
  32.  
  33. Start the service
  34.  
  35. sudo service mongod start
  36.  
  37. Start the shell and run a query, you should see an error:
  38.  
  39. MongoDB Enterprise > show databases
  40. ...
  41. listDatabases failed:{
  42. "ok" : 0,
  43. "errmsg" : "not authorized on admin to execute
  44. command { listDatabases: 1.0 }",
  45. ...
  46. Open the shell, this time with the correct username and password:
  47.  
  48. mongo -u myUserAdmin --authenticationDatabase=admin -p abc123
  49.  
  50. It should work this time
  51.  
  52. MongoDB Enterprise > show databases
  53. admin 0.000GB
  54. local 0.000GB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement