> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monad.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# 运行归档服务器

请参阅[数据瀑布流](/zh/node-ops/archive-data/data-waterfall)以了解各种归档数据来源的概览。本页专门介绍运行归档服务器的操作细节。

## 推荐配置规模

对于希望可靠地提供历史交易数据服务的任何人,建议运行多个归档服务器,由多个运行 Archive Writer 进程的全节点为其提供数据。

建议配置:

* 2 个运行 MongoDB + monad-indexer 的归档服务器
* 2 个 Archive Writer 节点,即全节点 + monad-archiver
* 大量提供 RPC 请求服务的全节点,它们连接到这两个归档服务器以获取历史交易数据

## 推荐的归档服务器主机规格

* CPU:16 核
* 内存:最低 64GB;为获得最佳性能,建议 > 512GB
* 存储:最低推荐 16TB;首选 32TB+
  * NVMe SSD
  * RAID 10
  * 至少持续 10K IOPS
* 网络:> 1GbE,当为更多 RPC 服务器提供服务时按需扩展

## 全新安装

以下说明假定已存在一个全节点(用于 Archive Writer)和一个全新主机(用于归档服务器)。

### 在您的归档服务器上:

1. 将以下内容添加到 `/home/monad/.env`

   ```sh theme={null}
   # Replace <db username> and <db pwd> with your actual values
   DB_USER="<your db username>"
   DB_PWD="<your db pwd>"
   DB_VOLUME="~/archive-db-data"

   # Note: source and sink should generally be the same local MongoDB.
   # Indexer reads from:
   BLOCK_DATA_SOURCE="mongodb mongodb://${DB_USER}:${DB_PWD}@0.0.0.0:27017 archive-db"
   # Indexer writes to:
   ARCHIVE_SINK="mongodb mongodb://${DB_USER}:${DB_PWD}@0.0.0.0:27017 archive-db"
   OTEL_ENDPOINT="http://0.0.0.0:4317"
   ```

2. 运行 MongoDB。以下说明展示了如何在 Docker 中运行,但运维人员可以按自己偏好的方式运行。

   ```yml theme={null}
   archive-db:
     image: mongo:latest
     command: mongod --bind_ip 0.0.0.0
     networks:
       - host
     environment:
       MONGO_INITDB_ROOT_USERNAME: ${DB_USER}
       MONGO_INITDB_ROOT_PASSWORD: ${DB_PWD}
     ports:
       - "27017:27017"
     volumes:
       - ${DB_VOLUME}:/data/db
     logging:
       driver: journald
       options:
         tag: "mongo"
   ```

3. 启动数据库

   ```sh theme={null}
   # create directory from .env db volume
   source /home/monad/.env
   sudo mkdir -p $DB_VOLUME
   sudo chown -R monad:monad $DB_VOLUME
   sudo chmod 700 $DB_VOLUME

   docker compose up archive-db -d
   ```

### 在您的 Archive Writer 主机上:

4. 将以下内容添加到 `/home/monad/.env`

   ```sh theme={null}
   ###############################################################
   ################### Vars that do not change ###################
   ###############################################################
   # Replace with your Archive Server credentials
   ARCHIVE_DB_USER="<your db username>"
   ARCHIVE_DB_PWD="<your db pwd>"
   ARCHIVE_DB_HOST="<your db hostname>"

   ## Where block data gets written (your Archive Server)
   ARCHIVE_SINK="mongodb mongodb://${ARCHIVE_DB_USER}:${ARCHIVE_DB_PWD}@${ARCHIVE_DB_HOST}:27017 archive-db"
   ## Change this if your ledger folder is in a different location
   BFT_BLOCK_PATH=~/monad-bft/ledger
   OTEL_ENDPOINT=http://0.0.0.0:4317
   ## This can be significantly higher during backfill
   MAX_CONCURRENT_BLOCKS=50


   ###############################################################
   ####### Backfill from Genesis (initial configuration) #########
   ###############################################################
   BLOCK_DATA_SOURCE="aws testnet-can-004-0-aavn9ll 50" # testnet
   BLOCK_DATA_SOURCE="aws mainnet-deu-010-0 50" # mainnet
   ## Alternatively you can use your other ArchiveDB if it has already been backfilled
   ## If using another Archive Server, define variables for it and use them:
   #OTHER_DB_USER="<other db username>"
   #OTHER_DB_PWD="<other db pwd>"
   #OTHER_DB_HOST="<other db hostname>"
   #BLOCK_DATA_SOURCE="mongodb mongodb://${OTHER_DB_USER}:${OTHER_DB_PWD}@${OTHER_DB_HOST}:27017 archive-db"
   FALLBACK_BLOCK_DATA_SOURCE="aws testnet-can-004-0-aavn9ll-0 50" # testnet
   FALLBACK_BLOCK_DATA_SOURCE="aws mainnet-deu-010-0 50" # mainnet

   ###############################################################
   ###################### Normal Operation #######################
   ###############################################################
   ## Archiver checks triedb first for block data
   BLOCK_DATA_SOURCE="triedb /dev/triedb 5000"

   ### FALLBACK_BLOCK_DATA_SOURCE ###
   ## This is used whenever data is missing from BLOCK_DATA_SOURCE
   ## Normally used after state-sync
   ## If you have another Archive Server for redundancy, configure it here:
   #OTHER_DB_USER="<other db username>"
   #OTHER_DB_PWD="<other db pwd>"
   #OTHER_DB_HOST="<other db hostname>"
   #FALLBACK_BLOCK_DATA_SOURCE="mongodb mongodb://${OTHER_DB_USER}:${OTHER_DB_PWD}@${OTHER_DB_HOST}:27017 archive-db"

   ## Alternatively you can use category-labs aws bucket
   ## Note: YOU pay for S3 egress costs if pulling from this bucket
   FALLBACK_BLOCK_DATA_SOURCE="aws testnet-can-004-0-aavn9ll 50" # testnet
   FALLBACK_BLOCK_DATA_SOURCE="aws mainnet-deu-010-0 50" # mainnet
   ```

5. 回填(Backfilling):

   <Note>
     自 v0.12.3 起,`--start-block` 不再作为守护进程参数支持。请改用 `set-start-block` 子命令,在启动守护进程之前以命令式方式设置起始区块标记。这可以防止因 systemd 覆盖配置导致意外丢失进度。
   </Note>

   ```sh theme={null}
   # [Optional] Set a specific start block if needed
   # This is typically only needed for initial setup or recovery scenarios
   # Example: start archiving from block 1000000
   monad-archiver set-start-block --block 1000000 --archive-sink "${ARCHIVE_SINK}"

   # For async backfill marker, add the --async-backfill flag:
   # monad-archiver set-start-block --block 1000000 --archive-sink "${ARCHIVE_SINK}" --async-backfill

   # Start the archiver daemon
   sudo systemctl start monad-archiver
   # Should show it running
   # Double check the arguments look correct based on the above ^
   systemctl status monad-archiver
   # Expect many:
   # > INFO: Successfully archived block block_num=X
   journalctl -u monad-archiver -o cat
   ```

6. 一旦 `monad-archiver` 追上链的最新高度,您将看到:

   > `INFO: Nothing to process`

7. 这意味着回填已完成,您应该:

   1. 在您的 `.env` 中注释掉 `BACKFILL` 部分,并取消注释 `Normal Operation`
   2. 重启: `systemctl restart monad-archiver`
   3. 按上述方式验证状态和日志

### 在您的 ArchiveDB 主机上

8. 启动 `monad-indexer`

   ```sh theme={null}
   sudo systemctl start monad-indexer
   systemctl status monad-indexer
   # Expect many:
   # > INFO: Indexing block...
   # > INFO: Index spot-check successful
   journalctl -u monad-indexer -o cat
   ```

## \[可选] 设置 `monad-archive-checker`

通常每个组织只运行 1 个 checker,但如有需要可以运行更多。

1. 为 `monad-archive-checker` 添加以下 systemd 覆盖配置

   ```sh theme={null}
   sudo systemctl edit monad-archive-checker
   ```

   ```toml theme={null}
   [Service]
   ExecStart=
   ExecStart=/usr/local/bin/monad-archive-checker \
       # Example --bucket my-org-mainnet-checker
       # Note: storing checker state in local mongo or filesystem
       #       will be supported in a future release
       --bucket <S3 Bucket for storing checker state>
       checker
       # Example of comparing a local self-hosted mongo against a Category Labs aws bucket
       # --init-replicas "<aws mainnet-deu-009-0 50,mongodb mongodb://<username>:<pwd>@<db hostname>:27017 archive-db>,mongodb mongodb://<username>:<pwd>@<second db hostname>:27017 archive-db>"
       --init-replicas "<ArchiveDB 1>,<ArchiveDB 2>"
   ```

2. 重新加载并重启

   ```sh theme={null}
   sudo systemctl daemon-reload
   sudo systemctl restart monad-archive-checker
   systemctl status monad-archive-checker

   # Check for errors
   journalctl -u monad-archive-checker -o cat -f
   ```
