MongoDB on EC2
위와 같이 EC2에서 몽고DB 환경 구성을 ReplicaSet + Sharding 테스트 환경구성하려고 합니다.
참고링크
테스트 환경
Win7 64bit
테스트 구성 정보
- 각각의 Replica Sets은 3개 노드로 구성되며, 그중 3번째 노드는 arbiter로 구성.
- Mongos N개까지 여유가 되면 추가 구성 합니다.
- Replica-Set은 스스로 감지할 수 있는 장점이 있지만, 간혹가다 감지를 못하는 경우도 있다고 합니다. 그럴경우를 대비해서 aribter node(결정권자 노드) 구성을 하여 복제 노드들 간의 투표 참가만 합니다. arbiter node는 복제본을 가지고 있지 않습니다.
Process
1. 3개 Replica Set 구성 및 테스트 데이타 등록
1.1 저장소 디렉토리 구성
D:\DB\MONGO> mkdir firstset1 , firstset2, firstarbiter
D:\DB\MONGO> mkdir secondset1, secondset2, secondarbiter
D:\DB\MONGO> mkdir thirdset1, thirdset2, thirdarbiter
D:\DB\MONGO> mkdir config1,
config2, config3
1.2 mongod 3개의 인스턴스를 시작
mongod --dbpath D:\DB\MONGO\firstset1 --port 10001 --replSet firstset --oplogSize 700 --rest
mongod --dbpath D:\DB\MONGO\firstset2 --port 10002 --replSet firstset --oplogSize 700 --rest
mongod --dbpath D:\DB\MONGO\firstarbiter --port 10003 --replSet firstset --oplogSize 700 --rest
- oplog : 마스터가 수행한 연산 기록을 oplog(operatorion log)이라고 하며, 특수한 데이타베이스의 oplog.$main 컬렉션에 저장. msql에서 보면 트랜잭션 로그 같다고 보면 된다. oplog는 제한 컬렉션에 저장된다는 점인데 새로운 연산이 oplog에 저장될때 자동적으로 오래된 연산을 대체 한다. 64비트 인스턴스라고 하면 가용량의 5% 정도를 oplog 용도로 사용 한다. 테스트 환경에서는 700M로 로그 제한 한다.
1.3 mongo shell 접속
C:\>mongo localhost:10001/admin
MongoDB shell version: 2.0.2
connecting to: localhost:10001/admin
>
1.4 first replica-set 초기화
db.runCommand({"replSetInitiate" : {"_id" : "firstset", "members" : [{"_id" : 1, "host" : "localhost:10001"}, {"_id" : 2, "host" : "localhost:10002"}, {"_id" : 3, "host" : "localhost:10003", arbiterOnly: true }]}})
1.5 데이타를 등록
PRIMARY> use test
switched to db test
PRIMARY> for(var i=0; i<1000000; i++){
name = people[Math.floor(Math.random()*people.length)];
user_id = i;
boolean = [true, false][Math.floor(Math.random()*2)];
added_at = new Date();
number = Math.floor(Math.random()*10001);
db.test_collection.save({"name":name, "user_id":user_id, "boolean": boolean, "added_at":added_at, "number":number });
}
2. Config instance와 single shard 생성
config server는 어떤 데이타가 어떤 샤드에 있는지, 클러스터 설정을 저장한다. mongos 어떤 데이터도 영구 저장하지 않기 때문에 샤드 설정 받을 곳이 필요하다.
2.1 config servers 실행
mongod --configsvr --dbpath D:\DB\MONGO\config1 --port 20001
mongod --configsvr --dbpath D:\DB\MONGO\config2 --port 20002
mongod --configsvr --dbpath D:\DB\MONGO\config3 --port 20003
2.2 mongos 실행
mongos --configdb localhost:20001,localhost:20002,localhost:20003 --port 27018 --chunkSize 1
2.3 샤드를 추가
D:\DB\MONGO>mongo localhost:27018/admin
MongoDB shell version: 2.0.2
connecting to: localhost:27018/admin
mongos> db.runCommand( { addshard : "firstset/localhost:10001,localhost:10002,localhost:10003" } )
{ "shardAdded" : "firstset", "ok" : 1 }
mongos>
3. 두번째, 세번째 복제셋 생성
3.1 두번째 mongod instance 실행
mongod --dbpath D:\DB\MONGO\secondset1 --port 10004 --replSet secondset --oplogSize 700 --rest
mongod --dbpath D:\DB\MONGO\secondset2 --port 10005 --replSet secondset --oplogSize 700 --rest
mongod --dbpath D:\DB\MONGO\secondarbiter --port 10006 --replSet secondset --oplogSize 700 --rest
3.2 second replica-set 초기화
mongo localhost:10004/admin
db.runCommand({"replSetInitiate" : {"_id" : "secondset", "members" : [{"_id" : 1, "host" : "localhost:10004"}, {"_id" : 2, "host" : "localhost:10005"}, {"_id" : 3, "host" : "localhost:10006", arbiterOnly: true}]}})
3.3 두번째 샤드 클러스터 복제셋 추가
mongo localhost:27018/admin
db.runCommand( { addshard : "secondset/localhost:10004,localhost:10005,localhost:10006" } )
db.runCommand({listshards:1})
3.4 세번째 리플리카 mongod instance 실행
mongod --dbpath D:\DB\MONGO\thirdset1 --port 10007 --replSet thirdset --oplogSize 700 --rest
mongod --dbpath D:\DB\MONGO\thirdset2 --port 10008 --replSet thirdset --oplogSize 700 --rest
mongod --dbpath D:\DB\MONGO\thirdarbiter --port 10009 --replSet thirdset --oplogSize 700 --rest
3.5 복제 설정 초기화
mongo localhost:10007/admin
db.runCommand({"replSetInitiate" : {"_id" : "thirdset", "members" : [{"_id" : 1, "host" : "localhost:10007"}, {"_id" : 2, "host" : "localhost:10008"}, {"_id" : 3, "host" : "localhost:10009", arbiterOnly: true }]}})
3.6 세번째 샤드 클러스터 복제셋 구성
mongo localhost:27018/admin
db.runCommand( { addshard : "thirdset/localhost:10007,localhost:10008,localhost:10009" } )
db.runCommand({listshards:1})
4. 샤딩 활성화
5.1 데이타베이스 레벨 샤딩 설정
mongo localhost:27018/admin
mongos> use admin
switched to db admin
mongos> show dbs;
config 0.0625GB
test 0.203125GB
mongos> db.runCommand( { enablesharding : "test" } )
{ "ok" : 1 }
5.2 샤딩키 인덱스 생성
use test
db.test_collection.ensureIndex({number:1})
5.3 Collection의 샤드키 설정
use admin
-- test.test_collection의 number를 키 설정
db.runCommand( { shardcollection : "test.test_collection", key : {"number":1} })
5.4 마무리
use test
db.stats()
db.printShardingStatus()
'Etc' 카테고리의 다른 글
몽고DB 책 소개 (0) | 2012.05.16 |
---|---|
DB 개체 스크립트 하기 (0) | 2012.04.25 |
몽고DB 활용 사례 (0) | 2012.03.19 |
몽고DB Sharding 설정 및 테스트 (16) | 2012.02.29 |
SQL 설치 유용한 스크립트 (0) | 2012.02.24 |