Docs Menu

キーファイル認証を使用した自己管理型シャーディングされたクラスターの配置

Enforcing access control on a シャーディングされたクラスター requires configuring:

For this tutorial, each member of the sharded cluster must use the same internal authentication mechanism and settings. This means enforcing internal authentication on each mongos and mongod in the cluster.

The following tutorial uses a keyfile to enable internal authentication.

Enforcing internal authentication also enforces user access control. To connect to the replica set, clients like mongosh need to use a user account. See アクセス制御.

Cloud ManagerまたはMongoDB Ops Managerを使用して配置を管理している場合は、それぞれのCloud ManagerマニュアルまたはMongoDB Ops Managerマニュアルを参照して、認証を強制します。

重要

IP アドレスの変更による構成の更新を防ぐには、IP アドレスの代わりに DNS ホスト名を使用します。レプリカセット ノードまたはシャーディングされたクラスター ノードを設定するときは、IP アドレスではなく DNS ホスト名を使用することが特に重要です。

分裂されたネットワーク ホライズン全体でクラスターを構成するには、IP アドレスの代わりにホスト名を使用します。 MongoDB 5.0以降、IP アドレスのみが設定されているノードは起動時の検証に失敗し、起動しません。

MongoDB バイナリ(mongodmongos)は、デフォルトで localhost にバインドされます。

キーファイルは最低限のセキュリティであり、テストや開発環境に最適です。本番環境では、X.509 証明書を使用することをお勧めします。

このチュートリアルでは、adminデータベース上のみでの最小限数の管理ユーザーの作成について説明します。ユーザー認証には、このチュートリアルではデフォルトのSCRAM認証メカニズムを使用します。チャレンジレスポンスのセキュリティ方式は、テスト環境または開発環境に最適です。本番環境では、 X.509 証明書 または 自己管理型 LDAP プロキシ認証 ( MongoDB Enterpriseでのみ使用可能)、または 自己管理型配置での Kerberos 認証 ( MongoDB Enterpriseでのみ使用可能)を使用することをお勧めします。

特定の認証メカニズムのユーザーを作成する方法の詳細については、特定の認証メカニズムのページを参照してください。

ユーザーの作成とマネジメントのベストプラクティスについては、➤ ロールベースのアクセス制御の構成を参照してください。

In general, to create users for a sharded clusters, connect to the mongos and add the sharded cluster users.

However, some maintenance operations require direct connections to specific shards in a sharded cluster. To perform these operations, you must connect directly to the shard and authenticate as a shard-local administrative user.

Shard-local users exist only in the specific shard and should only be used for shard-specific maintenance and configuration. You cannot connect to the mongos with shard-local users.

This tutorial requires creating sharded cluster users, but includes optional steps for adding shard-local users.

See the 自己管理型配置のユーザー security documentation for more information.

This tutorial uses the mongod and mongos programs. Windows users should use the mongod.exe and mongos.exe programs instead.

MongoDB 8.0以降では、 directShardOperationsロールを使用して、シャードに対してコマンドを直接実行する必要があるメンテナンス操作を実行できます。

警告

directShardOperationsロールを使用して コマンドを実行すると、クラスターが正しく動作しなくなり、データが破損する可能性があります。 directShardOperationsロールは、メンテナンス目的で、または MongoDB サポートのガイダンスに必ず従う必要があります。 メンテナンス操作を実行したら、 directShardOperationsロールの使用を停止します。

The following procedures involve creating a new sharded cluster that consists of a mongos, the config servers, and two shards.

重要

IP アドレスの変更による構成の更新を防ぐには、IP アドレスの代わりに DNS ホスト名を使用します。レプリカセット ノードまたはシャーディングされたクラスター ノードを設定するときは、IP アドレスではなく DNS ホスト名を使用することが特に重要です。

分裂されたネットワーク ホライズン全体でクラスターを構成するには、IP アドレスの代わりにホスト名を使用します。 MongoDB 5.0以降、IP アドレスのみが設定されているノードは起動時の検証に失敗し、起動しません。

