mongodb 权限管理,添加用户授权
非权限下启动 mongo
/usr/local/mongodb3.4.4/bin/mongod -f /usr/local/mongodb3.4.4/conf/10000.conf /usr/local/mongodb3.4.4/bin/mongo --port 10000
切换到 mongo admin 库添加管理员,默认 (db)admin (collection)users 第一个用户为超级管理员
use admin db.createUser({user:"admin",pwd:"admin",roles:["root"]})
重启 mongo
kill -HUP {mongo pid}
注意添加( -auth )
/usr/local/mongodb3.4.4/bin/mongod -f /usr/local/mongodb3.4.4/conf/10000.conf -auth /usr/local/mongodb3.4.4/bin/mongo --port 10000
管理员账号必须到 admin 库中去授权
use admin db.auth('admin','admin')
添加一个数据指定数据库的管理员
use ecommerce db.createUser({user:"cleey",pwd:"cleey",roles:["dbOwner"]}) db.auth('cleey','cleey')
或者admin 下
db.createUser({user:"cleey",pwd:"cleey",roles:[{role:"dbOwner",db:"ecommerce"}]})
admin 库下查看所有用户
需要 admin 授权 auth 可查看
> use admin switched to db admin > db.system.users.find() { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "367uQBl3wZGHM8AjIv75ew==", "storedKey" : "yU1V9nPK7xv01mNmI9I/oibmuxM=", "serverKey" : "fa5WKibh9XQYTkwMyxpFcUtJOq4=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } { "_id" : "ecommerce.cleey", "user" : "cleey", "db" : "ecommerce", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "uAI0N9hvg6Ct4CCSE6AaWQ==", "storedKey" : "gpZQ3vVbzP/+foK/Lh3/jrtYxDs=", "serverKey" : "WN89bM5oL140TQNkRHyrXQBdg1g=" } }, "roles" : [ { "role" : "dbOwner", "db" : "ecommerce" } ] }