Sharding Cluster 만들기(5) : Sharding Cluster 초기화 및 설정

arky uhm·2025년 5월 23일

MongoDB

목록 보기
12/14

1. Config Server Replica Set 초기화

# docker exec -it config1 mongosh --tls --tlsCAFile /etc/ssl/mongodb/ca_sharded.pem --tlsCertificateKeyFile /etc/ssl/mongodb/mongodb_sharded.pem

test> rs.initiate(
...   {
...     _id: "configRS",
...     configsvr: true,
...     members: [
...       { _id: 0, host: "config1:27017" },
...       { _id: 1, host: "config2:27017" },
...       { _id: 2, host: "config3:27017" }
...     ]
...   }
... )
{ ok: 1 }
configRS [direct: secondary] test> rs.status()
{
  set: 'configRS',
  date: ISODate('2025-05-23T07:15:54.459Z'),
  myState: 2,
  term: Long('0'),
  syncSourceHost: '',
  syncSourceId: -1,
  configsvr: true,
  heartbeatIntervalMillis: Long('2000'),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
    lastCommittedWallTime: ISODate('2025-05-23T07:15:47.021Z'),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
    appliedOpTime: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
    durableOpTime: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
    writtenOpTime: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
    lastAppliedWallTime: ISODate('2025-05-23T07:15:47.021Z'),
    lastDurableWallTime: ISODate('2025-05-23T07:15:47.021Z'),
    lastWrittenWallTime: ISODate('2025-05-23T07:15:47.021Z')
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1747984547, i: 1 }),
  members: [
    {
      _id: 0,
      name: 'config1:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 1662,
      optime: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
      optimeDate: ISODate('2025-05-23T07:15:47.000Z'),
      optimeWritten: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
      optimeWrittenDate: ISODate('2025-05-23T07:15:47.000Z'),
      lastAppliedWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      lastDurableWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      lastWrittenWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      configVersion: 1,
      configTerm: 0,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'config2:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 7,
      optime: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
      optimeDurable: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
      optimeWritten: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
      optimeDate: ISODate('2025-05-23T07:15:47.000Z'),
      optimeDurableDate: ISODate('2025-05-23T07:15:47.000Z'),
      optimeWrittenDate: ISODate('2025-05-23T07:15:47.000Z'),
      lastAppliedWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      lastDurableWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      lastWrittenWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      lastHeartbeat: ISODate('2025-05-23T07:15:54.102Z'),
      lastHeartbeatRecv: ISODate('2025-05-23T07:15:54.300Z'),
      pingMs: Long('0'),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      configVersion: 1,
      configTerm: 0
    },
    {
      _id: 2,
      name: 'config3:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 7,
      optime: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
      optimeDurable: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
      optimeWritten: { ts: Timestamp({ t: 1747984547, i: 1 }), t: Long('-1') },
      optimeDate: ISODate('2025-05-23T07:15:47.000Z'),
      optimeDurableDate: ISODate('2025-05-23T07:15:47.000Z'),
      optimeWrittenDate: ISODate('2025-05-23T07:15:47.000Z'),
      lastAppliedWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      lastDurableWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      lastWrittenWallTime: ISODate('2025-05-23T07:15:47.021Z'),
      lastHeartbeat: ISODate('2025-05-23T07:15:54.102Z'),
      lastHeartbeatRecv: ISODate('2025-05-23T07:15:54.336Z'),
      pingMs: Long('0'),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      configVersion: 1,
      configTerm: 0
    }
  ],
  ok: 1
}
configRS [direct: secondary] test>

configRS [direct: primary] test>
rs.initiate(
  {
    _id: "configRS",
    configsvr: true,
    members: [
      { _id: 0, host: "config1:27017" },
      { _id: 1, host: "config2:27017" },
      { _id: 2, host: "config3:27017" }
    ]
  }
)

2. Shard Replica Set 초기화 및 샤드 추가

