Docs Menu

自己管理型配置でユーザーを認証する

To authenticate as a user, you must provide a username, password, and the authentication database associated with that user.

重要

同じmongoshセッション内でユーザーを切り替えることはできません。 別のユーザーとして認証とは、セッションが認証された両方のユーザーの特権を持つことを意味します。 ユーザーを終了して再起動するには、 mongoshを使用します。

mongoshを使用すると、次のことが可能になります。

mongosh-u <username>-p次のように、 、 、--authenticationDatabase <database> コマンドライン オプションを使用して を開始します。

mongosh --port 27017 --authenticationDatabase \
"admin" -u "myUserAdmin" -p

パスワードの入力を求められたら、入力します。

Using mongosh, connect to the mongod or mongos instance:

mongosh --port 27017

In mongosh, switch to the authentication database (in this case, admin), and use the db.auth(<username>, <pwd>) method or the authenticate command to authenticate against the authentication database:

use admin
db.auth("myUserAdmin", passwordPrompt()) // or cleartext password

Tip

passwordPrompt() メソッドではパスワードの入力が求められます。パスワードを文字列として直接指定することもできます。パスワードが画面に表示されたり、shell 履歴にパスワードが流出したりすることを避けるために、 passwordPrompt() メソッドを使用することをお勧めします。

パスワードの入力を求められたら、入力します。

For examples using a MongoDB driver, see the driver documentation.