Configure Replica Set Tag Sets配置副本集标记集

On this page本页内容

A replica set member or members can be configured with tags:副本集成员可以使用tags进行配置:

{ "<tag1>": "<string1>", "<tag2>": "<string2>",... }

For read operations, you can specify a tag set in the read preferences to help direct read operations to members that have specific tag(s).对于读取操作,可以在读取首选项中指定标记集,以帮助将读取操作定向到具有特定标记的成员。

For write operations, you can use the tags to create a custom write concern.对于写操作,可以使用标记创建自定义的写关注点

Use Tag Sets in Read Preference在读取首选项中使用标记集

If a replica set member or members are associated with tags, you can specify a tag set in the read preference to target those members. 如果副本集成员与tags关联,则可以在读取首选项中指定标记集,以将这些成员作为目标。A tag set is an array of documents, where each document contains the tag and value pair(s). 标记集是一个文档数组,其中每个文档都包含标记和值对。The specifications are tried in order until a match is found. 按顺序尝试规格,直到找到匹配项。Once found, that specification is used to find all eligible matching members.一旦找到,该规范将用于查找所有符合条件的匹配成员。

Note注意

You cannot specify a tag set when specifying read preference mode primary.指定读取首选项模式primary时,不能指定标记集。

For example, a replica set has the following replica set configuration (some of the fields have been omitted for brevity):例如,副本集具有以下副本集配置(为简洁起见,省略了一些字段):

{
   "_id" : "rs0",
   "version" : 1,
   "protocolVersion" : NumberLong(1),
   "writeConcernMajorityJournalDefault" : true,
   "members" : [
       { "_id" : 0, "host" : "mongodb0.example.net:27017", ...,  "tags": { }, ... },
       { "_id" : 1, "host" : "mongodb1.example.net:27017", ...,  "tags": { }, ... },
       { "_id" : 2, "host" : "mongodb2.example.net:27017", ...,  "tags": { }, ... }
   ],
   "settings" : {
      ...
   }
}
  1. Add tags to the members.向成员添加标记。

    Connect mongosh to the replica set and use rs.reconfig() to add tags to the members:mongosh连接到副本集,并使用rs.reconfig()为成员添加标签:

    conf = rs.conf();
    conf.members[0].tags = { "dc": "east", "usage": "production" };
    conf.members[1].tags = { "dc": "east", "usage": "reporting" };
    conf.members[2].tags = { "dc": "west", "usage": "production" };
    rs.reconfig(conf);
  2. Verify the replica set configuration.验证复制集配置。

    Run rs.conf() to verify the replica set configuration (some of the fields have been omitted for brevity). 运行rs.conf()来验证副本集配置(为简洁起见,省略了一些字段)。The rs.conf() returns a document similar to the following:rs.conf()返回一个类似于以下内容的文档:

    {
       "_id" : "rs0",
       "version" : 2,
       "protocolVersion" : NumberLong(1),
       "writeConcernMajorityJournalDefault" : true,
       "members" : [
          {
              "_id" : 0,
              "host" : "mongodb0.example.net:27017",
              ...
              "tags" : {
                  "dc": "east",
                  "usage": "production"
              },
              ...
          },
          {
              "_id" : 1,
              "host" : "mongodb1.example.net:27017",
              ...
              "tags" : {
                  "dc": "east",
                  "usage": "reporting"
              },
              ...
           },
          {
              "_id" : 2,
              "host" : "mongodb2.example.net:27017",
              ...
              "tags" : {
                  "dc": "west",
                  "usage": "production"
              },
              ...
          }
       ],
       "settings" : {
          ...
       }
    }
  3. Specify tag sets in the read preference.在读取首选项中指定标记集。

    To direct read operations to the secondaries tagged with a particular tag(s), in the mongo shell connected to the replica set, you can use the readPref() method to specify the read preference mode and the tag set. For example,要将读取操作定向到带有特定标记的二级,在连接到副本集的mongo shell中,可以使用readPref()方法指定读取首选项模式标记集。例如

    • To direct read operations to the secondary tagged with both"dc": "east" and "usage": "production", include the following tag set:要将读取操作定向到标记为"dc": "east""usage": "production"的辅助设备,请包括以下标记集:

      db.collection.find({}).readPref( "secondary", [ { "dc": "east", "usage": "production" } ] )
    • To direct a read operation to the secondaries tagged with "dc":"east", and if not found, to secondaries tagged with "usage": "production", include the following tag set:要将读取操作定向到标记为"dc":"east"的辅助设备,如果找不到,则定向到标记有"usage": "production"的辅助服务器,请包括以下标记集:

      db.collection.find({}).readPref( "secondary", [ { "dc": "east"}, { "usage": "production" } ] )
    Tip提示
    See also: 参阅:

