> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB の更新

<Info>
  Server CE/Server Pro の新しいリリースでは、サポートされる MongoDB のバージョンに変更がある場合、その[リリースノート](https://docs.overleaf.com/on-premises/release-notes)に記載されます。
</Info>

### MongoDB を更新すべきか？

MongoDB のバージョンの更新を検討すべきなのは、Server CE/Server Pro のインスタンスをアップグレードする予定がある場合**のみ**です。

現在の（または移行先の）バージョンで推奨されているものより新しいバージョンの MongoDB を実行している場合は、何も変更する必要はありません。

<Warning>
  MongoDB のバージョンを決してダウングレードしないでください。
</Warning>

現在のバージョンの MongoDB に関係していると思われる特定の問題が発生した場合は、Server CE ユーザーであれば気軽に [issue を作成](https://github.com/overleaf/overleaf/issues)してください。Server Pro ユーザーであれば Overleaf サポートにお問い合わせください。

### MongoDB のバージョンを確認する

`mongo` シェルを開くと、すぐに現在のバージョンが表示されます。

Overleaf Toolkit ユーザーの場合：

```bash theme={null}
bin/docker-compose exec mongo mongod --version
db version v5.0.24
Build Info: {
    "version": "5.0.24",
    "gitVersion": "f034f0c51b3dffef4b8c9452d77ede9888f28f66",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}
```

Docker Compose ユーザーの場合：

```bash theme={null}
docker compose exec mongo mongod --version
db version v5.0.24
Build Info: {
    "version": "5.0.24",
    "gitVersion": "f034f0c51b3dffef4b8c9452d77ede9888f28f66",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

```

### 更新手順

Server CE/Server Pro インスタンスのアップグレード時に MongoDB のバージョンを更新する手順は次のとおりです：

1. アップグレード先の Server CE/Server Pro のバージョンを決定します。
2. その Overleaf Server CE/Server Pro リリースで推奨されている MongoDB のバージョンを確認します。
3. 手順に従って、MongoDB を目的のバージョンにアップグレードします。
4. Server CE/Server Pro のイメージのバージョンをアップグレードし、インスタンスを再起動します。

**Server CE/Server Pro** は常に利用可能な最新バージョンにアップグレードすることをお勧めします。最新バージョンは常にサポートが保証されているためです（Server Pro ユーザーのみ）。

<Danger>
  Server CE/Pro をアップグレードする際は、**次の**メジャーバージョンの最新リリースにアップグレードする**前に**、現在導入しているメジャーバージョンの最新リリースにアップグレードすることをお勧めします。導入環境が最新版から 2 つ以上メジャーバージョンが遅れている場合は、複数段階のアップグレードを行う必要があります。

  たとえば、3.5.10 を実行している場合は、3.5.13 にアップグレード -> 完全なプロジェクト履歴の移行を実行 -> 4.2.9 -> 5.5.4 の順にアップグレードする必要があります。

  メジャーバージョンを飛ばすこと（3.5.10 -> 5.5.4）は**決して**しないでください。Toolkit を使用していて、最新版から 2 つ以上メジャーバージョンが遅れている場合は、手動で複数段階のアップグレードを行う必要があるため、`bin/upgrade` スクリプトを使用**しないでください**。
</Danger>

<Warning>
  必要に応じてロールバックできるように、メジャーバージョンのアップグレードの**前に**毎回[整合性のとれたバックアップ](/ja/on-premises/maintenance/data-and-backups#performing-a-consistent-backup)を取得しておくことが重要です。
</Warning>

#### バージョンのサポート情報

以前のバージョンを使用する場合に備えて、この表では Server CE/Server Pro の以前のリリースで推奨される MongoDB のバージョンを示しています。ただし、MongoDB のバージョンを**決して**ダウングレードしないでください。

<div style={{ overflowX: "auto" }}><table style={{ display: "table", width: "100%" }}><thead><tr><th width="175">Server CE/Server Pro</th><th width="139" style={{ textAlign: "center" }}>MongoDB のバージョン</th><th style={{ textAlign: "center" }}>最小の機能互換性バージョン</th><th style={{ textAlign: "center" }}>Node.js ドライバーがサポートする最大バージョン</th></tr></thead><tbody><tr><td>2.0.x</td><td style={{ textAlign: "center" }}>3.4</td><td style={{ textAlign: "center" }}>-</td><td style={{ textAlign: "center" }}>-</td></tr><tr><td>2.1.x 〜 2.4.x</td><td style={{ textAlign: "center" }}>3.6</td><td style={{ textAlign: "center" }}>-</td><td style={{ textAlign: "center" }}>-</td></tr><tr><td>>=2.5.0</td><td style={{ textAlign: "center" }}>4.0</td><td style={{ textAlign: "center" }}>-</td><td style={{ textAlign: "center" }}>-</td></tr><tr><td>>=3.1.0</td><td style={{ textAlign: "center" }}>4.2</td><td style={{ textAlign: "center" }}>-</td><td style={{ textAlign: "center" }}>-</td></tr><tr><td>>=3.2.0</td><td style={{ textAlign: "center" }}>4.4</td><td style={{ textAlign: "center" }}>-</td><td style={{ textAlign: "center" }}>-</td></tr><tr><td>>=4.2.0</td><td style={{ textAlign: "center" }}>5.0</td><td style={{ textAlign: "center" }}>-</td><td style={{ textAlign: "center" }}>-</td></tr><tr><td>>=5.1.0</td><td style={{ textAlign: "center" }}>6.0</td><td style={{ textAlign: "center" }}>-</td><td style={{ textAlign: "center" }}>-</td></tr><tr><td>>=5.3.1</td><td style={{ textAlign: "center" }}>6.0</td><td style={{ textAlign: "center" }}>5.0</td><td style={{ textAlign: "center" }}>8.0</td></tr><tr><td>>=5.5.0</td><td style={{ textAlign: "center" }}>6.0</td><td style={{ textAlign: "center" }}>6.0</td><td style={{ textAlign: "center" }}>8.0</td></tr><tr><td>6.0.0</td><td style={{ textAlign: "center" }}>8.0</td><td style={{ textAlign: "center" }}>8.0</td><td style={{ textAlign: "center" }}>8.0</td></tr></tbody></table></div>

<Danger>
  上記の最小の機能互換性バージョンは、表示されている Overleaf のバージョンでの使用が推奨される、対応する MongoDB のバージョンに基づいています。より新しいバージョンを使用する予定の場合は、MongoDB 独自の最小要件があります。
</Danger>

MongoDB で使用できる MongoDB Node.js ドライバーのサポート対象バージョンを示す互換性表は、[こちら](https://www.mongodb.com/docs/drivers/node/current/reference/compatibility/)で確認できます。

MongoDB の各バージョンのサポート終了状況は、[こちら](https://endoflife.date/mongodb)で確認できます。

#### MongoDB のアップグレード

MongoDB では**段階的なアップグレード**が必要です。つまり、たとえば `4.0` から `5.0` に直接アップグレードすることはできません。まず `4.2` から `4.4` に、その後 `5.0` に更新する必要があります。

<Info>
  MongoDB では、安定版に偶数のバージョン番号が使用されます。
</Info>

#### **Docker 外で MongoDB を実行している場合の更新手順**

MongoDB をアップグレードする際の、mongodb.com の更新手順へのリンクは次のとおりです。

* [MongoDB リリースノート - MongoDB を `4.2` から `4.4` にアップグレードする](https://www.mongodb.com/docs/v4.4/release-notes/4.4-upgrade-standalone/)
* [MongoDB リリースノート - MongoDB を `4.4` から `5.0` にアップグレードする](https://www.mongodb.com/docs/v5.0/release-notes/5.0-upgrade-replica-set/)
* [MongoDB リリースノート - MongoDB を `5.0` から `6.0` にアップグレードする](https://www.mongodb.com/docs/v6.0/release-notes/6.0-upgrade-replica-set/)
* [MongoDB リリースノート - MongoDB を `6.0` から `7.0` にアップグレードする](https://www.mongodb.com/docs/manual/release-notes/7.0-upgrade-replica-set/#std-label-7.0-upgrade-replica-set)
* [MongoDB リリースノート - MongoDB を `7.0` から `8.0` にアップグレードする](https://www.mongodb.com/docs/manual/release-notes/8.0-upgrade-replica-set/#std-label-8.0-upgrade-replica-set)

<Info>
  `5.0` 以降の手順は、スタンドアロンではなくレプリカセットのインストールを対象としています。Server Pro/CE 4.0.1 以降ではトランザクションを使用するため、MongoDB をレプリカセットとして実行する必要があります。
</Info>

<Warning>
  MongoDB 3.2 〜 4.2 のドキュメントは、現在 [https://www.mongodb.com/docs/legacy/](https://www.mongodb.com/docs/legacy/) から参照できます
</Warning>

**基本的な手順**

ほとんどの場合、更新では、実際に mongo のバージョンを更新する前に互換性フラグを設定する必要があります。手順は次のとおりです：

1. MongoDB のリリースノートに記載されているとおりに互換性フラグを設定します（以下の例を参照）。
2. 次に、mongo イメージを更新します：
   1. **Toolkit ユーザー** `MONGO_VERSION` を更新します。例：`MONGO_VERSION=6.0`
   2. **Docker Compose ユーザー** `mongo` イメージタグのバージョンを更新します。\
      例：`services -> mongo -> image: mongo:6.0`

**例：MongoDB を `5.0` から `6.0` にアップグレードする**

まず、MongoDB `6.0` を実行していることを確認しましょう：

Overleaf Toolkit ユーザーの場合：

```bash theme={null}
bin/docker-compose exec mongo mongod --version
# db version v5.0.24
```

Docker Compose ユーザーの場合：

```bash theme={null}
docker compose exec mongo mongod --version
# db version v5.0.24
```

[アップグレード手順](https://docs.mongodb.com/manual/release-notes/3.6-upgrade-standalone/#upgrade-version-path)によると、唯一の要件は `featureCompatibilityVersion` を `5.0` に設定することです。これを行うには、MongoDB シェルを開いて指定されたコマンドを実行します：

Overleaf Toolkit ユーザーの場合：

```bash theme={null}
bin/mongo
# MongoDB shell version v5.0.24
# ...
# overleaf:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "5.0" } )
# { 
# "ok" : 1,
# ...
# }
overleaf:PRIMARY> exit
# bye
```

<Info>
  Docker Compose ユーザーは、`docker compose exec mongo mongosh` を実行してシェルを開き、Toolkit ユーザーと同じコマンドを実行できます。
</Info>

Overleaf Toolkit ユーザーの場合：

次に、`bin/stop` コマンドを使用して Server CE/Server Pro と MongoDB のインスタンスを停止し、`config/overleaf.rc` で `MONGO_VERSION=6.0` を設定してから、`bin/up mongo` を使用して `mongo` サービスを再起動し、更新が問題なく行われたことを確認します。

最後に、Server CE/Server Pro のイメージのバージョンを目的のバージョンに更新し、`bin/up -d` コマンドを使用してすべてのサービスを再作成します。

Docker Compose ユーザーの場合：

次に、`docker compose stop` コマンドを使用して Server CE/Server Pro と MongoDB のインスタンスを停止し、[`docker-compose.yml`](https://github.com/overleaf/overleaf/blob/4b1babd4ea634ab12c54e9a9aea7db0e8a500941/docker-compose.yml) ファイルを更新して `image: mongo:6.0` を使用するようにしてから、`docker compose up mongo` コマンドを使用して `mongo` サービスを再起動し、更新が問題なく行われたことを確認します。

最後に、Server CE/Server Pro のイメージのバージョンを目的のバージョンに更新し、`docker compose up` コマンドを使用してすべてのサービスを再作成します。

<strong>例：MongoDB を `6.0` から `7.0` にアップグレードする（Toolkit ユーザー）</strong>

まず、上記の `mongod --version` コマンドを使用して、MongoDB `6.0` を実行していることを確認します。

[アップグレード手順](https://www.mongodb.com/docs/manual/release-notes/7.0-upgrade-replica-set/#std-label-7.0-upgrade-replica-set)によると、唯一の要件は `featureCompatibilityVersion` を `6.0` に設定することです。これを行うには、MongoDB シェルを開いて `db.adminCommand({ setFeatureCompatibilityVersion: "6.0" })` コマンドを実行します。

Overleaf Toolkit ユーザーの場合：

```bash theme={null}
bin/mongo
# ...
# overleaf:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "6.0" } )
# { 
# "ok" : 1,
# ...
# }
overleaf:PRIMARY> exit
# bye
```

次に、`bin/stop` コマンドを使用して Server CE/Server Pro と MongoDB のインスタンスを停止し、`config/overleaf.rc` で `MONGO_VERSION=7.0` を設定してから、`bin/up mongo` を使用して `mongo` サービスを再起動し、更新が問題なく行われたことを確認します。

最後に、Server CE/Server Pro のイメージのバージョンを目的のバージョンに更新し、`bin/up -d` コマンドを使用してすべてのサービスを再作成します。

<strong>例：MongoDB を `7.0` から `8.0` にアップグレードする（Toolkit ユーザー）</strong>

まず、上記の `mongod --version` コマンドを使用して、MongoDB `7.0` を実行していることを確認します。

[アップグレード手順](https://www.mongodb.com/docs/manual/release-notes/8.0-upgrade-replica-set/#std-label-8.0-upgrade-replica-set)によると、唯一の要件は `featureCompatibilityVersion` を `7.0` に設定することです。これを行うには、MongoDB シェルを開いて `db.adminCommand({ setFeatureCompatibilityVersion: "7.0", confirm: true })` コマンドを実行します。このバージョンでは追加の `confirm: true` パラメーターが必要になったことに注意してください。

```bash theme={null}
bin/mongo
# ...
# overleaf:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "7.0", confirm: true } )
# { 
# "ok" : 1,
# ...
# }
overleaf:PRIMARY> exit
# bye
```

次に、`bin/stop` コマンドを使用して Server CE/Server Pro と MongoDB のインスタンスを停止し、`config/overleaf.rc` で `MONGO_VERSION=8.0` を設定してから、`bin/up mongo` を使用して `mongo` サービスを再起動し、更新が問題なく行われたことを確認します。

最後に、Server CE/Server Pro のイメージのバージョンを目的のバージョンに更新し、`bin/up -d` コマンドを使用してすべてのサービスを再作成します。

#### Docker Compose ユーザー向けの同等のコマンド

Docker Compose ユーザー向けの同等のコマンドは次のとおりです：

* `docker compose exec mongo mongod --version`：mongo のバージョンを表示します
* `docker compose exec mongo mongosh`：管理コマンド用の mongo シェルを起動します
* `docker compose stop` コマンド：サーバーを停止します
* [`docker-compose.yml`](https://github.com/overleaf/overleaf/blob/4b1babd4ea634ab12c54e9a9aea7db0e8a500941/docker-compose.yml) ファイルを編集して `image: mongo:6.0` を使用するようにし、mongo のバージョンをアップグレードします
* `docker compose up mongo`：mongo サービスを再起動し、更新が問題なく行われたことを確認します
* [`docker-compose.yml`](https://github.com/overleaf/overleaf/blob/4b1babd4ea634ab12c54e9a9aea7db0e8a500941/docker-compose.yml) ファイルを編集して `image: sharelatex:VERSION` を使用するようにし、イメージのバージョンをアップグレードします
* `docker compose up`：すべてのサービスを再作成します。

### カスタムロールの作成

バージョン `5.5.1` では、MongoDB の機能互換性バージョンを確認する起動時チェックが導入されました。MongoDB データベースで認証（基本認証など）を使用している場合、`sharelatex` コンテナが起動せず、"*not authorized on admin to execute command*" という権限エラーが表示されることがあります。

これを解決するには、以下の手順に従って MongoDB で新しいロールを作成し、データベースへのアクセスに使用するユーザーアカウントに割り当てるか、`ALLOW_MONGO_ADMIN_CHECK_FAILURES=true` を設定して、チェックが失敗しても導入環境の起動を妨げないようにします。

<Check>
  この新しいロールは、クラスター全体の MongoDB サーバーパラメーターを読み取る権限**のみ**を付与するもので、監視目的にも再利用できます。
</Check>

```bash wrap theme={null}
# Toolkit users
$ bin/docker-compose exec -it mongo mongosh -u {{YOUR-ADMIN-USERNAME}} -p

# Switch to the "admin" database using
overleaf [direct: primary] sharelatex> use admin
switched to db admin
overleaf [direct: primary] admin> 

# Create a new role with permission to use "getParamter". Copy and paste the function below into the shell and press the return key

db.createRole(
  {
    role: "clusterParameterReader",
    privileges: [
      {
        resource: { cluster: true },
        actions: ["getParameter"]    
      }
    ],
    roles: [] 
  }
);

# Switch back to the "sharelatex" database 
use sharelatex
switched to db sharelatex
overleaf [direct: primary] sharelatex> 

# Assign the new "clusterParameterReader" role to your database user by copy and pasting the function below into the shell and press the return key 
db.grantRolesToUser(
  "{{YOUR-DATABASE-USER}}",
  [
    { role: "clusterParameterReader", db: "admin" }
  ]
);

# Type exit then return to exit the MongoDB shell
# Run bin/up -d to start the deployment stack
$ bin/up -d
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.