> For the complete documentation index, see [llms.txt](https://ayakaleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayakaleaf-pro.ayaka.space/on-premises/zh-cn/wei-hu/updating-mongodb.md).

# 更新 MongoDB

{% hint style="info" %}
任何新的 Server CE/Server Pro 版本都会在其 [发行说明](https://docs.overleaf.com/on-premises/release-notes).
{% endhint %}

### 我应该更新 MongoDB 吗？

你应该 **仅** 如果你计划升级你的 Server CE/Server Pro 实例，则考虑更新你的 MongoDB 版本。

如果你运行的 MongoDB 版本比当前（或目标）版本推荐的更新，则无需进行任何更改。

{% hint style="warning" %}
你绝不应降级你的 MongoDB 版本。
{% endhint %}

如果你遇到一个具体问题，并认为它可能与你当前的 MongoDB 版本有关，请随时 [提交问题](https://github.com/overleaf/overleaf/issues) 如果你是 Server CE 用户，或者如果你是 Server Pro 用户，请联系 Overleaf 支持。

### 检查你的 MongoDB 版本

打开 `mongo` shell 后应立即打印当前版本。

Overleaf Toolkit 用户：

```bash
bin/docker-compose exec mongo mongod --version
数据库版本 v5.0.24
构建信息：{
    "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
docker compose exec mongo mongod --version
数据库版本 v5.0.24
构建信息：{
    "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 用户）。

{% hint style="danger" %}
在执行 Server CE/Pro 升级时，我们建议先升级到已部署主版本的最新发布版 **之前** 再升级到 **下一个** 主版本的最新发布版。如果你的部署比最新版本落后超过一个主版本，则需要执行多步骤升级。

例如，如果你运行的是 3.5.10，则需要升级到 3.5.13 -> 执行完整项目历史迁移 -> 4.2.9 -> 5.5.4。

你应该 **绝不** 跳过主版本（3.5.10 -> 5.5.4）。如果你使用 Toolkit，且比最新版本落后超过一个主版本，你必须 **不要** 使用 `bin/upgrade` 该脚本，因为你将需要执行手动的多步骤升级。
{% endhint %}

{% hint style="warning" %}
重要的是要确保你进行 [一致性备份](/on-premises/zh-cn/wei-hu/data-and-backups.md#performing-a-consistent-backup) **之前** 在每次主版本升级时，以便在需要时能够回滚。
{% endhint %}

#### 版本支持信息

如果你决定回退到更早的版本，此表显示较早版本 Server CE/Server Pro 推荐的 MongoDB 版本，但你应该 **绝不** 降级你的 MongoDB 版本。

<table><thead><tr><th width="175">Server CE/Server Pro</th><th width="139" align="center">MongoDB 版本</th><th align="center">最低功能兼容版本</th><th align="center">Node.js 驱动支持的最大版本</th></tr></thead><tbody><tr><td>2.0.x</td><td align="center">3.4</td><td align="center">-</td><td align="center">-</td></tr><tr><td>2.1.x 到 2.4.x</td><td align="center">3.6</td><td align="center">-</td><td align="center">-</td></tr><tr><td>>=2.5.0</td><td align="center">4.0</td><td align="center">-</td><td align="center">-</td></tr><tr><td>>=3.1.0</td><td align="center">4.2</td><td align="center">-</td><td align="center">-</td></tr><tr><td>>=3.2.0</td><td align="center">4.4</td><td align="center">-</td><td align="center">-</td></tr><tr><td>>=4.2.0</td><td align="center">5.0</td><td align="center">-</td><td align="center">-</td></tr><tr><td>>=5.1.0</td><td align="center">6.0</td><td align="center">-</td><td align="center">-</td></tr><tr><td>>=5.3.1</td><td align="center">6.0</td><td align="center">5.0</td><td align="center">8.0</td></tr><tr><td>>=5.5.0</td><td align="center">6.0</td><td align="center">6.0</td><td align="center">8.0</td></tr><tr><td>6.0.0</td><td align="center">8.0</td><td align="center">8.0</td><td align="center">8.0</td></tr></tbody></table>

{% hint style="danger" %}
上面的最低功能兼容版本基于与所示 Overleaf 版本配合使用所推荐的相应 MongoDB 版本。如果你计划使用更高版本，MongoDB 将有其自身的最低要求。
{% endhint %}

你可以在这里查看说明可与 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`.

{% hint style="info" %}
MongoDB 对其稳定版本使用偶数。
{% endhint %}

#### **在 Docker 外运行 MongoDB 时的更新说明**

以下是 mongodb.com 上关于升级 MongoDB 的更新说明链接。

* [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)

{% hint style="info" %}
针对 `5.0` 及更高版本的说明指向副本集安装，而不是独立实例。由于 Server Pro/CE 4.0.1+ 使用事务，MongoDB 需要以副本集方式运行。
{% endhint %}

{% hint style="warning" %}
MongoDB 3.2 到 4.2 的文档现可通过以下地址获取： <https://www.mongodb.com/docs/legacy/>
{% endhint %}

**基本说明**

在大多数情况下，更新要求在实际更新 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
bin/docker-compose exec mongo mongod --version
# 数据库版本 v5.0.24
```

Docker Compose 用户：

```bash
docker compose exec mongo mongod --version
# 数据库版本 v5.0.24
```

根据 [升级说明](https://docs.mongodb.com/manual/release-notes/3.6-upgrade-standalone/#upgrade-version-path)，唯一要求是将 `featureCompatibilityVersion` 设置为 `5.0`。我们通过打开 MongoDB shell 并运行所示命令来完成：

Overleaf Toolkit 用户：

```bash
bin/mongo
# MongoDB shell 版本 v5.0.24
# ...
# overleaf:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "5.0" } )
# { 
# "ok" : 1,
# ...
# }
overleaf:PRIMARY> exit
# 再见
```

{% hint style="info" %}
Docker Compose 用户可以运行 `docker compose exec mongo mongosh` 以获取 shell 并运行与 Toolkit 用户相同的命令。
{% endhint %}

Overleaf Toolkit 用户：

然后我们将使用 `bin/stop` 命令停止 Server CE/Server Pro 和 MongoDB 实例，设置 `MONGO_VERSION=6.0` 在 `config/overleaf.rc`，然后重启 `mongo` 服务并使用 `bin/up mongo`）来验证更新是否顺利完成。

最后，我们将把 Server CE/Server Pro 镜像版本更新到目标版本，并使用 `bin/up -d` 命令重新创建所有服务。

Docker Compose 用户：

然后我们将使用 `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` 命令来验证更新是否顺利完成。

最后，我们将把 Server CE/Server Pro 镜像版本更新到目标版本，并使用 `docker compose up` 命令重新创建所有服务。

**示例：将 MongoDB 从 `6.0` 到 `7.0` （Toolkit 用户）**

首先确保你正在运行 MongoDB `6.0` 使用上面的 `mongod --version` 命令。

根据 [升级说明](https://www.mongodb.com/docs/manual/release-notes/7.0-upgrade-replica-set/#std-label-7.0-upgrade-replica-set)，唯一要求是将 `featureCompatibilityVersion` 设置为 `6.0`。我们通过打开 MongoDB shell 并运行命令来完成 `db.adminCommand({ setFeatureCompatibilityVersion: "6.0" })` .

Overleaf Toolkit 用户：

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

然后我们将使用 `bin/stop` 命令停止 Server CE/Server Pro 和 MongoDB 实例，设置 `MONGO_VERSION=7.0` 在 `config/overleaf.rc`，然后重启 `mongo` 服务并使用 `bin/up mongo`）来验证更新是否顺利完成。

最后，我们将把 Server CE/Server Pro 镜像版本更新到目标版本，并使用 `bin/up -d` 命令重新创建所有服务。

**示例：将 MongoDB 从 `7.0` 到 `8.0` （Toolkit 用户）**

首先确保你正在运行 MongoDB `7.0` 使用上面的 `mongod --version` 命令。

根据 [升级说明](https://www.mongodb.com/docs/manual/release-notes/8.0-upgrade-replica-set/#std-label-8.0-upgrade-replica-set)，唯一要求是将 `featureCompatibilityVersion` 设置为 `7.0`。我们通过打开 MongoDB shell 并运行命令来完成 `db.adminCommand({ setFeatureCompatibilityVersion: "7.0", confirm: true })`。请注意，这现在需要额外的 `confirm: true` 参数。

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

然后我们将使用 `bin/stop` 命令停止 Server CE/Server Pro 和 MongoDB 实例，设置 `MONGO_VERSION=8.0` 在 `config/overleaf.rc`，然后重启 `mongo` 服务并使用 `bin/up 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 shell 以执行管理员命令
* `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` 容器可能无法启动，并显示“*未获得在 admin 上执行命令的授权*”权限错误。

为了解决此问题，你可以按照下面的说明在 MongoDB 中创建一个新角色并将其分配给用于访问数据库的用户账户，或者设置 `ALLOW_MONGO_ADMIN_CHECK_FAILURES=true` 以允许检查失败而不阻止部署启动。

{% hint style="success" %}
这个新角色 **仅** 授予读取集群范围 MongoDB 服务器参数的权限，并且可重复用于监控目的，
{% endhint %}

{% code overflow="wrap" %}

```bash
# Toolkit 用户
$ bin/docker-compose exec -it mongo mongosh -u {{YOUR-ADMIN-USERNAME}} -p

# 使用
overleaf [direct: primary] sharelatex> use admin
已切换到数据库 admin
overleaf [direct: primary] admin> 

# 创建一个新角色，并授予使用“getParamter”的权限。将下面的函数复制并粘贴到 shell 中，然后按回车键

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

# 切换回 "sharelatex" 数据库 
use sharelatex
已切换到数据库 sharelatex
overleaf [direct: primary] sharelatex> 

# 将新 "clusterParameterReader" 角色分配给你的数据库用户，将下面的函数复制并粘贴到 shell 中，然后按回车键 
db.grantRolesToUser(
  "{{YOUR-DATABASE-USER}}",
  [
    { role: "clusterParameterReader", db: "admin" }
  ]
);

# 输入 exit，然后按回车键退出 MongoDB shell
# 运行 bin/up -d 以启动部署栈
$ bin/up -d
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ayakaleaf-pro.ayaka.space/on-premises/zh-cn/wei-hu/updating-mongodb.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
