본문으로 건너뛰기

몽고디비 클러스터 구성하기

MongoDB 구성

MongoDB Configuration

호스트 및 MongoDB 인스턴스에 대한 예기치 못한 문제로 인하여 프로세스가 down되는 등 장애 상황이 발생하거나 데이터 유실이 발생 함

이를 대비하기 위해 여러 다른 종류의 DBMS와 비슷하게 MongoDB 또한 복제 구성을 통한 DB HA(High Availability) 사용 해야 함

이렇게 복제 구성된 그룹은 Replica Set이라 하며, 나아가 다수의 Replica Set을 함께 구성하여 쿼리의 분산 처리와 Scale out에 유리하게 구성한 형태를 Sharded Cluster 라고 부름

Unexpected issues with the host or MongoDB instance can cause failures such as process downtime or data loss.

To prepare for this, similar to various other types of DBMS, MongoDB also requires DB HA (High Availability) through replication configuration.

A group configured with replication is called a Replica Set, and furthermore, a configuration that combines multiple Replica Sets to facilitate distributed query processing and scale out is called a Sharded Cluster.

PSS / PSA

https://docs.mongodb.com/manual/replication/

image

(from ncloud MongoDB Cluster)

클러스터 구성하기

Setting Up the Cluster

설정파일 수정하기

Modifying the Configuration File

  • 세팅을 수정하지 않았다면, 기본적으로 Linux상의 MongoDB는 /etc/mongod.conf 에 설정값들이 있음
  • 아래의 내용을 설정파일에 넣고 수정이 필요
  • If you haven't modified the settings, by default MongoDB on Linux has its configuration values in /etc/mongod.conf
  • You need to add and modify the following content in the configuration file

[중요] replica set 구성은 "security: enable" 상태에서는 설정되지 않는다 → 나중에 key로 대체

[IMPORTANT] Replica set configuration cannot be set when "security: enable" is active -> This will be replaced with a key later

# /etc/mongod.conf
replication:
replSetName: "mobidicSet"
  • 설정 된 replSetName은 구성하고자 하는 RS(레플리카셋)에 모두 동일하게 적용해야 한다
  • 설정 적용 후, 몽고디비를 재시작 해 주어야 한다
  • The configured replSetName must be applied identically to all RS (Replica Sets) you want to configure
  • After applying the settings, you need to restart MongoDB
sudo systemctl stop mongod
sudo systemctl start mongod
sudo systemctl status mongod

TIPS

[몽고db 클러스터끼리 통신이 안될 때]

  • ec2의 인바운드 그룹을 확인하자
  • 해당 ec2들이 보안그룹에 묶여있다면 가장 편하다
  • ICMP 와 TCP 27017 모두 풀어주어야 한다
  • 돌다리도 두들겨보고 건너라고, telnet이나 ping으로 확인 해 준다

[When MongoDB clusters cannot communicate with each other]

  • Check the inbound rules of your EC2 instances
  • It's easiest if the EC2 instances are grouped in the same security group
  • Both ICMP and TCP 27017 need to be allowed
  • As the saying goes, "look before you leap" - verify with telnet or ping

[몽고db 클러스터가 잘 뜨지 않을 경우]

  • /data/db가 생성되어있지 않는 경우 → sudo mkdir /data/db 로 해결
  • /data/db 가 권한이 없는 경우 → sudo chown mongodb: /data/db 로 해결
  • systemctl status mongod 로 확인 했을 때 status가 fail → 출력되고있는 화면에 에러 이유가 잘 나와있다
  • authorized 문제가 생길 경우, 위에 적힌 내용을 잘 안읽은것이므로 반성한 후에 /etc/mongod.conf 의 security쪽을 주석처리 해주고 큰 한숨을 쉬고 다시 시도한다

[When MongoDB cluster doesn't start properly]

  • If /data/db is not created -> Solve with sudo mkdir /data/db
  • If /data/db has no permissions -> Solve with sudo chown mongodb: /data/db
  • If status shows fail when checking with systemctl status mongod -> The error reason is clearly shown on the output screen
  • If authorization issues occur, it means you didn't read the instructions above carefully, so reflect on that, comment out the security section in /etc/mongod.conf, take a deep sigh, and try again

클러스터 설정하기

Configuring the Cluster

위 부분까지 세팅이 잘 되었다면 Replica Set을 이어주어야 한다

If the setup up to this point has been done correctly, you need to connect the Replica Set

  1. mongo shell 접속
  1. Connect to mongo shell
mongo
  1. rs세팅
  1. RS configuration
rs.initiate({
_id : "<Replica Set 명>",
members : [
{_id : 0, host : "<BindIp>:<Port>"},
{_id : 1, host : "<BindIp>:<Port>"},
{_id : 2, host : "<BindIp>:<Port>"},
]
})
  1. 위의 형태로 작성하고, 주의 할 점은 IP,PORT가 모두 ""(큰따옴표) 안에 들어 가 있어야 한다

    ex)

  1. Write in the format above, and note that IP and PORT must all be enclosed in "" (double quotes)

    ex)

rs.initiate({
_id : "mobidicSet",
members : [
{_id : 0, host : "1.2.3.4:27017"},
{_id : 1, host : "3.2.1.4:27017"},
{_id : 2, host : "4.3.1.2:27017"},
]
})
  1. 결과 확인하기

    잘 이어졌다면 몽고디비 shell에 PRIMARY 혹은 SECONDARY라고 뜬다

  1. Verify the results

    If connected successfully, PRIMARY or SECONDARY will appear in the MongoDB shell

rs.status()