Custom Multi-Datacenter Write Concerns自定义多数据中心写入问题

If a replica set member or members are associated with tags, you can configure the replica set's settings.getLastErrorModes setting to create a custom write concern.如果副本集成员与tags关联,则可以配置副本集的settings.getLastErrorModes设置以创建自定义写入问题。

Given a five member replica set with members in two data centers:给定一个包含两个数据中心中成员的五成员副本集:

  1. a facility VA tagged dc_va标记为dc_VA的设施VA
  2. a facility CA tagged dc_ca标记为dc_CA的设施CA
{
   "_id" : "rs0",
   "version" : 1,
   "protocolVersion" : NumberLong(1),
   "writeConcernMajorityJournalDefault" : true,
   "members" : [
       { "_id" : 0, "host" : "mongodb0.example.net:27017", ...,  "tags": { }, ... },
       { "_id" : 1, "host" : "mongodb1.example.net:27017", ...,  "tags": { }, ... },
       { "_id" : 2, "host" : "mongodb2.example.net:27017", ...,  "tags": { }, ... }
       { "_id" : 3, "host" : "mongodb3.example.net:27017", ...,  "tags": { }, ... }
       { "_id" : 4, "host" : "mongodb4.example.net:27017", ...,  "tags": { }, ... }
   ],
   "settings" : {
      ...
   }
}
  1. Add tags to the replica set members.向副本集成员添加标记。

    Connect mongosh to the replica set and use rs.reconfig() to add tags to the members:mongosh连接到副本集,并使用rs.reconfig()为成员添加标签:

    conf = rs.conf();
    conf.members[0].tags = { "dc_va": "rack1"};
    conf.members[1].tags = { "dc_va": "rack2"};
    conf.members[2].tags = { "dc_ca": "rack1"};
    conf.members[3].tags = { "dc_ca": "rack2"};
    conf.members[4].tags = { "dc_va": "rack1"};
    rs.reconfig(conf);
  2. Create a custom write concern.创建自定义写入关注点。

    In the replica set configuration, define a custom write concern in the settings.getLastErrorModes setting. 在副本集配置中,在settings.getLastErrorModes设置中定义自定义写入问题。For example, the following defines the custom write concern MultipleDC that requires the write to propagate to two members with differentdc_va tag values and one member with any dc_ca tag value.例如,下面定义了自定义写入关注点MultipleDC,该关注点要求将写入传播到两个具有不同dc_va标记值的成员,以及一个具有任何dc_ca标记值。

    conf = rs.conf();
    conf.settings = { getLastErrorModes: { MultipleDC : { "dc_va": 2, "dc_ca": 1 } } };
    rs.reconfig(conf);
    Note注意

    The MultipleDC write concern is not satisfied if the write propagates to two members with the same "dc_va" tag. 如果写入传播到具有相同"dc_va"标记的两个成员,则不满足MultipleDC写入问题。For example, if the write has only propagated to members[0] and members[4], "dc_va": 2 is not satisfied since they have the same tag value "rack1".例如,如果写入仅传播到members[0]members[4],则不满足"dc_va": 2,因为它们具有相同的标记值"rack1"

  3. Use the custom write concern.使用自定义写入问题。

    To use the custom write concern, pass in the write concern name to the w Option in the write concern:要使用自定义写入关注点,请将写入关注点名称传递给写入关注点中的w选项

    db.collection.insertOne(
       { id: "xyz", status: "A" },
       { writeConcern: { w: "MultipleDC" } }
    )
←  Resync a Member of a Replica SetReconfigure a Replica Set with Unavailable Members →