§2024-05-29

Setting up an SRV (Service) record for a MongoDB replica set involves configuring your DNS (Domain Name System) to include specific details about your MongoDB instances. This enables MongoDB clients to automatically discover and connect to the members of the replica set.

I had a ~/.mongosh.js@hc4Jammy.yushei.com.tw as,

// connect to relicationSet odroid01
const db = connect("mongodb://siteRootAdmin:b23258585@x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/YuSheiDBTest?replicaSet=odroid01&authSource=admin");

And I am going to setup the SRV as,

inLaneCatch.yushei.com.tw. 3600 IN SRV 0 0 27017 x8664Arch.yushei.com.tw.
inLaneCatch.yushei.com.tw. 3600 IN SRV 0 0 27017 hc4Jammy.yushei.com.tw.
inLaneCatch.yushei.com.tw. 3600 IN SRV 0 0 27017 n2Jammy.yushei.com.tw.
  1. set up DNS srv record

inLaneCatch-srv-01.png inLaneCatch-srv-02.png inLaneCatch-srv-03.png

  1. check
alexlai@MacMini Downloads % dig +short SRV _mongodb._tcp.inLaneCatch.yushei.com.tw
0 0 27017 hc4Jammy.yushei.com.tw.
0 0 27017 x8664Arch.yushei.com.tw.
0 0 27017 n2Jammy.yushei.com.tw.
  1. Try connect without TLS, default is using TLS

+srv TLS Behavior When you use the +srv connection string modifier, MongoDB automatically sets the --tls connection option to true. To override this behavior, set --tls to false.

alexlai@hc4Jammy:~$ mongosh "mongodb+srv://siteRootAdmin:b23258585@inLaneCatch.yushei.com.tw/YuSheiDBTest?replicaSet=odroid01&authSource=admin&tls=false"Current Mongosh Log ID: 6656555d897add247618d3ce
Connecting to:          mongodb+srv://<credentials>@inLaneCatch.yushei.com.tw/YuSheiDBTest?replicaSet=odroid01&authSource=admin&tls=false&appName=mongosh+1.10.1
Using MongoDB:          7.0.0-rc8
Using Mongosh:          1.10.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2024-05-26T08:27:17.654+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
------

┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
│ (index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
│    0    │  0  │ 'hc4Jammy.yushei.com.tw:27017'  │   1    │   2   │ 'SECONDARY' │
│    1    │  1  │  'N2Jammy.yushei.com.tw:27017'  │   1    │   2   │ 'SECONDARY' │
│    2    │  2  │ 'x8664Arch.yushei.com.tw:27017' │   1    │   1   │  'PRIMARY'  │
└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Wed May 29 2024 06:06:25 GMT+0800 (Taipei Standard Time)

¶ What will happen, when for exampke n2Bookworm.yushei.com.tw is in this _mongodb._tcp.inLaneCatch.yushei.com.tw, but not add into the replcationSet yet?

inLaneCatch-srv-04.png

alexlai@opi58G:~$ dig +short SRV _mongodb._tcp.inLaneCatch.yushei.com.tw
0 0 27017 hc4Jammy.yushei.com.tw.
0 0 27017 x8664Arch.yushei.com.tw.
0 0 27017 n2Boookworm.yushei.com.tw.
0 0 27017 n2Jammy.yushei.com.tw.

So, even the n2Bookwork.yushei.com.tw

alexlai@hc4Jammy:~$ mongosh "mongodb+srv://siteRootAdmin:b23258585@inLaneCatch.yushei.com.tw/YuSheiDBTest?replicaSet=odroid01&authSource=admin&tls=false"
Current Mongosh Log ID:	66566ad2b64477e3c66cf07b
Connecting to:		mongodb+srv://<credentials>@inLaneCatch.yushei.com.tw/YuSheiDBTest?replicaSet=odroid01&authSource=admin&tls=false&appName=mongosh+1.10.1
Using MongoDB:		7.0.0-rc8
Using Mongosh:		1.10.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2024-05-26T08:27:17.654+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
------

┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
│ (index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
│    0    │  0  │ 'hc4Jammy.yushei.com.tw:27017'  │   1    │   2   │ 'SECONDARY' │
│    1    │  1  │  'N2Jammy.yushei.com.tw:27017'  │   1    │   2   │ 'SECONDARY' │
│    2    │  2  │ 'x8664Arch.yushei.com.tw:27017' │   1    │   1   │  'PRIMARY'  │
└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Wed May 29 2024 07:37:57 GMT+0800 (Taipei Standard Time)
alexlai@n2Bookworm:/etc$ mongosh "mongodb+srv://siteRootAdmin:b23258585@inLaneCatch.yushei.com.tw/YuSheiDBTest?replicaSet=odroid01&authSource=admin&tls=false"
Current Mongosh Log ID:	66567c604485362fcf8db5fa
Connecting to:		mongodb+srv://<credentials>@inLaneCatch.yushei.com.tw/YuSheiDBTest?replicaSet=odroid01&authSource=admin&tls=false&appName=mongosh+2.2.6
Using MongoDB:		7.0.0-rc8
Using Mongosh:		2.2.6

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2024-05-26T08:27:17.654+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
------

odroid01 [primary] YuSheiDBTest> rs.conf()
{
  _id: 'odroid01',
  version: 10,
  term: 115,
  members: [
    {
      _id: 0,
      host: 'hc4Jammy.yushei.com.tw:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long('0'),
      votes: 1
    },
    {
      _id: 1,
      host: 'N2Jammy.yushei.com.tw:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long('0'),
      votes: 1
    },
    {
      _id: 2,
      host: 'x8664Arch.yushei.com.tw:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long('0'),
      votes: 1
    }
  ],
  protocolVersion: Long('1'),
  writeConcernMajorityJournalDefault: true,
  settings: {
    chainingAllowed: true,
    heartbeatIntervalMillis: 2000,
    heartbeatTimeoutSecs: 10,
    electionTimeoutMillis: 10000,
    catchUpTimeoutMillis: -1,
    catchUpTakeoverDelayMillis: 30000,
    getLastErrorModes: {},
    getLastErrorDefaults: { w: 1, wtimeout: 0 },
    replicaSetId: ObjectId('64be6b28c8d241793d99cb2f')
  }
}
odroid01 [primary] YuSheiDBTest> rs.add( { host: "mongodb3.example.net:27017" } )
{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1716944162, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('R9LQuC5BviVsMFrqTzvcXcvuQlA=', 0),
      keyId: Long('7326325939019710465')
    }
  },
  operationTime: Timestamp({ t: 1716944162, i: 1 })
}
odroid01 [primary] YuSheiDBTest> rs.add( { host: "n2Bookworm.yushei.com.tw:27017" } )
{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1716944202, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('6FZs4/+qb04h3kFt37iGT+RDjCc=', 0),
      keyId: Long('7326325939019710465')
    }
  },
  operationTime: Timestamp({ t: 1716944202, i: 1 })
}
odroid01 [primary] YuSheiDBTest> rs.remove("mongodb3.example.net:27017", {force: true})
{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1716944309, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('xregj9TaTERmf0VV9GMwg/CERrY=', 0),
      keyId: Long('7326325939019710465')
    }
  },
  operationTime: Timestamp({ t: 1716944309, i: 1 })
}
odroid01 [primary] YuSheiDBTest> rs.conf()
{
  _id: 'odroid01',
  version: 13,
  term: 115,
  members: [
    {
      _id: 0,
      host: 'hc4Jammy.yushei.com.tw:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long('0'),
      votes: 1
    },
    {
      _id: 1,
      host: 'N2Jammy.yushei.com.tw:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long('0'),
      votes: 1
    },
    {
      _id: 2,
      host: 'x8664Arch.yushei.com.tw:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long('0'),
      votes: 1
    },
    {
      _id: 4,
      host: 'n2Bookworm.yushei.com.tw:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long('0'),
      votes: 1
    }
  ],
  protocolVersion: Long('1'),
  writeConcernMajorityJournalDefault: true,
  settings: {
    chainingAllowed: true,
    heartbeatIntervalMillis: 2000,
    heartbeatTimeoutSecs: 10,
    electionTimeoutMillis: 10000,
    catchUpTimeoutMillis: -1,
    catchUpTakeoverDelayMillis: 30000,
    getLastErrorModes: {},
    getLastErrorDefaults: { w: 1, wtimeout: 0 },
    replicaSetId: ObjectId('64be6b28c8d241793d99cb2f')
  }
}
odroid01 [primary] YuSheiDBTest>