[DataBase] MongoDB(8) 복제본 세트

6314 단어
MongoDB 복제본 세트 만들기
MongoDB 복제는 여러 서버에 데이터를 동기화하는 프로세스입니다.
복제는 데이터의 중복 백업을 제공하고 여러 서버에 데이터 사본을 저장하여 데이터의 가용성을 높이며 데이터의 안전성을 보장한다.
또한 복제를 통해 하드웨어 장애 및 서비스 중단으로부터 데이터를 복구할 수 있습니다.
 
1. 프로필 만들기
mongodb.conf

  1 # Start MongoDB as a daemon on port 5568                                                                                                                   
  2 
  3 port = 5568
  4 fork = true
  5 replSet = test_replica_set
  6 dbpath = /datatest/db
  7 logpath = /datatest/db/test.log
  8 logappend = true
  9 

mongodb2.conf

  2 
  3 port = 5569
  4 fork = true
  5 replSet = test_replica_set
  6 dbpath = /datatest/db2
  7 logpath = /datatest/db/test2.log
  8 logappend = true
  9 

mongodb3.conf

  2 
  3 port = 5570
  4 fork = true
  5 replSet = test_replica_set
  6 dbpath = /datatest/db3
  7 logpath = /datatest/db/test.log
  8 logappend = true
  9 

 2. 데이터베이스 시작
mac-abeen:bin abeen$ sudo ./mongod -f mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 37181
child process started successfully, parent exiting
mac-abeen:bin abeen$ sudo ./mongod -f mongodb2.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 37185
child process started successfully, parent exiting
mac-abeen:bin abeen$ sudo ./mongod -f mongodb3.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 37189
child process started successfully, parent exiting

 3. 복제본 세트 초기화
mac-abeen:bin abeen$ ./mongo --nodb
MongoDB shell version: 3.2.8
> config = {}
{ }
> config = {"_id": "test_replica_set", "members": [
... {"_id": 0, "host": "localhost:5568"}, {"_id": 1, "host": "localhost:5569"}, {"_id": 2, "host": "localhost:5570"}]}
{
	"_id" : "test_replica_set",
	"members" : [
		{
			"_id" : 0,
			"host" : "localhost:5568"
		},
		{
			"_id" : 1,
			"host" : "localhost:5569"
		},
		{
			"_id" : 2,
			"host" : "localhost:5570"
		}
	]
}
> db = (new Mongo("localhost:5568")).getDB("test")
test
> rs.initiate(config)
{ "ok" : 1 }
test_replica_set:OTHER> 


test_replica_set:PRIMARY> rs.config()
{
	"_id" : "test_replica_set",
	"version" : 1,
	"protocolVersion" : NumberLong(1),
	"members" : [
		{
			"_id" : 0,
			"host" : "localhost:5568",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "localhost:5569",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "localhost:5570",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("5850f445c8cacd70496883b0")
	}
}

 4. 데이터 쓰기 및 복제본 세트 데이터 보기
 
test_replica_set:PRIMARY> 
test_replica_set:PRIMARY> for (i=0; i < 100; i++){db.coll.insert({"count": i})}
WriteResult({ "nInserted" : 1 })
test_replica_set:PRIMARY> db.coll.count()
100
test_replica_set:PRIMARY> db.coll.find().limit(5)
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b51"), "count" : 0 }
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b52"), "count" : 1 }
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b53"), "count" : 2 }
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b54"), "count" : 3 }
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b55"), "count" : 4 }


 
mac-abeen:bin abeen$ ./mongo --port 5569 --host localhost
MongoDB shell version: 3.2.8
connecting to: localhost:5569/test

test_replica_set:SECONDARY> db.coll.find().limit(5)
Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }
 
test_replica_set:SECONDARY> db.setSlaveOk()
  
test_replica_set:SECONDARY> db.coll.find().limit(5)
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b53"), "count" : 2 }
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b55"), "count" : 4 }
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b52"), "count" : 1 }
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b54"), "count" : 3 }
{ "_id" : ObjectId("5850f5f35f1a7d82c0b45b51"), "count" : 0 }

좋은 웹페이지 즐겨찾기