MongoDB副本集踩坑

635 阅读1分钟
  1. Node.js使用 mongodb 这个驱动库时,默认不会使用从节点分担读请求,只有设置 readPreference=secondary 或者 readPreference=secondaryPreferred 后才能让从节点分担读请求。

mongodb://localhost:2001,localhost:2002/[dbname]?replicaSet=[rsname]&readPreference=secondaryPreferred

  1. 当数据量比较少时,有可能不会使用索引。
  2. mongodb 数据查询可以使用 db.tbl_name.find().explain() 查看索引命中情况
  3. mongodb 慢查询,可以分析查询速度大于 xx 毫秒的操作
  4. 当使用 write concern 时,仲裁节点不会算在里面。 官方文档原文:

Write concern for replica sets describe the number of data-bearing members (i.e. the primary and secondaries, but not arbiters) that must acknowledge a write operation before the operation returns as successful. A member can only acknowledge a write operation after it has received and applied the write successfully.

For replica sets, the default write concern of w: 1 requires that only the primary replica set member acknowledge the write before returning write concern acknowledgment. You can specify an integer value greater than 1 to require acknowledgment from the primary and as many secondaries as needed to meet the specified value, up to the total number of data-bearing members in the replica set.