# docker exec -it shard1_a mongosh --tls --tlsCAFile /etc/ssl/mongodb/ca_sharded.pem --tlsCertificateKeyFile /etc/ssl/mongodb/mongodb_sharded.pem
test> rs.initiate(
...   {
...     _id: "shard1RS",
...     members: [
...       { _id: 0, host: "shard1_a:27017" },
...       { _id: 1, host: "shard1_b:27017" },
...       { _id: 2, host: "shard1_c:27017" }
...     ]
...   }
... )
{ ok: 1 }
shard1RS [direct: primary] test> rs.status()
{
  set: 'shard1RS',
  date: ISODate('2025-05-23T07:21:34.211Z'),
  myState: 1,
  term: Long('1'),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long('2000'),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
    lastCommittedWallTime: ISODate('2025-05-23T07:21:30.063Z'),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
    appliedOpTime: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
    durableOpTime: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
    writtenOpTime: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
    lastAppliedWallTime: ISODate('2025-05-23T07:21:30.063Z'),
    lastDurableWallTime: ISODate('2025-05-23T07:21:30.063Z'),
    lastWrittenWallTime: ISODate('2025-05-23T07:21:30.063Z')
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1747984878, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate('2025-05-23T07:21:29.727Z'),
    electionTerm: Long('1'),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1747984878, i: 1 }), t: Long('-1') },
    lastSeenWrittenOpTimeAtElection: { ts: Timestamp({ t: 1747984878, i: 1 }), t: Long('-1') },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1747984878, i: 1 }), t: Long('-1') },
    numVotesNeeded: 2,
    priorityAtElection: 1,
    electionTimeoutMillis: Long('10000'),
    numCatchUpOps: Long('0'),
    newTermStartDate: ISODate('2025-05-23T07:21:29.847Z'),
    wMajorityWriteAvailabilityDate: ISODate('2025-05-23T07:21:30.291Z')
  },
  members: [
    {
      _id: 0,
      name: 'shard1_a:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 2002,
      optime: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
      optimeDate: ISODate('2025-05-23T07:21:30.000Z'),
      optimeWritten: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
      optimeWrittenDate: ISODate('2025-05-23T07:21:30.000Z'),
      lastAppliedWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      lastDurableWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      lastWrittenWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: 'Could not find member to sync from',
      electionTime: Timestamp({ t: 1747984889, i: 1 }),
      electionDate: ISODate('2025-05-23T07:21:29.000Z'),
      configVersion: 1,
      configTerm: 1,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'shard1_b:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 15,
      optime: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
      optimeDurable: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
      optimeWritten: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
      optimeDate: ISODate('2025-05-23T07:21:30.000Z'),
      optimeDurableDate: ISODate('2025-05-23T07:21:30.000Z'),
      optimeWrittenDate: ISODate('2025-05-23T07:21:30.000Z'),
      lastAppliedWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      lastDurableWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      lastWrittenWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      lastHeartbeat: ISODate('2025-05-23T07:21:33.773Z'),
      lastHeartbeatRecv: ISODate('2025-05-23T07:21:32.775Z'),
      pingMs: Long('0'),
      lastHeartbeatMessage: '',
      syncSourceHost: 'shard1_a:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 1,
      configTerm: 1
    },
    {
      _id: 2,
      name: 'shard1_c:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 15,
      optime: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
      optimeDurable: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
      optimeWritten: { ts: Timestamp({ t: 1747984890, i: 3 }), t: Long('1') },
      optimeDate: ISODate('2025-05-23T07:21:30.000Z'),
      optimeDurableDate: ISODate('2025-05-23T07:21:30.000Z'),
      optimeWrittenDate: ISODate('2025-05-23T07:21:30.000Z'),
      lastAppliedWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      lastDurableWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      lastWrittenWallTime: ISODate('2025-05-23T07:21:30.063Z'),
      lastHeartbeat: ISODate('2025-05-23T07:21:33.772Z'),
      lastHeartbeatRecv: ISODate('2025-05-23T07:21:32.769Z'),
      pingMs: Long('0'),
      lastHeartbeatMessage: '',
      syncSourceHost: 'shard1_a:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 1,
      configTerm: 1
    }
  ],
  ok: 1
}
shard1RS [direct: primary] test>

# docker exec -it mongos1 mongosh --tls --tlsCAFile /etc/ssl/mongodb/ca_sharded.pem --tlsCertificateKeyFile /etc/ssl/mongodb/mongodb_sharded.pem
[direct: mongos] test> sh.addShard( "shard1RS/shard1_a:27017,shard1_b:27017,shard1_c:27017" )
{
  shardAdded: 'shard1RS',
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1747984973, i: 20 }),
    signature: {
      hash: Binary.createFromBase64('+kzcDqmXQJMbQxHJsfA0mkxdY3g=', 0),
      keyId: Long('7507536506228047895')
    }
  },
  operationTime: Timestamp({ t: 1747984973, i: 20 })
}

rs.initiate(
  {
    _id: "shard1RS",
    members: [
      { _id: 0, host: "shard1_a:27017" },
      { _id: 1, host: "shard1_b:27017" },
      { _id: 2, host: "shard1_c:27017" }
    ]
  }
)

3. 관리자 사용자 생성 및 권한 설정

[direct: mongos] test> use admin
switched to db admin
[direct: mongos] admin> db.createUser(
...   {
...     user: "admin",
...     pwd: "****",
...     roles: [ { role: "root", db: "admin" }]
...   }
... )
{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1747985155, i: 4 }),
    signature: {
      hash: Binary.createFromBase64('BRv2smKgKrkiaxcnPY96CxkrXyM=', 0),
      keyId: Long('7507536506228047895')
    }
  },
  operationTime: Timestamp({ t: 1747985155, i: 4 })
}
db.createUser( 
  { 
    user: "admin", 
    pwd: "****", 
	roles: [ { role: "root", db: "admin" }] 
  } 
)

0개의 댓글