Skip to content

Instantly share code, notes, and snippets.

@BugKiller-tech
Last active August 24, 2018 17:04
Show Gist options
  • Select an option

  • Save BugKiller-tech/943d763580ed71c1fc6dcac300e05774 to your computer and use it in GitHub Desktop.

Select an option

Save BugKiller-tech/943d763580ed71c1fc6dcac300e05774 to your computer and use it in GitHub Desktop.

create admin user

mongo

    use admin
    db.createUser(
      {
        user: "AdminSammy",
        pwd: "AdminSammy'sSecurePassword",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      }
    )

enabling authentication

sudo nano /etc/mongod.conf

 . . .
security:
  authorization: "enabled"
 . . . 

and then restart by following command

sudo systemctl restart mongod

connect to mongo in terminal

mongo -u AdminSammy -p --authenticationDatabase admin

create user for specific database

use products
db.createUser( { user: "accountAdmin01",
                 pwd: "changeMe",
                 customData: { employeeId: 12345 },
                 roles: [ { role: "clusterAdmin", db: "admin" },
                          { role: "readAnyDatabase", db: "admin" },
                          "readWrite"] },
               { w: "majority" , wtimeout: 5000 } )

create user with role

use products
db.createUser(
   {
     user: "accountUser",
     pwd: "password",
     roles: [ "readWrite", "dbAdmin" ]
   }
)

Finally You can connect to your db like this if you are using mongoose for node.

mongoose.connect('mongodb://username:password@host:port/database?options...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment