mongodb增加auth

mongo

db.createCollection('demodb')

create super user

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

create app user

mongo --port 27017 -u "user" -p "password" --authenticationDatabase "admin"
use demodb
db.createUser(
  {
    user: "demo",
    pwd: "demopwd",
    roles: [ { role: "readWrite", db: "demodb" } ]
  }
)
exit
mongo -u demo -p demopwd --authenticationDatabase demodb
use demodb
db.getCollection('yourcollection').find()

开启验证

/usr/bin/mongod --auth -f /etc/mongod.conf

docs

  • manage-users-and-roles

你可能感兴趣的:(mongodb)