08-19 Shard 코드

Ruinak·2021년 8월 19일
0

Bigdata Lesson

목록 보기
17/18
post-thumbnail

Shard

Shard config 서버
// config 서버 실행
mongod --configsvr --replSet configRepl --dbpath c:\devtools\shard\config01 -port 20001

mongod --configsvr --replSet configRepl --dbpath c:\devtools\shard\config02 -port 20002

mongod --configsvr --replSet configRepl --dbpath c:\devtools\shard\config03 -port 20003


// config 서버 접속 및 리플리카 셋 설정
mongo localhost:20001

var config = {
_id : "configRepl", members : [
{_id : 0, host : 'localhost:20001'},
{_id : 1, host : 'localhost:20002'},
{_id : 2, host : 'localhost:20003'}
]
}

rs.initiate(config)


// shard 서버 구성을 위한 리플리카 셋 설정
mongod --shardsvr --replSet shardRep1 --dbpath C:\devtools\shard\shard1\shardRep1\data -port 30011
mongod --shardsvr --replSet shardRep1 --dbpath C:\devtools\shard\shard1\shardRep2\data -port 30012
mongod --shardsvr --replSet shardRep1 --dbpath C:\devtools\shard\shard1\shardRep3\data -port 30013

mongod --shardsvr --replSet shardRep2 --dbpath C:\devtools\shard\shard2\shardRep1\data -port 30021
mongod --shardsvr --replSet shardRep2 --dbpath C:\devtools\shard\shard2\shardRep2\data -port 30022
mongod --shardsvr --replSet shardRep2 --dbpath C:\devtools\shard\shard2\shardRep3\data -port 30023

mongod --shardsvr --replSet shardRep3 --dbpath C:\devtools\shard\shard3\shardRep1\data -port 30031
mongod --shardsvr --replSet shardRep3 --dbpath C:\devtools\shard\shard3\shardRep2\data -port 30032
mongod --shardsvr --replSet shardRep3 --dbpath C:\devtools\shard\shard3\shardRep3\data -port 30033


각각의 리플리카 셋 서버 접속 및 설정

mongo localhost:30011

var config = {
_id : "shardRep1", members : [
{_id : 0, host : 'localhost:30011'},
{_id : 1, host : 'localhost:30012'},
{_id : 2, host : 'localhost:30013'}
]
}

rs.initiate(config)

mongo localhost:30012

var config = {
_id : "shardRep2", members : [
{_id : 0, host : 'localhost:30021'},
{_id : 1, host : 'localhost:30022'},
{_id : 2, host : 'localhost:30023'}
]
}

rs.initiate(config)

mongo localhost:30013

var config = {
_id : "shardRep3", members : [
{_id : 0, host : 'localhost:30031'},
{_id : 1, host : 'localhost:30032'},
{_id : 2, host : 'localhost:30033'}
]
}

rs.initiate(config)


// Mongos(shard 서버) 설정
mongos --configdb configRepl/localhost:20001,localhost:20002,localhost:20003

// Sharding 설정
mongo

// 샤드 설정
sh.addShard("shardRep1/localhost:30011")
sh.addShard("shardRep2/localhost:30021")
sh.addShard("shardRep3/localhost:30031")

// 샤드 DB 등록
sh.enableSharding("test")

// 샤딩 시킬 collection의 인덱싱 설정
use test
db.things.createIndex({ empno : 1})

// 샤드 시킬 콜렉션 설정(admin)
use admin
sh.shardCollection("test.things", {empno : "hashed"})

// 테스트 데이터 삽입
use test
for(var n = 100000; n<=110000; n++){
db.things.insert({empno : n, ename : 'test', sal : 1000})
}
for(var n = 0; n<=100000; n++){
db.things.insert({empno : n, ename : 'test', sal : 1000})
}
// 데이터 갯수 확인
db.things.count()

// 각 mongo 서버 데이터 확인
use admin

db.runCommand({ listshards })

mongo localhost:30011
use test
db.things.count()
exit
mongo localhost:30021
use test
db.things.count()
exit
mongo localhost:30031
test
db.things.count()


// 각 shardRep별 저장데이터 확인
mongo localhost:30011
use test
db.things.count()
exit

mongo localhost:30021
use test
db.things.count()
exit

mongo localhost:30031
use test
db.things.count()
exit

profile
Nil Desperandum <절대 절망하지 마라>

0개의 댓글

관련 채용 정보