[mongoDB] 레플리카 셋(Replica Set) 설정
트렌젝션을 사용하기 위해선 mongoDB 서버에 반드시 Replica Set을 설정해야 한다. 아주 기본적인 레플리카 셋을 설정하는 법을 알아보자.
Docker에서 세개의 컨테이너에 각각 primary, secondary, arbiter를 만드는 것에 대해 문제가 있었다. (보안, 접근 이슈)
하나의 컨테이너 안, 세개의 포트에 primary, secondary, arbiter를 만드는 법에 대해서 알아보겠다.
1. 세개(primary, secondary, arbiter)의 mongoDB 서버가 필요하다.
mongoDB의 명령어(mongod, mongo)는 기본적으로 27017포트가 default값으로 사용되게 설정되어있다. 따라서 configuration을 반드시 적용해주어야 한다.
configuration 파일을을 정의해야한다.
각 서버마다 다른 설정이 필요하므로 파일 이름을 구분해주자
mongod.primary.conf
mongod.secondary.conf
mongod.arbiter.conf
vi /etc/mongod.primary.conf
## 차례대로 모두 작성해주자
vi /etc/mongod.secondary.conf
vi /etc/mongod.arbiter.conf
설정값은
## /etc/mongod.primary.conf
systemLog:
destination: file
path: "/var/log/mongodb/mongod.primary.log"
logAppend: true
storage:
dbPath: "/data/db"
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 9900
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "rs-test"
이다.
여기서 서버마다 구분되는 값들은
systemLog.path
stoarage.dbPath
net.port
다.
이제 mongod.primary.conf 설정 파일을 가지고 mongoDB 데몬을 실행 시키자.
mongod --config /etc/mongod.primary.conf
위와 같은 설정/실행을 나머지 서버
secondary, arbiter 서버에 모두 적용시켜주자
secondary 서버 포트를 9901,
arbiter 서버 포트를 9902로 지정했다
2. 레플리카 셋 설정하기
우선 mongo를 실행시키자
여기서 유의할점은 위에서 말한것 처럼 기본적으로 27017포트를 사용하게 되어있으므로 원래와 같이 mongo로만 실행시키면 안된다.
## primary 에서 모든 Replica Set 설정을 하므로 9900 포트로 접속
mongo --port 9900
위에 설정한 포트를 넣어 실행시키면 된다
그럼 9901 포트나 9902에 실행된 secondary, arbiter 서버에서는 설정을 안하는가?
=> 그렇다. primary서버에 접속해서 secondary와 arbiter를 추가해 줄 것이다.
=> 추가 후, secondary와 arbiter에 접속하면 자동으로 연결되어 있는 것을 확인 할 수 있을 것이다.
mongo가 정상적으로 실행되면
레플리카 셋 명령어를 입력하자
rs.initiate(
{
_id: "rs-test",
version: 1,
members: [
{ _id: 0, host : "127.0.0.1:9900" },
{ _id: 1, host : "127.0.0.1:9901" }
]
}
)
## initiate 실행시 test-rs:SECONDARY 가 출력될것이다
## 몇번 엔터를 눌러가면서 기다리면 test-rs:PRIMARY 로 돌아오고 생성이 완료 된다.
rs.addArb("127.0.0.1:9902")
3. 설정 확인
rs.status()
를 사용해 primary, secondary, arbiter 서버들이 잘 구성되었는지 확인 해준다.
members 필드의 stateStr 값에 "PRIMARY", "SECONDARY", "ARBITOR" 로 멤버 역할이 나온다
https://www.mongodb.com/docs/manual/reference/configuration-options/
Configuration File Options — MongoDB Manual
Docs Home → MongoDB Manual The following page describes the configuration options available in MongoDB 6.0. For configuration file options for other versions of MongoDB, see the appropriate version of the MongoDB Manual.You can configure mongod and mongo
www.mongodb.com
Setup MongoDb ReplicaSet with Authentication enabled, using docker-compose.
The solution is intentionally toned down to a basic POC (proof of concept) style to fit for all kinds of users. Please do not use my…
prashix.medium.com
https://www.mongodb.com/docs/manual/reference/method/rs.initiate/