AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

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 Authentication Mechanism を参照してください。

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

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

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

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

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

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

DEFAULT 認証メカニズムは、サーバーによってサポートされている最初の認証メカニズムを次の優先順位でネゴシエートするようにドライバーに指示するフォールバック設定です。

  1. SCRAM-SHA-256

  2. SCRAM-SHA-1

  3. MONGODB-CR

DEFAULT オプションが指定されている場合、ドライバーは最初に SCRAM-SHA-256 を使用して認証を試みます。MongoDB インスタンスのバージョンがそのメカニズムをサポートしていない場合、ドライバーは SCRAM-SHA-1 を使用して認証を試みます。インスタンスがそのメカニズムもサポートしていない場合、ドライバーは MONGODB-CR を使用して認証を試みます。

デフォルトの認証メカニズムを指定するには、接続文字列で authMechanism パラメータを DEFAULT に設定するか、デフォルト値であるためパラメーターを省略します。

次の例では、接続文字列で authMechanism を DEFAULT に設定し、認証メカニズムをデフォルトに設定する方法を示しています。

重要

ユーザー名とパスワードが正しく解析されるようにするには、必ず encodeURIComponent メソッドを使用して URI エンコード してください。

const { MongoClient } = require("mongodb"); // Replace the following with values for your environment.const username = encodeURIComponent("<db_username>");const password = encodeURIComponent("<db_password>");const clusterUrl = "<cluster_url>"; const authMechanism = "DEFAULT"; // Replace the following with your MongoDB deployment's connection string.const uri = `mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`; // Create a new MongoClientconst client = new MongoClient(uri); // Function to connect to the serverasync function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); }}run().catch(console.dir);

MongoDB がサポートする SCRAM バージョンの詳細については、 MongoDB Serverマニュアルの 「SCRAM」セクション を参照してください。

注意

SCRAM-SHA-256 MongoDB バージョン 4.0 以降のデフォルトの認証方法です。

SCRAM-SHA-256 は、SHA-256アルゴリズムで暗号化されたユーザー名とパスワードを使用してユーザーを認証する SCRAM バージョンです。

この認証メカニズムを指定するには、次のサンプル コードに示すように、接続stringで authMechanism を値 SCRAM-SHA-256 に設定します。

重要

ユーザー名とパスワードが正しく解析されるようにするには、必ず encodeURIComponent メソッドを使用して URI エンコード してください。

const { MongoClient } = require("mongodb"); // Replace the following with values for your environment.const username = encodeURIComponent("<db_username>");const password = encodeURIComponent("<db_password>");const clusterUrl = "<cluster_url>"; const authMechanism = "SCRAM-SHA-256"; // Replace the following with your MongoDB deployment's connection string.const uri = `mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`; // Create a new MongoClientconst client = new MongoClient(uri); // Function to connect to the serverasync function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); }}run().catch(console.dir);

注意

SCRAM-SHA-1 は、MongoDB バージョン 3.0、3.2、3.4、および 3.6 のデフォルトの認証方法です。

SCRAM-SHA-1 は、SHA-1アルゴリズムで暗号化されたユーザー名とパスワードを使用してユーザーを認証する SCRAM バージョンです。

この認証メカニズムを指定するには、次のサンプル コードに示すように、接続文字列で authMechanism パラメーターを値 SCRAM-SHA-1 に設定します。

重要

ユーザー名とパスワードが正しく解析されるようにするには、必ず encodeURIComponent メソッドを使用して URI エンコード してください。

const { MongoClient } = require("mongodb"); // Replace the following with values for your environment.const username = encodeURIComponent("<db_username>");const password = encodeURIComponent("<db_password>");const clusterUrl = "<cluster_url>"; const authMechanism = "SCRAM-SHA-1"; // Replace the following with your MongoDB deployment's connection string.const uri = `mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`; // Create a new MongoClientconst client = new MongoClient(uri); // Function to connect to the serverasync function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); }}run().catch(console.dir);

このページで説明するメソッドやタイプの詳細については、次のAPIドキュメントを参照してください。