DataBase/Mongo DB

Mongo DB 9강 - 샤딩 시스템 구성 (Git Bash)

상맹 2021. 10. 6. 16:37
반응형

1. shard 폴더생성

 - C:\mongolab 폴더 생성

2. config 서버 저장 폴더 생성

config1,2 폴더 생성(서버 뻗을시 나머지 하나로 서버 실행하기 위해 두개 생성)

3. 샤드 서버 설정

 

(1) 실행

mongod --shardsvr --dbpath "C:\mongolab\shard1" --port 30001
mongod --shardsvr --dbpath "C:\mongolab\shard2" --port 30002
mongod --shardsvr --dbpath "C:\mongolab\shard3" --port 30003

각 shard1,2,3 폴더별로 git bash 후 해당 명령어 실행 후 명령창 종료 하지 말고 대기

 

(2) 포트 확인

netstat -ano | grep 3000*

 

4. config 서버

 (1) 서버 실행

mongod --configsvr --replSet configSet --dbpath "C:\mongolab\config1" --port 50001
mongod --configsvr --replSet configSet --dbpath "C:\mongolab\config2" --port 50002

 

 (2) 리플리카셋 설정

  새로운 git bash 창 열기

mongo --port 50001;
let con = {
              _id:"configSet",
             members:[
                 {_id:1, host:"localhost:50001"},
                 {_id:2, host:"localhost:50002"}]
};
rs.initiate(con);

상태 확인

 rs.status();

5. mongos 서버 실행

mongos --configdb configSet/localhost:50001,localhost:50002 --port 20000

6. mongos 서버 접속

mongo --port 20000

7. admin으로 접속

use admin

8. 샤드 서버 추가해주기

명령어 하나씩 실행

sh.addShard("localhost:30001");
sh.addShard("localhost:30002");
sh.addShard("localhost:30003");

 

9. 샤드 키 설정

여러 개의 Shard 서버로 분할 되어 있기 때문에 특정 필드의 값으로 샤딩해야한다. _id로 되었다면 그 값을 통해 샤딩한다. 한마디로 _id값으로 구분하여 데이터를 분할 저장한다. 이렇게 저장해야지 나중에 _id값으로 어디에 샤딩되었는지 알 수 있다. 아래의 "sharding" 값은 DB 이름이다.

sh.enableSharding("DB 이름");
sh.shardCollection("sharding.컬렉션 이름", {_id: "hashed"});

 

10. 샤딩 상태 확인

mongos> sh.status()

 

11. 더미 데이터 추가해서 샤딩 제대로 되었는지 확인하기

(1) 더미데이터 추가

시간이 좀 걸린다. 생각보다!! (대략 10분 정도)

* 10만개에 1분정도 걸렸으니 100만개면 10분정도 걸림.

use sharding; for (let i=0; i<100000; i++){db.account.save({name:"name"+i})}

 

(2) 30001 포트로 접속해서 카운팅 해보기

mongo --port 30002 use sharding; db.account.count();

 

(3) 샤딩 상태 확인

db.account.getShardDistribution()

 


참고

 

Sharding in MongoDB: A Practical Guide

Sharding is a process of splitting up the large scale of data sets into a chunk of smaller data sets across multiple MongoDB instances in a distributed

geekflare.com


 

반응형