SKILL/DATABASE

몽고db 샤드, 리플리카

Jedy_Kim 2018. 1. 5. 18:22
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
Sharding : 파티션, 데이터를 나누는 방법
Replica Set : 복제 셋(복구의 용도)
    -> 둘은 거의 같이 사용
    -> Shard는 홀수로 7개이상 하는 것을 권장
    
Shard key : index와 비슷
    - chunk단위(하나의 온전한 단위)로 나누기 쉬워야 한다.
    - shard에 64mb까지 가능.
    - 일반적인 document라고 생각.
    - _id 키로 설정하면 데이터가 한 곳에 몰릴 가능성이 존재.
        (why? 시간에 따라서 id가 설정되기 때문에)
 
폴더 구조
shard -> -config 1
     -config 2
     -config 3
     -shard    -> -shard1 ->shardRep1,2,3 -> data -> db
           -shard2 ->shardRep1,2,3 -> data -> db
           -shard3 ->shardRep1,2,3 -> data -> db
 
config서버로 만들겠다?
mongod --configsvr --replSet <세트이름> --dbpath <config 서버 경로> --port
mongod --configsvr --replSet configRepl --dbpath C:\Users\bit-user\Desktop\김동운\shard\config1 --port 20001
mongod --configsvr --replSet configRepl --dbpath C:\Users\bit-user\Desktop\김동운\shard\config2 --port 20002
mongod --configsvr --replSet configRepl --dbpath C:\Users\bit-user\Desktop\김동운\shard\config3 --port 20003
 
//경로 C:\Users\bit-user\Desktop\김동운\shard\config3
 
config 서버에 접속
//접속한 20001번 port의 config server가 primary가 됨.
mongo --host localhost --port 20001
 
config서버 묶는 작업 : 
    -샤딩을 위한 ConfigServer들을 등록.
    -다른 컴퓨터를 다룰 때는, localhost -> ip address
>rs.initiate({_id:"configRepl", configsvr:true, members:[{_id:0, host: "localhost:20001"},{_id:1, host: "localhost:20002"},{_id:2, host: "localhost:20003"}]})
rs.initiate(
    {
        _id:"configRepl",
        configsvr:true,
        members:[
            {_id:value, host:"ip_address:port"},
            {_id:value, host:"ip_address:port"},
            {_id:value, host:"ip_address:port"}
        ]
    }
)
 
Shard 서버
mongod --shardsvr --replSet <세트이름> --dbpath <shard sever path> --port 30001
 
mongod --shardsvr --replSet shardRepl1 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard1\shardRep1\data\db --port 30011
mongod --shardsvr --replSet shardRepl1 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard1\shardRep2\data\db --port 30012
mongod --shardsvr --replSet shardRepl1 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard1\shardRep3\data\db --port 30013
 
mongod --shardsvr --replSet shardRepl2 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard2\shardRep1\data\db --port 30021
mongod --shardsvr --replSet shardRepl2 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard2\shardRep2\data\db --port 30022
mongod --shardsvr --replSet shardRepl2 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard2\shardRep3\data\db --port 30023
 
mongod --shardsvr --replSet shardRepl3 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard3\shardRep1\data\db --port 30031
mongod --shardsvr --replSet shardRepl3 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard3\shardRep2\data\db --port 30032
mongod --shardsvr --replSet shardRepl3 --dbpath C:\Users\bit-user\Desktop\김동운\shard\shard\shard3\shardRep3\data\db --port 30033 
 
mongo --host localhost --port 30011
mongo --host localhost --port 30021
mongo --host localhost --port 30031
rs.initiate(
{
    _id:"shardRepl1",
    shardsvr:true,
    members:[
        {_id:0, host:"localhost:30011"},
        {_id:1, host:"localhost:30012"},
        {_id:2, host:"localhost:30013"}
    ]
}
)
 
rs.initiate({_id:"shardRepl1", members:[{_id:0, host:"localhost:30011"},{_id:1, host:"localhost:30012"},{_id:2, host:"localhost:30013"}]})
rs.initiate({_id:"shardRepl2", members:[{_id:0, host:"localhost:30021"},{_id:1, host:"localhost:30022"},{_id:2, host:"localhost:30023"}]})
rs.initiate({_id:"shardRepl3", members:[{_id:0, host:"localhost:30031"},{_id:1, host:"localhost:30032"},{_id:2, host:"localhost:30033"}]})
 
5) mongos 연결하기 (클라이언트 위한 통로 만들기)
mongos --configdb <configReplSetName>/<host:port>,<host:port>,<host:port> --port 20000
 
mongos --configdb configRepl/localhost:20001,localhost:20002,localhost:20003 --port 20000
//20000번 포트로 몽고스 연결, config로 가는 mongos를 연결하겠다.
//직접 못가니깐 20000번 포트를 사용하겠다.
 
mongos 연결
mongo localhost:20000
 
Shard 붙이기(연결) //기존에 샤드를 결합했으므로 primary 들만 추가해주면 됨.
sh.addShard("<replSetName>/<localhost:port>")
sh.addShard("shardRepl1/localhost:30011")
sh.addShard("shardRepl2/localhost:30021")
sh.addShard("shardRepl3/localhost:30031")
 
sh.status()
 
//Sharding 가능한 데이터베이스 등록
sh.enableSharding("<database>")
sh.enableSharding("hellomongo")
 
//샤딩 설정전 컬렉션의 인덱스를 먼저 걸어줘야 한다.
use hellomongo
db.employees.createIndex({empno:1})
 
