Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Pymongo ドライバー
/ /

SCRAM

Salted Challenge Response Authentication Mechanism(SCRAM)は、チャレンジ レスポンス メカニズムを使用してユーザーを認証する認証メカニズムのファミリーです。SHA-256アルゴリズムを使用してパスワードをハッシュする SCRAM-SHA-256 は、 MongoDB Serverバージョン 4.0 以降のデフォルトの認証メカニズムです。 代わりに SHA-1アルゴリズムを使用する SCRAM-SHA-1 が、4.0 より前のバージョンのMongoDB Serverのデフォルトの認証メカニズムです。

SCRAM を使用して、 MongoDB Atlas、 MongoDB Enterprise Advanced、 MongoDB Community Edition を認証できます。

Tip

SCRAM メカニズム

SCRAM ファミリーの認証メカニズムの詳細については、 Wikipedia の RFC5802 および Salted Challenge Response 認証メカニズムを参照してください。

MongoDBの SCRAM実装の詳細については、 MongoDB Serverマニュアルの「 SCRAM 」を参照してください。

このページのコード例では、次のプレースホルダーを使用します。

  • +srv: MongoDB Atlasクラスターに接続している場合にのみ、このオプションを接続文字列のプレフィックスに含めます。 +srvオプションの詳細については、 MongoDB Serverマニュアルの「 接続文字列の形式 」を参照してください。

  • <db_username>: 認証するユーザーのMongoDBユーザー名。

  • <db_password>: 認証するユーザーのMongoDBパスワード。

  • <hostname>: MongoDBデプロイのネットワーク アドレス。

  • <port>: MongoDBデプロイのポート番号。 このパラメーターを省略すると、ドライバーはデフォルトのポート番号(27017)を使用します。 MongoDB Atlasクラスターに接続する場合、ポート番号は必要ありません。

  • <authenticationDb>: ユーザーの認証データが含まれるMongoDBデータベース 。 このパラメータを省略すると、ドライバーはデフォルト値の admin を使用します。

  • <authenticationMechanism>: SCRAM-SHA-1 または SCRAM-SHA-256 に設定します。

このページのコード例を使用するには、これらのプレースホルダーを独自の値に置き換えます。

重要

パーセント エンコーディング

ユーザー名とパスワードは、 MongoDB URI に含める前に、パーセント エンコードする必要があります。quote_plus() urllib.parse モジュールで使用可能な メソッドは、このタスクを実行する 1 つの方法です 。例、 を呼び出すとquote_plus("and / or") stringand+%2F+or が返されます。

ユーザー名またはパスワードをMongoClientに引数として渡す場合は、これらをパーセント エンコードしないでください。

SCRAM を使用して認証するには、authMechanism 接続オプションを SCRAM-SHA-1 または SCRAM-SHA-256 に設定します。 このオプションは、MongoClient コンストラクターに引数を渡す方法と、接続文字列のパラメーターを使用する方法の 2 通りがあります。

client = pymongo.MongoClient("mongodb[+srv]://<hostname>:<port>",
username="<db_username>",
password="<db_password>",
authSource="<authenticationDb>",
authMechanism="<authenticationMechanism>")
uri = ("mongodb[+srv]://<percent-encoded db_username>:<percent-encoded db_password>"
"@<hostname>:<port>/?"
"authSource=<authenticationDb>"
"&authMechanism=<authenticationMechanism>")
client = pymongo.MongoClient(uri)
client = pymongo.AsyncMongoClient("mongodb[+srv]://<hostname>:<port>",
username="<db_username>",
password="<db_password>",
authSource="<authenticationDb>",
authMechanism="<authenticationMechanism>")
uri = ("mongodb[+srv]://<percent-encoded db_username>:<percent-encoded db_password>"
"@<hostname>:<port>/?"
"authSource=<authenticationDb>"
"&authMechanism=<authenticationMechanism>")
client = pymongo.AsyncMongoClient(uri)

PyMongo でアプリケーションを認証するの詳細については、次の API ドキュメントを参照してください。

戻る

認証

項目一覧