본문 바로가기
MongoDB

[4.2]MongoDB sharding shard 세팅하기

by 2세1의 행복한 개발 2020. 5. 26.
반응형

안녕하세요 오늘은 mongodb를 세팅하려고합니다

 

기본적인 서버는

router안에 config 3개를 넣고

나머지 두개 서버에 shard를 하나씩할 예정입니다

 

기본적으로 순서는

shard -> config -> router 순으로 합니다

shard,config는 뭘 먼저해도 상관은 없지만 router는 꼭 마지막에 해야합니다

두개의 설정을 묶는 역할이기 때문이죠

 

우선 ip로 세팅하는걸 싫어하기 때문에

/etc/hosts에 제가 사용할 아이피를 정의하겠습니다

(config server ip) mongo_config

(shard server ip) mongo_shard1
(shard server ip) mongo_shard2

를 추가하여 직접 아이피로 설정을 하는게 아닌 특정 명칭을 넣어 연결하겠습니다

저는 

폴더 구조를

mkdir /etc/mongo
mkdir /etc/mongo/conf
mkdir /etc/mongo/shard1
mkdir /etc/mongo/shard2
mkdir /etc/mongo/config1
mkdir /etc/mongo/config2
mkdir /etc/mongo/config3
mkdir /etc/mongo/mongos

conf 폴더는 몽고를 실행할 모든 conf가 모여있고

나머지는 각각 mongo가 실행될 database폴더입니다

각 서버에 폴더를 배분하여 실행하면 됩니다

 

[shard1번 서버] shard1 생성

vi /etc/mongo/conf/shard1.conf
storage:
  dbPath: /etc/mongo/shard1/

systemLog:
  path: "/etc/mongo/shard1/mongo.log"
  logAppend: true
  destination: file
  traceAllExceptions: false

processManagement:
  fork: true

replication:
  replSetName: "rs1"

sharding:
  clusterRole: shardsvr

net:
  bindIp: 0.0.0.0
  port: 20001

port는 자유롭게 설정하셔도 됩니다

여기서 중요한건 replSetName인데 몽고에서 사용할 해당 샤드의 명칭이라고 보시면 됩니다

shard1 쉘스크립트 작성

vi /etc/mongo/mongo.sh
#!/bin/bash

mongod -f /etc/mongo/conf/shard1.conf

shard1 실행

/etc/mongo/mongo.sh

확인

ps -ef | grep mongo
root      1865     1  8 May25 ?        00:02:34 mongod -f /etc/mongo/conf/shard1.conf
root      2084  1852  0 00:09 pts/0    00:00:00 grep --color=auto mongo

이렇게 정상적으로 실행된것을 확인하셨으면 접속을 테스트해야합니다

접속

mongo mongo_shard1:20001

접속이 된다면 다음단계로 넘어갈 수 있고

문제가 발생한다면 거의 방화벽 문제라고 보시면 됩니다

프로세스가 잘 떠있기 때문이죠

일부로 로컬이 아닌 ip로 외부접속한 이유는 외부서버들로 서버를 구성하기 때문입니다

[shard2번 서버] shard2 생성

vi /etc/mongo/conf/shard2.conf
storage:
  dbPath: /etc/mongo/shard2/

systemLog:
  path: "/etc/mongo/shard2/mongo.log"
  logAppend: true
  destination: file
  traceAllExceptions: false

processManagement:
  fork: true

replication:
  replSetName: "rs1"

sharding:
  clusterRole: shardsvr

net:
  bindIp: 0.0.0.0
  port: 20002

shard2.conf만 다를뿐 설정은 위에랑 동일하게 하시면됩니다

 

shard - replica set

mongo mongo_shard1:20001

접속하고

rs.initiate()
cfg = rs.conf()
cfg.members[0].host = "mongo_shard1:20001"
rs.reconfig(cfg,{force:true})
rs.add("mongo_shard2:20002")
rs.status()

결과는

이렇게 members에 두개가 정확히 들어간걸 확인하실 수 있습니다

이렇게 나오면 shard설정은 끝이납니다

https://devel-lee.tistory.com/16

 

[4.2]MongoDB sharding config-router 세팅하기

https://devel-lee.tistory.com/15 [4.2]mongoDB sharding shard 세팅하기 안녕하세요 오늘은 mongodb를 세팅하려고합니다 기본적인 서버는 router안에 config 3개를 넣고 나머지 두개 서버에 shard를 하나씩할 예..

devel-lee.tistory.com

 

댓글