# MongoDB设置

{% hint style="info" %}
及时获悉最准确和最新硬件建议，请参考[MongoDB官方文档](https://www.mongodb.com/zh-cn/docs/cloud-manager/tutorial/provisioning-prep/)。
{% endhint %}

该系统需要一个MongoDB副本集集群，用于实现数据冗余和高可用性。

对于生产部署，强烈建议使用多节点集群，以确保最佳性能和稳定性。

### 硬件要求

* CPU：多核处理器
* 内存：单个节点至少 4GB
* 硬盘：建议使用SSD硬盘，以获得更好的性能

MongoDB代理只支持安装在64位操作系统中。

### 安装MongoDB

遵循[MongoDB官方安装文档](https://www.mongodb.com/zh-cn/docs/manual/administration/install-community/)要求，选择合适你操作系统的安装流程。

### 配置MongoDB副本集

1. **编辑MongoDB配置文件**

```
sudo vi /etc/mongod.conf
```

2. **添加副本集配置**

找到`replication`部分并添加以下代码：

```
replication:
   replSetName: "rs0"
```

此过程将指示MongoDB作为指定副本集 (本示例中为`rs0` ) 的一部分启动。

3. **重启MongoDB，以上配置将生效**

```
sudo systemctl restart mongod
```

4. **初始化副本集**

启动MongoDB shell，并连接到本地端口27017， (`localhost` - `27017`)，运行已安装的MongoDB实例。

如果你的MongoDB服务器运行在不同的主机或端口上，可以分别使用`--host`和`--port`选项进行指定。

```
mongosh --host localhost --port 27017
```

然后，在MongoDB shell中运行下方命令：

```
rs.initiate()
```

5. **验证副本集配置**

```
rs.status()
```

`rs.status()`的输出将包括以下信息：

* `Set Name` (set)：副本集名称
* `成员` (成员)：副本集成员列表 (成员阵列)，包含`hostname` (主机名)、`state` (状态)、`health` (健康状况)、`uptime` (运行时间)、`last heartbeat message` (最后heartbeat消息) 等详细信息。

以下是一个简化示例，用于展示输出结果：

```
  set: 'rs0',
  members: [
    {
      _id: 0,
      name: '127.0.0.1:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 1202009,
      optime: { ts: Timestamp({ t: 1721191458, i: 1 }), t: Long('1') },
      optimeDate: ISODate('2024-07-17T04:44:18.000Z'),
      lastAppliedWallTime: ISODate('2024-07-17T04:44:18.441Z'),
      lastDurableWallTime: ISODate('2024-07-17T04:44:18.441Z'),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1719989484, i: 2 }),
      electionDate: ISODate('2024-07-03T06:51:24.000Z'),
      configVersion: 1,
      configTerm: 1,
      self: true,
      lastHeartbeatMessage: ''
    }
  ]
```

### 创建MongoDB凭证

{% hint style="info" %}
服务器稍后将使用这些确切的凭证连接到队列。
{% endhint %}

启动MongoDB shell，并连接到本地端口27017， (`localhost` - `27017`)，运行已安装的MongoDB实例。

如果你的MongoDB服务器运行在不同的主机或端口上，可以分别使用`--host`和`--port`选项进行指定。

```
mongosh --host localhost --port 27017
```

在MongoDB shell中，运行下方命令创建新用户：

```
use admin
db.createUser({
  user: "<username>",
  pwd: "<password>",
  roles: [ { role: "readWrite", db: "<database>" } ]
})
```

你可以自定义`<username>` (用户名)、 `<password>`(密码)和`<database>`(数据库名称)。

### 服务器监控

可通过[Prometheus Blackbox Exporter](https://github.com/prometheus/blackbox_exporter)轮询MongoDB服务器的可用性。

MongoDB特定的Prometheus指标也可以通过任何开源[Prometheus MongoDB导出器](https://github.com/percona/mongodb_exporter)来公开。


---

# Agent Instructions: 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:

```
GET https://www.docszh.com/babylon-docs/user-guides/bitcoin-staking-phase1/backend-deployment/infra/mongodb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
