Replica set
Replica set 는 홀수개를 운영해야함
- 노드가 짝수개밖에 없으면, 포트번호로 추가지정가능
Primary :
Secondary :
Arbiter : heart bit 역할, 부하거의 없음
cd ~/mongodb
rm -rf data/db
mkdir -p data/db
./bin/mongod --dbpath data/db --replSet test1 --oplogSize 1
C:\mongodb\bin>mongo.exe s1:27017
2017-01-25T09:36:31.983+0900 Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 2.6.12
connecting to: s1:27017/test
> use admin
switched to db admin
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "s1.test.com:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
>
test1:PRIMARY>
test1:PRIMARY> rs.con
rs.conf( rs.config( rs.constructor
test1:PRIMARY> rs.conf()
{
"_id" : "test1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "s1.test.com:27017"
}
]
}
test1:PRIMARY> cfg = rs.conf()
{
"_id" : "test1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "s1.test.com:27017"
}
]
}
test1:PRIMARY> cfg.members[0].host = "s1:27017";
s1:27017
test1:PRIMARY> rs.reconfig(cfg);
2017-01-25T09:40:17.336+0900 DBClientCursor::init call() failed
2017-01-25T09:40:17.338+0900 trying reconnect to s1:27017 (192.168.56.101) failed
2017-01-25T09:40:17.340+0900 reconnect s1:27017 (192.168.56.101) ok
reconnected to server after rs command (which is normal)
test1:PRIMARY> rs.add("s2:27017");
{ "ok" : 1 }
test1:PRIMARY> rs.addArb("s3:27017");
{ "ok" : 1 }
test1:PRIMARY> rs.conf()
{
"_id" : "test1",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "s1:27017"
},
{
"_id" : 1,
"host" : "s2:27017"
},
{
"_id" : 2,
"host" : "s3:27017",
"arbiterOnly" : true
}
]
}
test1:PRIMARY> rs.status()
{
"set" : "test1",
"date" : ISODate("2017-01-25T00:41:16Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "s1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 613,
"optime" : Timestamp(1485304834, 1),
"optimeDate" : ISODate("2017-01-25T00:40:34Z"),
"electionTime" : Timestamp(1485304638, 1),
"electionDate" : ISODate("2017-01-25T00:37:18Z"),
"self" : true
},
{
"_id" : 1,
"name" : "s2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 43,
"optime" : Timestamp(1485304834, 1),
"optimeDate" : ISODate("2017-01-25T00:40:34Z"),
"lastHeartbeat" : ISODate("2017-01-25T00:41:15Z"),
"lastHeartbeatRecv" : ISODate("2017-01-25T00:41:16Z"),
"pingMs" : 0,
"syncingTo" : "s1:27017"
},
{
"_id" : 2,
"name" : "s3:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 42, <= Arbiter는 OPtime 없음
"lastHeartbeat" : ISODate("2017-01-25T00:41:16Z"),
"lastHeartbeatRecv" : ISODate("2017-01-25T00:41:16Z"),
"pingMs" : 0
}
],
"ok" : 1
}
test1:PRIMARY>
use test
for (var i=0; i < 10000; i++) {
db.user.save({name:"john"+i, age:20 + (i%10) })
}
C:\mongodb\bin>mongo.exe s2:27017
2017-01-25T09:44:49.448+0900 Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 2.6.12
connecting to: s2:27017/test
test1:SECONDARY>
test1:SECONDARY> use test
switched to db test
test1:SECONDARY> db.user.findOne()
2017-01-25T09:45:12.489+0900 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131
test1:SECONDARY> rs.slaveOk();
test1:SECONDARY> db.user.findOne()
{
"_id" : ObjectId("5887f4bc514b2dcdfa1a4554"),
"name" : "john0",
"age" : 20
}
test1:SECONDARY> db.user.insert({name :"obama", age:60});
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
test1:SECONDARY>
[Oplog]
test1:PRIMARY> use test
switched to db test
test1:PRIMARY> db.user.insert({ name :'gdhong', age:20})
WriteResult({ "nInserted" : 1 })
test1:PRIMARY> db.user.update({name:'gdhong'}, { $set : { age:30 }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
test1:PRIMARY>
test1:PRIMARY> use local
switched to db local
test1:PRIMARY>
test1:PRIMARY> db.oplog.rs.find().sort({ts:-1}).limit(2).pretty();
{
"ts" : Timestamp(1485305312, 1),
"h" : NumberLong("-2545699453346556688"),
"v" : 2,
"op" : "u",
"ns" : "test.user",
"o2" : {
"_id" : ObjectId("5887f5df514b2dcdfa1a6c64")
},
"o" : {
"$set" : {
"age" : 30
}
}
}
{
"ts" : Timestamp(1485305311, 1),
"h" : NumberLong("-4829261684578901723"),
"v" : 2,
"op" : "i",
"ns" : "test.user",
"o" : {
"_id" : ObjectId("5887f5df514b2dcdfa1a6c64"),
"name" : "gdhong",
"age" : 20
}
}
[Primary 죽이기]
2017-01-25T09:52:21.070+0900 [initandlisten] connection accepted from 192.168.56.103:52089 #57 (5 connections now open)
2017-01-25T09:52:32.578+0900 [conn56] end connection 192.168.56.102:57793 (4 connections now open)
2017-01-25T09:52:32.579+0900 [initandlisten] connection accepted from 192.168.56.102:57795 #58 (5 connections now open)
^C2017-01-25T09:52:45.426+0900 [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2017-01-25T09:52:45.427+0900 [signalProcessingThread] now exiting
2017-01-25T09:52:45.427+0900 [signalProcessingThread] dbexit:
2017-01-25T09:52:45.427+0900 [signalProcessingThread] shutdown: going to close listening sockets...
2017-01-25T09:52:45.427+0900 [signalProcessingThread] closing listening socket: 7
2017-01-25T09:52:45.427+0900 [signalProcessingThread] closing listening socket: 8
2017-01-25T09:52:45.427+0900 [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2017-01-25T09:52:45.428+0900 [signalProcessingThread] shutdown: going to flush diaglog...
2017-01-25T09:52:45.428+0900 [signalProcessingThread] shutdown: going to close sockets...
2017-01-25T09:52:45.428+0900 [signalProcessingThread] shutdown: waiting for fs preallocator...
2017-01-25T09:52:45.428+0900 [signalProcessingThread] shutdown: lock for final commit...
2017-01-25T09:52:45.428+0900 [signalProcessingThread] shutdown: final commit...
2017-01-25T09:52:45.430+0900 [conn2] end connection 192.168.56.1:49553 (4 connections now open)
2017-01-25T09:52:45.430+0900 [conn11] end connection 192.168.56.102:57746 (4 connections now open)
2017-01-25T09:52:45.431+0900 [conn57] end connection 192.168.56.103:52089 (4 connections now open)
2017-01-25T09:52:45.431+0900 [conn58] end connection 192.168.56.102:57795 (4 connections now open)
2017-01-25T09:52:45.433+0900 [signalProcessingThread] shutdown: closing all files...
2017-01-25T09:52:45.434+0900 [signalProcessingThread] closeAllFiles() finished
2017-01-25T09:52:45.434+0900 [signalProcessingThread] journalCleanup...
2017-01-25T09:52:45.434+0900 [signalProcessingThread] removeJournalFiles
2017-01-25T09:52:45.473+0900 [signalProcessingThread] shutdown: removing fs lock...
2017-01-25T09:52:45.493+0900 [signalProcessingThread] dbexit: really exiting now
다시 살리면
[mongodb@s1 mongodb]$ ./bin/mongod --dbpath data/db --replSet test1
2017-01-25T09:55:18.797+0900 [initandlisten] MongoDB starting : pid=9101 port=27017 dbpath=data/db 64-bit host=s1.test.com
2017-01-25T09:55:18.812+0900 [initandlisten] db version v2.6.9
2017-01-25T09:55:18.813+0900 [initandlisten] git version: df313bc75aa94d192330cb92756fc486ea604e64
2017-01-25T09:55:18.813+0900 [initandlisten] build info: Linux build20.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2017-01-25T09:55:18.813+0900 [initandlisten] allocator: tcmalloc
2017-01-25T09:55:18.813+0900 [initandlisten] options: { replication: { replSet: "test1" }, storage: { dbPath: "data/db" } }
2017-01-25T09:55:18.841+0900 [initandlisten] journal dir=data/db/journal
2017-01-25T09:55:18.841+0900 [initandlisten] recover : no journal files present, no recovery needed
2017-01-25T09:55:18.864+0900 [initandlisten] waiting for connections on port 27017
2017-01-25T09:55:18.974+0900 [rsStart] replSet I am s1:27017
2017-01-25T09:55:18.974+0900 [rsStart] replSet STARTUP2
2017-01-25T09:55:18.974+0900 [rsSync] replSet SECONDARY
2017-01-25T09:55:18.975+0900 [rsMgr] replSet can't see a majority, will not try to elect self
2017-01-25T09:55:18.977+0900 [rsHealthPoll] replset info s2:27017 thinks that we are down
2017-01-25T09:55:18.977+0900 [rsHealthPoll] replSet member s2:27017 is up
2017-01-25T09:55:18.977+0900 [rsHealthPoll] replSet member s2:27017 is now in state PRIMARY
2017-01-25T09:55:18.981+0900 [rsHealthPoll] replset info s3:27017 thinks that we are down
2017-01-25T09:55:18.981+0900 [rsHealthPoll] replSet member s3:27017 is up
2017-01-25T09:55:18.981+0900 [rsHealthPoll] replSet member s3:27017 is now in state ARBITER
2017-01-25T09:55:19.456+0900 [initandlisten] connection accepted from 192.168.56.102:58213 #1 (1 connection now open)
2017-01-25T09:55:20.133+0900 [initandlisten] connection accepted from 192.168.56.103:52538 #2 (2 connections now open)
2017-01-25T09:55:20.150+0900 [conn2] end connection 192.168.56.103:52538 (1 connection now open)
2017-01-25T09:55:20.150+0900 [initandlisten] connection accepted from 192.168.56.103:52539 #3 (3 connections now open)
2017-01-25T09:55:20.456+0900 [conn1] end connection 192.168.56.102:58213 (1 connection now open)
2017-01-25T09:55:20.457+0900 [initandlisten] connection accepted from 192.168.56.102:58214 #4 (2 connections now open)
2017-01-25T09:55:23.976+0900 [rsBackgroundSync] replSet syncing to: s2:27017
2017-01-25T09:55:23.978+0900 [rsBackgroundSync] replset setting syncSourceFeedback to s2:27017
2017-01-25T09:55:28.462+0900 [conn4] end connection 192.168.56.102:58214 (1 connection now open)
2017-01-25T09:55:28.463+0900 [initandlisten] connection accepted from 192.168.56.102:58216 #5 (2 connections now open)
2017-01-25T09:55:33.806+0900 [initandlisten] connection accepted from 192.168.56.1:49562 #6 (3 connections now open)
2017-01-25T09:55:46.169+0900 [conn3] end connection 192.168.56.103:52539 (2 connections now open)
2017-01-25T09:55:46.169+0900 [initandlisten] connection accepted from 192.168.56.103:52541 #7 (3 connections now open)
2017-01-25T09:55:58.482+0900 [conn5] end connection 192.168.56.102:58216 (2 connections now open)
2017-01-25T09:55:58.483+0900 [initandlisten] connection accepted from 192.168.56.102:58218 #8 (3 connections now open)
C:\mongodb\bin>mongo.exe s1:27017
2017-01-25T09:55:33.809+0900 Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 2.6.12
connecting to: s1:27017/test
test1:SECONDARY>
test1:SECONDARY> db.isMaster()
{
"setName" : "test1",
"setVersion" : 4,
"ismaster" : false,
"secondary" : true,
"hosts" : [
"s1:27017",
"s2:27017"
],
"arbiters" : [
"s3:27017"
],
"primary" : "s2:27017",
"me" : "s1:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2017-01-25T01:01:12.168Z"),
"maxWireVersion" : 2,
"minWireVersion" : 0,
"ok" : 1
}
'dev > NoSQL 모델링' 카테고리의 다른 글
NoSql 모델링 기법 (0) | 2017.01.25 |
---|---|
Shard Cluster (1) | 2017.01.25 |
4. Document Database (0) | 2017.01.24 |
3. Column Family Database (0) | 2017.01.24 |
NO SQL을 선택하는 이유 (0) | 2017.01.23 |