//인덱스를 통해 샤드 키를 생서하기 위해
//관리자 권한을 실행할 것.
use admin
>sh.shardCollection("<database>.<collection>", {<key> : <direction>})
sh.shardCollection("hellomongo.employees", {empno:"hashed"}) //샤딩 가능한 컬레션 등록하기(해쉬를 통해 분배되도록)
 
 
//샤드 확인
>db.runCommand({listshards:1})
 
예시 데이터 넣기
**use hellomongo
for (var n = 1; n<=1000; n++) db.employees.save({empno:n, ename:"test", sal:1000})
 
db.employees.count()
 
Direction key
Hashed Shard Key : 유일한 값으로 키 값을 설정하여 데이터가 몰리는 것을 방지
            키를 기준으로 분배. (ex : num % 3)
            검색 속도를 줄이고, 아이템이 저장된 그룹을 지정.
            *검색에 대한 범위를 줄이는 것이 핵심
Ranged Shared Key : 데이터가 한 곳에 몰릴수도 있음.
            (0~1000은 shard1, 1001~3000은 shard2, 3001~7000은 shard3)
 
Master_Slave
서버 실행
mongod --dbpath <path> --port<port_no> --master
            //포트는 10000, 옵션을 master로 지정
mongod -dbpath C:\Users\bit-user\Desktop\김동운\repl1\master --master -port 10000
 
mongod --dbpath C:\Users\bit-user\Desktop\김동운\repl1\slave1 -port 10001 --slave --source <master_ip:master_port_no>
//슬레이브 서버는 마스터 종속도 표기, 마스터는 10000
 
mongod --dbpath C:\Users\bit-user\Desktop\김동운\repl1\slave1 -port 10001 --slave --source localhost:10000
mongod --dbpath C:\Users\bit-user\Desktop\김동운\repl1\slave2 -port 10002 --slave --source localhost:10000
 
마스터 접속
mongo localhost:10000
show dbs //마스터는 조작 가능
슬레이브 접속
mongo localhost:10001
show dbs //일반적으로 볼 수 있는 서버가 아니라 err
    //not master and slaveOk = false
rs.slaveOk() //but, 해당 함수를 실행하면 slaveOk = true로 변환
 
use admin
db.shutdownServer()
 
del <마스터 경로\>*.* //일반 cmd
del C:\Users\bit-user\Desktop\김동운\repl1\master\*.*
 
rm -/Users/User/Desktop/비트전문가과정/repl1/master/* //맥os
슬레이브 경로에서 마스터경로로 복사
copy <slave1>\*.* <마스터경로>\*.* //일반 cmd
copy C:\Users\bit-user\Desktop\김동운\repl1\slave1\*.* C:\Users\bit-user\Desktop\김동운\repl1\master\*.*
cp -r /Users/User/Desktop/비트전문가과정/repl1/slave1/* /Users/User/Desktop/비트전문가과정/repl1/master //맥os
UUID 사용할것 
//아비터를 이용한 복구
mongod --dbpath <path> --port 10000 --replSet rptmongo --oplogSize 10 //지정하지 않으면 1GB
mongod --dbpath C:\Users\bit-user\Desktop\김동운\repl2\disc1 --port 10000 --replSet rptmongo --oplogSize 10
mongod --dbpath C:\Users\bit-user\Desktop\김동운\repl2\disc2 --port 10001 --replSet rptmongo --oplogSize 10
mongod --dbpath C:\Users\bit-user\Desktop\김동운\repl2\arbit --port 10002 --replSet rptmongo --oplogSize 10
//서버 접속
mongo localhost:10000/admin
//리플리카 셋 초기화
rs.initiate() //enter 더 쳐서 primary 확인!
disc1 ,disc2, arbit 서버 접속 후
primary server에서 슬레이브를 추가 
>rs.add("localhost:10001")    //리플리카 셋 추가하는 작업, secondary
아비터 추가
>rs.addArb("<ip_addr>:<port_no>")
>rs.addArb("localhost:10002")     
상태확인
>rs.status() //10000 마스터
rs.add("localhost:10001")
rs.addArb("localhost:10002")
rs.status()
//각각 접속하여 secondary, arbiter, primary 확인!
//정보 확인
db.printReplicationInfo()
//슬레이브 정보 확인
db.printSlaveReplicationInfo()
mongo localhost:1000/admin
//primary 셧 다운 시켜서
10001(secondary) 접속 후 primary로 바뀌었는지 확인
mongod --dbpath C:\Users\bit-user\Desktop\김동운\repl2\disc1 --port 1000 --replSet rptmongo --oplogSize 10
10001포트 다시 살려서 secondary로 바뀌었는지 확인
mkdir C:\Users\bit-user\Desktop\김동운\repl2\disc3
mongod --dbpath C:\Users\bit-user\Desktop\김동운\repl2\disc3 --port 10004 --replSet rptmongo --oplogSize 10
//disc3 폴더를 생성 후에 해당 서버를 열어라
//기존 primary 서버 접속 mongo
db.printSlaveReplicationInfo()
rs.add("localhost:10004")
primary에서 use hellomongo
db.employees.insert({empno:1101, ename:"hello"})
mongo localhost:10001 //secondary로 접속
rs.slaveOk()
db.employees.find()
//arbiter
//다중 프로세서에서 중재 역할을 함
//서버가 다운되었을 때 다른 서버를 넣어줌.
cs


반응형

'SKILL > DATABASE' 카테고리의 다른 글

몽고DB master/slave  (2) 2018.01.06
몽고DB  (0) 2018.01.04
몽고db JDBC 예제  (0) 2018.01.03
모델링  (0) 2018.01.03
몽고db 실습예제  (0) 2018.01.02