movePrimaryIn a sharded cluster,
movePrimaryreassigns the primary shard which holds all un-sharded collections in the database.movePrimaryfirst changes the primary shard in the cluster metadata, and then migrates all un-sharded collections to the specified shard. Use the command with the following form:db.adminCommand( { movePrimary: <databaseName>, to: <newPrimaryShard> } ) たとえば、次のコマンドは、プライマリシャードを
testからshard0001に移動します。db.adminCommand( { movePrimary : "test", to : "shard0001" } ) コマンドが返すと、データベースのプライマリシャードの場所は指定されたシャードに切り替えられました。 シャードを完全に廃止するには、
removeShardコマンドを使用します。movePrimaryis an administrative command that is only available formongosinstances.警告
After starting
movePrimary, do not perform any read or write operations against any unsharded collection in that database until the command completes. Read or write operations issued against those collections during the migration can result in unexpected behavior, including potential failure of the migration operation or loss of data.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
Considerations
mongos uses "majority" write concern for movePrimary.
シャーディングされていないコレクション
Only run movePrimary after you move unsharded collections using sh.moveCollection(). Starting in MongoDB 8.0, unmoved, unsharded collections are not available during the movePrimary process.
MongoDB Search インデックス
移動されるデータベースに MongoDB Search を使用するシャーディングされていないコレクションが含まれている場合、movePrimary が完了すると、それらの検索インデックスは使用できなくなります。movePrimary は移動したシャーディングされていないコレクションの UUID を更新するため、それらのコレクション上の MongoDB Search インデックスを手動で再構築する必要があります。
メンテナンスウィンドウ
movePrimary may require a significant time to complete depending on the size of the database and factors such as network health or machine resources. During migration, attempts to write or perform any DDL operations to the unsharded collections on the database being moved fail with the error: "movePrimary is in progress".
Consider scheduling a maintenance window during which applications stop all reads and writes to the cluster. Issuing movePrimary during planned downtime mitigates the risk of encountering undefined behavior due to interleaving reads or writes to the unsharded collections in the database.
名前空間の競合
movePrimary 宛先シャードに競合するコレクション名前空間が含まれている場合、 は失敗します。 例:
管理者は、
hrデータベースのプライマリシャードを変更するためにmovePrimaryを発行します。movePrimaryがそのコレクションを移動している間に、ユーザーまたはアプリケーションがhrのシャーディングされていないコレクションに対して書込み操作を発行します。 書込み (write) 操作により、元のプライマリシャードに コレクションが作成されます。管理者は後で
movePrimaryを発行して、hrデータベースの元のプライマリ シャードを復元します。movePrimaryインターリーブ書込み (write) 操作から残った名前空間と競合するため失敗します。
インデックスの再ビルド
movePrimary操作の一部として、宛先シャードは、プライマリ シャードになった後、移行されたコレクションのインデックスを再構築する必要があります。 これには、コレクションあたりのインデックス数とインデックスを作成するデータ量によっては、かなりの時間が必要になる場合があります。
インデックス構築プロセスの詳細については、「入力済みコレクションでのインデックス構築 」を参照してください。
変更ストリーム
MongoDB8.0 以降、movePrimary は 変更ストリーム を持つイベント コレクションを無効化しません。コレクションが新しいシャードに移動された後も、変更ストリームはコレクションからイベントを引き続き読み取ります。
MongoDBの以前のバージョンでは、 movePrimaryはコレクション変更ストリームを無効にし、変更ストリームはコレクションからイベントを読み取れません。
すべてのMongoDBバージョンで、移動されたシャーディングされていないコレクションのUUIDをmovePrimaryが更新します。
詳細情報
完全な手順については、「 シャーディングされたシャードクラスタからシャードを削除する」を参照してください。