With keyfile authentication, each mongod or mongos instances in the sharded cluster uses the contents of the keyfile as the shared password for authenticating other members in the deployment. Only mongod or mongos instances with the correct keyfile can join the sharded cluster.

注意

内部メンバーシップ認証用のキーファイルでは、キーファイル内に複数のキーを含めるために YAML 形式が使用されます。YAML 形式は次のいずれかを受け入れます。

  • 1 つのキー文字列(以前のバージョンと同じ)

  • キー文字列のシーケンス

YAML 形式は、テキストファイル形式を使用する既存の単一のキー キーファイルと互換性があります。

A key's length must be between 6 and 1024 characters and may only contain characters in the base64 set. All members of the sharded cluster must share at least one common key.

注意

UNIX システムでは、キーファイルにグループ権限またはワールド権限があってはなりません。Windows システムでは、キーファイルの権限はチェックされません。

キーファイルは、選択した任意の方法で生成できます。たとえば、次の操作では、openssl を使用して、共有パスワードとして使用する疑似ランダムの複雑な 1024 文字の文字列を生成します。次に、chmod を使用してファイル権限を変更し、ファイル所有者のみに読み取り権限を付与します。

openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>

キーファイルの使用に関する詳細と要件については、「キーファイル」を参照してください。

Copy the keyfile to each server hosting the sharded cluster members. Ensure that the user running the mongod or mongos instances is the owner of the file and can access the keyfile.

Avoid storing the keyfile on storage mediums that can be easily disconnected from the hardware hosting the mongod or mongos instances, such as a USB drive or a network attached storage device.

以下の手順でコンフィギュレーションサーバーのレプリカセットをデプロイします。

For a production deployment, deploys a config server replica set with at least three members. For testing purposes, you can create a single-member replica set.

1

始める each mongod in the config server replica set. Include the keyFile setting. The keyFile setting enforces both 自己管理型内部認証とメンバーシップ認証 and 自己管理型配置におけるロールベースのアクセス制御.

You can specify the mongod settings either via a configuration file or the command line.

構成ファイル

構成ファイルを使用する場合、次を設定します security.keyFile to the keyfile's path, sharding.clusterRole to configsvr, and replication.replSetName to the desired name of the config server replica set.

security:
keyFile: <path-to-keyfile>
sharding:
clusterRole: configsvr
replication:
replSetName: <setname>

構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを使用して配置に接続する場合や、配置ノードを異なるホスト上で実行する場合は、 net.bindIp設定を指定します。

Start the mongod specifying the --config option and the path to the configuration file.

mongod --config <path-to-config-file>

コマンドライン

If using the command line parameters, start the mongod with the --keyFile, --configsvr, and --replSet parameters.

mongod --keyFile <path-to-keyfile> --configsvr --replSet <setname> --dbpath <path>

構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを配置に接続する場合、または配置ノードを異なるホスト上で実行する場合は、 --bind_ipを指定します。

2

mongoshローカルホスト インターフェース経由で mongod インスタンスの 1 つに接続します。mongoshmongod インスタンスと同じ物理マシン上で実行する必要があります。

配置用のユーザーが作成されていないため、ローカルホスト インターフェースのみ使用できます。最初のユーザーが作成された後に、 ローカルホスト インターフェースは閉じます。

3

The rs.initiate() method initiates the replica set and can take an optional replica set configuration document. In the replica set configuration document, include:

  • The _id. The _id must match the --replSet parameter passed to the mongod.

  • The members field. The members field is an array and requires a document per each member of the replica set.

  • The configsvr field. The configsvr field must be set to true for the config server replica set.

レプリカセットの構成ドキュメントについて詳しくは、「自己管理型レプリカセット構成」を参照してください。

Initiate the replica set using the rs.initiate() method and a configuration document:

