Guest User

Untitled

a guest
Jan 16th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. ## Hosting a MongoDB and creating accounts for application in it
  2.  
  3. 1. Docker Compose is only for creating `root` account in db `admin`, this is not the database for application
  4. 2. Create Application specific accounts and set the authentication details
  5.  
  6. 2.1 Login to MongoDB using root credentials and use below syntax to create application specific account and the database
  7.  
  8. 2.2 Application should use its own DB and Credentials
  9.  
  10. ### Syntax for creating an account for application
  11.  
  12. > PS: You have to be in the database for which you are creating the credentials
  13.  
  14. ```shell
  15. use <application db name>
  16. db.createUser({user: "<application db username>", pwd: "<application db pwd>", roles: ["readWrite", "dbAdmin"]});
  17. ```
  18.  
  19. Eg:
  20.  
  21. ```shell
  22. use sampleapp
  23. db.createUser({user: "myuser", pwd: "mypwd", roles: ["readWrite", "dbAdmin"]});
  24. ```
  25.  
  26. You can use docker compose like below to host a DB
  27.  
  28. ```yaml
  29. version: '3.1'
  30.  
  31. services:
  32.  
  33. mongo:
  34. image: mongo:latest
  35. restart: always
  36. ports:
  37. - 27017:27017
  38. environment:
  39. MONGO_INITDB_ROOT_USERNAME: root
  40. MONGO_INITDB_ROOT_PASSWORD: example
  41. ```
  42.  
  43. You can use this command to login to admin account, which will prompt for password
  44.  
  45. ```shell
  46. mongo --host <whatever is the hostname> -u root --password --authenticationDatabase admin
  47. ```
Add Comment
Please, Sign In to add comment