mongo

FerretDB/DocumentDB 内核,提供 MongoDB 协议兼容能力

mongo 配置模板使用 FerretDB 或 AWS DocumentDB 兼容层,为 PostgreSQL 提供 MongoDB 线缆协议兼容能力。


配置概览

  • 配置名称: mongo
  • 节点数量: 单节点
  • 配置说明:FerretDB/DocumentDB MongoDB 兼容配置
  • 适用系统:el8, el9, d12, u22, u24
  • 适用架构:x86_64, aarch64
  • 相关配置:meta

启用方式:

./configure -c mongo [-i <primary_ip>]

配置内容

源文件地址:pigsty/conf/mongo.yml

all:
  children:
    infra: { hosts: { 10.10.10.10: { infra_seq: 1 }} ,vars: { repo_enabled: false }}
    etcd:  { hosts: { 10.10.10.10: { etcd_seq: 1  }} ,vars: { etcd_cluster: etcd  }}

    #----------------------------------------------#
    # MongoDB Compatible PostgreSQL
    #----------------------------------------------#
    pg-meta:
      hosts:
        10.10.10.10: { pg_seq: 1, pg_role: primary }
      vars:
        pg_cluster: pg-meta
        pg_users:
          - {name: dbuser_meta ,password: DBUser.Meta   ,pgbouncer: true ,roles: [dbrole_admin]    ,comment: pigsty admin user }
          - {name: dbuser_view ,password: DBUser.Viewer ,pgbouncer: true ,roles: [dbrole_readonly] ,comment: read-only viewer }
        pg_databases:
          - name: meta
            baseline: cmdb.sql
            comment: pigsty meta database
            schemas: [pigsty]
            extensions:
              - { name: documentdb_core }
              - { name: documentdb_distributed }
        pg_hba_rules:
          - {user: dbuser_view , db: all ,addr: infra ,auth: pwd ,title: 'allow grafana access'}
        node_crontab: [ '00 01 * * * postgres /pg/bin/pg-backup full' ]

        # DocumentDB 专用配置
        pg_mode: mongo                    # MongoDB 兼容模式
        pg_extensions: [ documentdb, postgis, pgvector ]
        pg_libs: 'documentdb.so, pg_stat_statements, auto_explain'

    #----------------------------------------------#
    # FerretDB Docker 服务 (可选)
    #----------------------------------------------#
    app:
      hosts: { 10.10.10.10: {} }
      vars:
        docker_enabled: true
        app: ferretdb
        apps:
          ferretdb:
            conf:
              FERRETDB_POSTGRESQL_URL: "postgres://dbuser_meta:DBUser.Meta@10.10.10.10:5432/ferretdb"

  vars:
    version: v4.0.0
    admin_ip: 10.10.10.10
    region: default
    infra_portal:
      home : { domain: i.pigsty }

    nodename_overwrite: false
    node_repo_modules: node,infra,pgsql
    node_tune: oltp

    pg_version: 17
    pg_conf: oltp.yml

    #----------------------------------------------#
    # PASSWORD
    #----------------------------------------------#
    grafana_admin_password: pigsty
    grafana_view_password: DBUser.Viewer
    pg_admin_password: DBUser.DBA
    pg_monitor_password: DBUser.Monitor
    pg_replication_password: DBUser.Replicator
    patroni_password: Patroni.API
    haproxy_admin_password: pigsty
    minio_secret_key: S3User.MinIO
    etcd_root_password: Etcd.Root

配置解读

mongo 模板让您可以使用 MongoDB 客户端工具(如 mongosh)连接 PostgreSQL。

两种实现方式

  1. FerretDB:作为 Docker 服务运行的协议转换代理
  2. DocumentDB:PostgreSQL 扩展,原生支持 MongoDB 协议

关键特性

  • 使用 MongoDB 协议(端口 27017),兼容 MongoDB 客户端
  • 数据实际存储在 PostgreSQL(JSONB 格式)
  • 保留 PostgreSQL 的 ACID 特性和事务支持
  • 可结合 PostgreSQL 扩展(如 pgvector)使用

连接方式

# 使用 mongosh 连接 FerretDB
mongosh "mongodb://dbuser_meta:DBUser.Meta@10.10.10.10:27017/ferretdb"

# 同时保留 PostgreSQL 连接能力
psql postgres://dbuser_meta:DBUser.Meta@10.10.10.10:5432/ferretdb

适用场景

  • 从 MongoDB 迁移到 PostgreSQL
  • 需要同时支持 MongoDB 和 SQL 的应用
  • 希望在 PostgreSQL 上存储文档数据
  • 需要利用 PostgreSQL 生态(如向量搜索)处理文档

注意事项

  • 部分 MongoDB 特性可能存在兼容性差异
  • FerretDB 通过 Docker 运行,需要启用 Docker
  • DocumentDB 扩展提供更原生的集成

最后修改 2025-12-21: udpate conf structure (2cd7d6e)