rs.initiate(
{
_id: "myReplSet",
configsvr: true,
members: [
{ _id : 0, host : "cfg1.example.net:27019" },
{ _id : 1, host : "cfg2.example.net:27019" },
{ _id : 2, host : "cfg3.example.net:27019" }
]
}
)

コンフィギュレーションサーバーのレプリカセット(CSRS)を初期化し、起動したら、シャード レプリカセットの作成に進みます。

本番環境には、少なくとも 3 つのノードを含むレプリカセットをデプロイします。テスト用に、シングルノードのレプリカセットを作成できます。

These steps include optional procedures for adding shard-local users. Executing them now ensures that there are users available for each shard to perform shard-level maintenance.

1

Running a mongod with the keyFile parameter enforces both 自己管理型内部認証とメンバーシップ認証 and 自己管理型配置におけるロールベースのアクセス制御.

始める each mongod in the replica set using either a configuration file or the command line.

構成ファイル

If using a configuration file, set the security.keyFile option to the keyfile's path, the replication.replSetName to the desired name of the replica set, and the sharding.clusterRole option to shardsvr.

security:
keyFile: <path-to-keyfile>
sharding:
clusterRole: shardsvr
replication:
replSetName: <replSetName>
storage:
dbPath: <path>

構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを使用して配置に接続する場合や、配置ノードを異なるホスト上で実行する場合は、 net.bindIp設定を指定します。

Start the mongod specifying the --config option and the path to the configuration file.

mongod --config <path-to-config-file>

コマンドライン

If using the command line option, when starting the component, specify the --keyFile, replSet, and --shardsvr parameters, as in the following example:

mongod --keyFile <path-to-keyfile> --shardsvr --replSet <replSetName> --dbpath <path>

構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを配置に接続する場合、または配置ノードを異なるホスト上で実行する場合は、 --bind_ipを指定します。

2

mongoshローカルホスト インターフェース経由で mongod インスタンスの 1 つに接続します。mongoshmongod インスタンスと同じ物理マシン上で実行する必要があります。

配置用のユーザーが作成されていないため、ローカルホスト インターフェースのみ使用できます。最初のユーザーが作成された後に、 ローカルホスト インターフェースは閉じます。

3

mongoshから、 rs.initiate()メソッドを実行します。

rs.initiate() は、オプションのレプリカセット構成ドキュメントを指定できます。レプリカセット構成ドキュメントには、次を含めます。

  • _id は、replication.replSetName または --replSet オプションのいずれかで指定されたレプリカセット名に設定されます。

  • レプリカセットの各ノードごとのドキュメントを含む members 配列

次の例では、3 つのノードがあるレプリカセットを初期化します。

rs.initiate(
{
_id : "myReplSet",
members: [
{ _id : 0, host : "s1-mongo1.example.net:27018" },
{ _id : 1, host : "s1-mongo2.example.net:27018" },
{ _id : 2, host : "s1-mongo3.example.net:27018" }
]
}
)

rs.initiate()は、選挙をトリガーし、ノードの 1 つをプライマリとして選出します。

続行する前にプライマリに接続します。プライマリ ノードを見つけるには、rs.status() を使用します。

4

重要

最初のユーザーを作成すると、localhost 例外は利用できなくなります。

最初のユーザーには、 userAdminAnyDatabaseを持つユーザーなど、他のユーザーを作成する特権を付与する必要があります。 これにより、自己管理型配置の Localhost 例外 が終了した後に追加のユーザーを作成できるようになります。

少なくとも 1 人ユーザーを作成できる権限を持つユーザーがいない場合、localhost 例外が終了すると、権限を持ったユーザーを新規作成、または既存ユーザーの権限を編集できなくなり、必要な操作にアクセスできなくなる可能性があります。

db.createUser() メソッドを使用してユーザーを追加します。このユーザーには、admin データベース上で userAdminAnyDatabase 以上のロールを付与する必要があります。

ユーザーを作成するには、プライマリに接続する必要があります。

次の例では、ユーザー fred を作成し、admin データベースに対して userAdminAnyDatabase ロールを付与しています。

重要

パスワードは、システムのセキュリティを確保し、悪意のあるアクセスを防止または遅延させるために、ランダムで長く、複雑なものにする必要があります。

Tip

passwordPrompt() メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo シェルと同様に、パスワードを直接指定することもできます。

admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "fred",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

パスワードの入力を求められたら、入力します。 データベース管理操作に関連する組み込みロールの完全なリストについては、「データベースユーザー ロール」を参照してください。

5

admin データベースで認証を行います。

mongoshでは、 db.auth()を使用して認証します。 たとえば、次の例では、ユーザー管理者fredとして認証します。

Tip

passwordPrompt() メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo シェルと同様に、パスワードを直接指定することもできます。

db.getSiblingDB("admin").auth("fred", passwordPrompt()) // or cleartext password

または、-u <username>-p <password>--authenticationDatabase パラメータを使用して、新しい mongosh インスタンスをプライマリ レプリカセット ノードに接続します。

mongosh -u "fred" -p --authenticationDatabase "admin"

-pコマンドライン オプションにパスワードを指定しない場合、 mongoshはパスワードの入力を要求します。

6

The shard-local cluster administrator user has the clusterAdmin role, which provides privileges that allow access to replication operations.

For a full list of roles related to replica set operations see クラスター管理ロール.

クラスター管理者ユーザーを作成し、admin データベースでの clusterAdmin ロールを割り当てます。

Tip

passwordPrompt() メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo シェルと同様に、パスワードを直接指定することもできます。

db.getSiblingDB("admin").createUser(
{
"user" : "ravi",
"pwd" : passwordPrompt(), // or cleartext password
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
}
)

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

レプリカセットとシャーディングされたクラスターの操作に関連する組み込みロールの完全なリストについては、「クラスター管理ロール」を参照してください。

1

Start a mongos specifying the keyfile using either a configuration file or a command line parameter.

構成ファイル

If using a 構成ファイル, set the security.keyFile to the keyfile's path and the sharding.configDB to the replica set name and at least one member of the replica set in <replSetName>/<host:port> format.

security:
keyFile: <path-to-keyfile>
sharding:
configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...

構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを使用して配置に接続する場合や、配置ノードを異なるホスト上で実行する場合は、 net.bindIp設定を指定します。

Start the mongos specifying the --config option and the path to the configuration file.

mongos --config <path-to-config>

コマンドライン

If using command line parameters start the mongos and specify the --keyFile and --configdb parameters.

mongos --keyFile <path-to-keyfile> --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,...

構成に必要な追加オプションを含めます。 たとえば、リモート クライアントを配置に接続する場合、または配置ノードを異なるホスト上で実行する場合は、 --bind_ipを指定します。

2

mongoshローカルホスト インターフェース経由で mongos インスタンスの 1 つに接続します。mongoshmongos インスタンスと同じ物理マシン上で実行する必要があります。

配置用のユーザーが作成されていないため、ローカルホスト インターフェースのみ使用できます。最初のユーザーが作成された後に、 ローカルホスト インターフェースは閉じます。

3

重要

最初のユーザーを作成すると、localhost 例外は利用できなくなります。

最初のユーザーには、 userAdminAnyDatabaseを持つユーザーなど、他のユーザーを作成する特権を付与する必要があります。 これにより、自己管理型配置の Localhost 例外 が終了した後に追加のユーザーを作成できるようになります。

If at least one user does ではない have privileges to create users, once the localhost exception closes you cannot create or modify users, and therefore may be unable to perform necessary operations.

db.createUser() メソッドを使用してユーザーを追加します。このユーザーには、admin データベース上で userAdminAnyDatabase 以上のロールを付与する必要があります。

重要

パスワードは、システムのセキュリティを確保し、悪意のあるアクセスを防止または遅延させるために、ランダムで長く、複雑なものにする必要があります。

The following example creates the user fred on the admin database:

Tip

