LoginSignup
156

More than 5 years have passed since last update.

MongoDBコマンド集(ユーザ操作)

Last updated at Posted at 2014-08-21

ユーザ一覧

db.system.users.find()

ユーザ作成

1. 認証モードをOFFにしておく

/etc/mongod.conf

noauth=true
# auth=true

2. ユーザを作成する

ユーザはデータベースごとに作成する。adminデータベースで作成されたユーザは管理者となる。

管理者ユーザの作成

$ mongo
> use admin

# すべてのデータベースの管理者権限をもつユーザの作成
> db.createUser(
  {
    user: "ユーザ名",
    pwd: "パスワード",
    roles:
    [
      {
        role: "userAdminAnyDatabase",
        db: "admin"
      }
    ]
  }
)

# 特定のデータベースのみ管理者権限をもつユーザの作成
> use データベース名
> db.createUser(
  {
    user: "ユーザ名",
    pwd: "パスワード",
    roles:
    [
      {
        role: "userAdmin",
        db: "データベース名"
      }
    ]
  }
)

# アクセス制限が全くないユーザの作成
> use admin
db.createUser(
    {
      user: "ユーザ名",
      pwd: "パスワード",
      roles: [ "root" ]
    }
)

一般ユーザの作成

$ mongo
> use データベース名
db.createUser(
    {
      user: "ユーザ名",
      pwd: "パスワード",
      roles: [
        { 
          role: "read", # or "readWrite"
          db: "データベース名" 
        }
      ]
    }
)

3. 認証の確認

> db.auth("ユーザ名","パスワード")
> 1 # 成功

mongoシェル起動時に認証する場合は、

$ mongo データベース名 -u ユーザ名 -p パスワード

4. 認証モードをONに戻してMonboDBを再起動

/etc/mongod.conf

# noauth=true
auth=true
$ sudo service mongod restart

ユーザの削除

> use データベース名
> db.system.users.remove({user:"username"})

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
156