mongodb simple auth

  • start mongod without –auth parameter, disable auth
  • connect to this instance with mongo, then create a super user
  • restart mongod with –auth
$ mongo --port 27017 -u siteUserAdmin -p password --authenticationDatabase admin 
  • re-connect to it with mongo, then create normal db user
$ mongo
> user admin
> db.auth("user", "password")
> use admin
> db.createUser(
    {
      user: "superuser",
      pwd: "12345678",
      roles: [ "root" ]
    }
  )

> use reporting
> db.createUser(
    {
      user: "reportsUser",
      pwd: "12345678",
      roles: [
         { role: "read", db: "reporting" },
         { role: "read", db: "products" },
         { role: "read", db: "sales" },
         { role: "readWrite", db: "accounts" }
      ]
    }
  )