Skip to content

Instantly share code, notes, and snippets.

@ywwwtseng
Last active August 22, 2017 02:51
Show Gist options
  • Save ywwwtseng/bce54c3eec965ad51e5ee9b44137c83a to your computer and use it in GitHub Desktop.
Save ywwwtseng/bce54c3eec965ad51e5ee9b44137c83a to your computer and use it in GitHub Desktop.
Mongodb Security

Mongodb Security

  1. Start MongoDB without access control.

    $ systemctl start mongod
    
  2. Connect to the instance.

    $ mongo
    
  3. Create the user administrator.

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

    Disconnect the mongo shell.

  4. Open Mongodb with auth support .

    Edit /etc/mongod.conf

    security:
      authorization: "enabled"
    

    Restart Mongodb

    $ systemctl restart mongod
    
  5. Create additional users as needed for your deployment.

    $ mongo -u admin -p password --authenticationDatabase admin
    
    use test
    
    db.createUser(
      {
        user: "myTester",
        pwd: "password",
        roles:
        [
          {
            role: "readWrite",
            db: "test"
          }
        ]
      }
    )
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment