You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置MongoDB副本集以仅复制指定集合或数据库?

Hey there! Great question—MongoDB's default setup replicates every database and collection in a replica set, but you can absolutely lock this down to only sync specific databases or collections using partial replication. Let's walk through exactly how to do this, step by step.

1. Prerequisites First
  • Make sure you already have a basic replica set up (minimum 3 nodes: 1 primary, 2 secondaries/arbiter). All nodes need to run the same MongoDB version to avoid compatibility issues.
  • Critical note: Nodes configured for partial replication cannot become primary nodes. They have to stay as secondaries, so we'll set their priority/vote count to 0 later to prevent election attempts.
2. Replicate Only Specific Databases

This method lets a secondary node sync only the databases you explicitly list.

Step-by-Step:

  1. Stop the target secondary node's MongoDB service:

    sudo systemctl stop mongod
    

    (Use your custom stop command if you're not using systemd.)

  2. Edit the node's config file (usually /etc/mongod.conf). In the replication section, add the only parameter to list your target databases:

    replication:
      replSetName: "myReplicaSet"  # Match your existing replica set name
      only: ["inventory", "sales"]  # List databases you want to replicate; single DB uses ["inventory"]
    

    Heads up: The only flag only works on secondary nodes—never set this on your primary.

  3. Warning: Wipe the node's existing data directory (old full-replica data will conflict with filtered replication):

    sudo rm -rf /var/lib/mongodb/*  # Replace with your actual data path—double-check this!
    
  4. Start the MongoDB service back up:

    sudo systemctl start mongod
    
  5. Re-add the node to the replica set (with non-electable settings):
    Connect to your primary node via mongosh:

    mongosh --host <primary-ip>:27017
    

    Then run these commands:

    // Remove the old entry for the secondary
    rs.remove("<secondary-ip>:27017")
    // Re-add it with priority/vote set to 0 (non-electable)
    rs.add({
      _id: 1,  // Match the node's original _id in rs.conf()
      host: "<secondary-ip>:27017",
      priority: 0,
      votes: 0
    })
    // Verify the updated config
    rs.conf()
    

    The secondary will now only sync the databases you listed.

3. Replicate Only Specific Collections

For finer control (syncing individual collections within a database), use the filter parameter with a namespace match.

Step-by-Step:

  1. Stop the target secondary node's MongoDB service (same as before):

    sudo systemctl stop mongod
    
  2. Edit the config file to add the filter rule. For example, to sync only inventory.products and sales.orders:

    replication:
      replSetName: "myReplicaSet"
      filter: '{ "ns": { "$in": ["inventory.products", "sales.orders"] } }'
    
    • ns stands for "namespace" (format: database.collection)
    • Use $in to list multiple collections you want to sync
  3. Wipe the node's data directory (same warning as before):

    sudo rm -rf /var/lib/mongodb/*
    
  4. Start the service and re-add the node to the replica set with non-electable settings—follow the same steps as the database-level filtering section.

4. Verify It's Working

Connect to the filtered secondary via mongosh and run these checks:

// Check replication status
rs.status()
// List databases—only your target ones should appear
show dbs
// Check collections in the target DB
use inventory
show collections
// Confirm data matches the primary
db.products.find().count()

If the counts match the primary's collections, you're good to go!

Key Notes to Remember
  • Partial replication nodes can never be primary—always set priority: 0 and votes: 0 to avoid cluster issues.
  • Once you set only or filter, you can't switch back to full replication without wiping the node's data and reconfiguring.
  • You can set up multiple secondaries with different filter rules if you need different subsets of data.
  • Keep all nodes on the same MongoDB version—partial replication has strict compatibility requirements.

内容的提问来源于stack exchange,提问作者mohamed hamada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:23:12