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

如何在MongoDB 3.6.3中启用ReadConcernMajority?解决pymongo watch报错

Hey there! I’ve dealt with this exact error before when working with MongoDB 3.6 and pymongo’s watch() method. Let’s walk through how to fix it step by step.

First: Understand Why This Happens

The ReadConcernMajorityNotEnabled error pops up because either your MongoDB deployment doesn’t support read concern "majority" (like a single-node instance), or the feature is explicitly disabled in your configuration. Change streams (used by watch()) rely on read concern "majority" to ensure you’re seeing consistent, committed data.

Fix 1: Enable Read Concern Majority for Replica Sets (Most Common Use Case)

If you’re running a replica set (the recommended setup for production and change streams), follow these steps:

a. Update Your MongoDB Configuration File

Locate your mongod.conf (Linux: /etc/mongod.conf; Windows: C:\Program Files\MongoDB\Server\3.6\bin\mongod.cfg) and edit the replication block to include the enableMajorityReadConcern setting:

replication:
  replSetName: "yourReplicaSetName"  # You should already have this for a replica set
  enableMajorityReadConcern: true    # Add this line if missing, or set to true

b. Restart the MongoDB Service

Save the config file, then restart the mongod process to apply changes:

  • Linux: sudo systemctl restart mongod
  • Windows: Open Command Prompt as admin and run net stop MongoDB && net start MongoDB

c. Verify the Configuration

Connect to your MongoDB instance via the mongo shell, then run this command to confirm the setting is active:

db.adminCommand({ getParameter: 1, enableMajorityReadConcern: 1 })

You should see output like: { "enableMajorityReadConcern" : true, "ok" : 1 }

Fix 2: Convert a Single-Node Instance to a Replica Set

If you’re running a single-node MongoDB instance, read concern "majority" isn’t supported out of the box (since there’s no "majority" of nodes to confirm writes). To use watch(), you’ll need to convert it to a single-node replica set:

a. Stop the Current mongod Instance

First, shut down your running MongoDB service:

  • Linux: sudo systemctl stop mongod
  • Windows: net stop MongoDB

b. Restart mongod with Replica Set Enabled

Either update your config file to add replSetName: "rs0" under the replication block, or run mongod directly with the replica set flag:

mongod --replSet "rs0" --dbpath /path/to/your/database/folder --port 27017

Adjust the --dbpath and --port values to match your setup.

c. Initialize the Replica Set

Connect to the mongo shell, then run this command to initialize the single-node replica set:

rs.initiate()

Once this completes, MongoDB will enable read concern "majority" by default, and your watch() method should work.

Fix 3: For Sharded Clusters

If you’re using a sharded cluster, you need to enable enableMajorityReadConcern on all shard replica sets and the config server replica set. Follow the same steps as the replica set section for each of these components.

Quick Check for Pymongo

While the main fix is on the MongoDB side, you can explicitly set read concern "majority" in your pymongo code to ensure it’s using the correct setting:

from pymongo import MongoClient, ReadConcern

client = MongoClient("mongodb://your-host:27017/")
collection = client["your-database"]["your-collection"]
cursor = collection.watch(read_concern=ReadConcern("majority"))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:14:59