passwordPrompt() メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo シェルと同様に、パスワードを直接指定することもできます。

admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "fred",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

データベース管理操作に関連する組み込みロールの全リストは、「データベースユーザー ロール」を参照してください。

4

使用 db.auth() to authenticate as the user administrator to create additional users:

Tip

passwordPrompt() メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo シェルと同様に、パスワードを直接指定することもできます。

db.getSiblingDB("admin").auth("fred", passwordPrompt()) // or cleartext password

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

Alternatively, connect a new mongosh session to the target replica set member using the -u <username>, -p <password>, and the --authenticationDatabase "admin" parameters. You must use the 自己管理型配置における Localhost 例外 to connect to the mongos.

mongosh -u "fred" -p --authenticationDatabase "admin"

-pコマンドライン オプションにパスワードを指定しない場合、 mongoshはパスワードの入力を要求します。

5

The cluster administrator user has the clusterAdmin role, which grants access to replication and sharding operations.

Create a clusterAdmin user in the admin database.

The following example creates the user ravi on the admin database.

重要

パスワードは、システムのセキュリティを確保し、悪意のあるアクセスを防止または遅延させるために、ランダムで長く、複雑なものにする必要があります。

Tip

passwordPrompt() メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo シェルと同様に、パスワードを直接指定することもできます。

db.getSiblingDB("admin").createUser(
{
"user" : "ravi",
"pwd" : passwordPrompt(), // or cleartext password
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
}
)

レプリカセットとシャーディングされたクラスターの操作に関連する組み込みロールの完全なリストについては、「クラスター管理ロール」を参照してください。

6

Create users to allow clients to connect and access the sharded cluster. See データベースユーザー ロール for available built-in roles, such as read and readWrite. You may also want additional administrative users. For more information on users, see 自己管理型配置のユーザー.

To create additional users, you must authenticate as a user with userAdminAnyDatabase or userAdmin roles.

To proceed, you must be connected to the mongos and authenticated as the cluster administrator user for the sharded cluster.

注意

This is the cluster administrator for the sharded cluster and ではない the shard-local cluster administrator.

To add each shard to the cluster, use the sh.addShard() method. If the shard is a replica set, specify the name of the replica set and specify a member of the set. In production deployments, all shards should be replica sets.

次の操作では、単一のシャード レプリカセットをクラスターに追加します。

sh.addShard( "<replSetName>/s1-mongo1.example.net:27017")

The following operation is an example of adding a standalone mongod shard to the cluster:

sh.addShard( "s1-mongo1.example.net:27017")

Repeat these steps until the cluster includes all shards. At this point, the sharded cluster enforces access control for the cluster as well as for internal communications between each sharded cluster component.

To proceed, you must be connected to the mongos and authenticated as the cluster administrator user for the sharded cluster.

注意

This is the cluster administrator for the sharded cluster and ではない the shard-local cluster administrator.

To shard a collection, use the sh.shardCollection() method. You must specify the full namespace of the collection and a document containing the shard key.

Your selection of shard key affects the efficiency of sharding, as well as your ability to take advantage of certain sharding features such as zones. See the selection considerations listed in the シャードキーの選択.

If the collection already contains data, you must create an index on the シャードキー using the db.collection.createIndex() method before using shardCollection().

If the collection is empty, MongoDB creates the index as part of sh.shardCollection().

The following is an example of the sh.shardCollection() method:

sh.shardCollection("<database>.<collection>", { <key> : <direction> } )

Create users to allow clients to connect to and interact with the sharded cluster.

See データベースユーザー ロール for basic built-in roles to use in creating read-only and read-write users.

X.509 を使用した内部認証の詳細については、「自己管理型MongoDBによるメンバーシップ認証に X.509 証明書を使用」を参照してください。

鍵ファイルによる内部認証から X.509 内部認証にアップグレードするには、「 自己管理型MongoDB を鍵ファイル認証から X. 認証にアップグレードする509 」を参照してください。

以下も参照してください。