3 분 소요


MongoDB 구성

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

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

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

PSS / PSA

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

image

(from ncloud MongoDB Cluster)

클러스터 구성하기

설정파일 수정하기

  • 세팅을 수정하지 않았다면, 기본적으로 Linux상의 MongoDB는 /etc/mongod.conf 에 설정값들이 있음
  • 아래의 내용을 설정파일에 넣고 수정이 필요

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

# /etc/mongod.conf
replication:
  replSetName: "mobidicSet"
  • 설정 된 replSetName은 구성하고자 하는 RS(레플리카셋)에 모두 동일하게 적용해야 한다
  • 설정 적용 후, 몽고디비를 재시작 해 주어야 한다
sudo systemctl stop mongod
sudo systemctl start mongod
sudo systemctl status mongod

TIPS

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

  • ec2의 인바운드 그룹을 확인하자
  • 해당 ec2들이 보안그룹에 묶여있다면 가장 편하다
  • ICMP 와 TCP 27017 모두 풀어주어야 한다
  • 돌다리도 두들겨보고 건너라고, telnet이나 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쪽을 주석처리 해주고 큰 한숨을 쉬고 다시 시도한다

클러스터 설정하기

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

  1. mongo shell 접속

     mongo
    
  2. rs세팅

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

    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"},
         ]
     })
    
  4. 결과 확인하기

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

     rs.status()
    

MongoDB Configuration

Unexpected issues with hosts or MongoDB instances can cause process failures or data loss.

To prepare for such situations, similar to other DBMS types, MongoDB should also use DB HA (High Availability) through replication configuration.

A group configured with replication is called a Replica Set. Furthermore, configuring multiple Replica Sets together for distributed query processing and easier scale-out is called a Sharded Cluster.

PSS / PSA

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

image

(from ncloud MongoDB Cluster)

Setting Up a Cluster

Modifying the Configuration File

  • If you haven’t modified the settings, MongoDB on Linux has its configuration values in /etc/mongod.conf by default
  • Add and modify the following content in the configuration file

[IMPORTANT] Replica set configuration cannot be set when “security: enable” is active - replace with key later

# /etc/mongod.conf
replication:
  replSetName: "mobidicSet"
  • 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

[When MongoDB clusters cannot communicate with each other]

  • Check the EC2 inbound rules
  • It’s easiest if the EC2 instances are in the same security group
  • Both ICMP and TCP 27017 must be allowed
  • As the saying goes “look before you leap” - verify with telnet or ping

[When MongoDB cluster won’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 is fail when checking with systemctl status mongod - the error reason is well displayed on the output screen
  • If authorization issues occur, you didn’t read the content 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

If the setup above is complete, you need to connect the Replica Set

  1. Connect to mongo shell

     mongo
    
  2. RS configuration

     rs.initiate({
      _id : "<Replica Set Name>",
         members : [
           {_id : 0, host : "<BindIp>:<Port>"},
             {_id : 1, host : "<BindIp>:<Port>"},
             {_id : 2, host : "<BindIp>:<Port>"},
         ]
     })
    
  3. Write in the format above. 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"},
         ]
     })
    
  4. Verify the result

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

     rs.status()
    

댓글남기기