Mongodbユーザー権限管理関連コマンド

3761 ワード

//指定db全ユーザの表示
use admin;
show users;
// 
db.getUsers();

//管理者ユーザーの作成
use admin
db.runCommand({
    createUser: "admin",
    pwd: "123456",
    roles: [ { role: "root", db: "admin" } ]
});

//一般ユーザーの作成
use mydb;
db.runCommand({
    createUser:"testuser",
    pwd:"123456",
    roles:[{role:"dbOwner",db:"mydb"}]
});

//ユーザー情報の修正
use admin;
db.runCommand({
    updateUser: "admin",
    pwd: "654321",
    roles: [ { role: "root", db: "admin" } ]
});

//指定ユーザー情報の表示
db.getUser("admin");

//指定したユーザーの削除
user mydb;
db.dropUser("testuser");

//検証が必要な方法でmongodを起動する
mongod --port 27017 --dbpath=/xxx --logpath=/xxx --auth --logappend &

//作成したユーザーでmongodbに接続する
mongo --port 27017 -u testuser -p '123456' --authenticationDatabase 'mydb'

//mongodbに接続して-u、-pを持っていない場合は、実行できます.
db.auth("testuser","123456");

Mongodb内蔵権限大全https://docs.mongodb.com/manual/core/security-built-in-roles/