Primary, Secondary 바꾸기

arky uhm·2025년 5월 12일

MongoDB

목록 보기
1/14

Primary를 mongo3 에서 mongo1으로 변경

✅ 현재 Replica set에서 Primary, Secondary 상태 확인

bash# docker exec -it mongo3 mongosh  ## 현재 primary DB인 mongo3로 접속
Current Mongosh Log ID: 68217d675e12ec42cad861df
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.0
Using MongoDB:          8.0.9
Using Mongosh:          2.5.0

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

------
   The server generated these startup warnings when booting
   2025-05-12T04:12:42.589+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2025-05-12T04:12:42.589+00:00: For customers running the current memory allocator, we suggest changing the contents of the following sysfsFile
   2025-05-12T04:12:42.589+00:00: We suggest setting the contents of sysfsFile to 0.
   2025-05-12T04:12:42.590+00:00: vm.max_map_count is too low
   2025-05-12T04:12:42.590+00:00: We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.
------

myReplicaSet [direct: primary] test> rs.status()
...
  members: [
    {
      _id: 0,
      name: 'mongo1:27017',
      health: 1,
      state: 1,
      stateStr: 'SECONDARY',
...
    },
    {
      _id: 1,
      name: 'mongo2:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
...
    },
    {
      _id: 2,
      name: 'mongo3:27017',
      health: 1,
      state: 2,
      stateStr: 'PRIMARY',  ## mongo3가 primary인 상태
...
  operationTime: Timestamp({ t: 1747025254, i: 1 })
}
myReplicaSet [direct: primary] test> 


#### ✅ mongo1의 우선순위(priority) 확인 및 조정
/* in primary DB mongosh
cfg = rs.conf()
cfg.members[0].priority = 2   ## mongo1의 priority를 2로 설정 (다른 노드보다 높게)
cfg.members[1].priority = 1  ## mongo2, mongo3은 낮게 유지
cfg.members[2].priority = 1
rs.reconfig(cfg)
*/
myReplicaSet [direct: primary] test> cfg = rs.conf()
... cfg.members[0].priority = 2
... cfg.members[1].priority = 1
... cfg.members[2].priority = 1
... rs.reconfig(cfg)
{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1747028081, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
      keyId: Long('0')
    }
  },
  operationTime: Timestamp({ t: 1747028081, i: 1 })
}
myReplicaSet [direct: primary] test> rs.status()
...
  members: [
    {
      _id: 0,
      name: 'mongo1:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',  ## mongo1이 primary인 상태

...
    {
      _id: 1,
      name: 'mongo2:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',

...
    {
      _id: 2,
      name: 'mongo3:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
...
  operationTime: Timestamp({ t: 1747028170, i: 1 })
}
myReplicaSet [direct: secondary] test>  ## mongo3 DB의 prompt가 primary에서 secondary로 바뀜

bash# docker exec -it mongo1 mongosh
Current Mongosh Log ID: 68218983386385ba50d861df
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.0
Using MongoDB:          8.0.9
Using Mongosh:          2.5.0

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

------
   The server generated these startup warnings when booting
   2025-05-12T05:35:00.309+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2025-05-12T05:35:00.309+00:00: For customers running the current memory allocator, we suggest changing the contents of the following sysfsFile
   2025-05-12T05:35:00.309+00:00: We suggest setting the contents of sysfsFile to 0.
   2025-05-12T05:35:00.309+00:00: vm.max_map_count is too low
   2025-05-12T05:35:00.309+00:00: We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.
------

myReplicaSet [direct: primary] test>

※ 주의 : Secondary에서 우선순위 조정 시 오류가 발생하므로 primary에서 우선순위를 조정함
myReplicaSet [direct: secondary] test> cfg = rs.conf()
... cfg.members[0].priority = 1
... cfg.members[1].priority = 1
... cfg.members[2].priority = 1
... rs.reconfig(cfg)
MongoServerError[NotWritablePrimary]: New config is rejected :: caused by :: replSetReconfig should only be run on a writable PRIMARY. Current state SECONDARY;


0개의 댓글