1 实验主从复制,并验证复制成功
2 实验副本集,并验证自动切换primary成功.
=======================================================================
1.1.启动master节点:
mongod -dbpath=D:\Program_file\mongo\db1 -logpath=D:\Program_file\mongo\log\mongodb1.log --port 18001 --master --rest --nojournal
1.2.启动slave1节点:
mongod -dbpath=D:\Program_file\mongo\db2 -logpath=D:\Program_file\mongo\log\mongodb2.log --port 18002 --slave --rest --nojournal --source localhost:18001
1.3.启动slave2节点:
mongod -dbpath=D:\Program_file\mongo\db3 -logpath=D:\Program_file\mongo\log\mongodb2.log --port 18003 --slave --rest --nojournal --source localhost:18001
1.4.打开主库,新建立一个库及表:
D:\Program_file\mongo\mongodb\bin>mongo -port 18001
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:18001/test
Server has startup warnings:
Thu Mar 13 11:36:06.084 [initandlisten]
Thu Mar 13 11:36:06.084 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Thu Mar 13 11:36:06.084 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Thu Mar 13 11:36:06.084 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Thu Mar 13 11:36:06.084 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Thu Mar 13 11:36:06.084 [initandlisten]
Thu Mar 13 11:36:06.192 [initandlisten]
Thu Mar 13 11:36:06.192 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.replset
Thu Mar 13 11:36:06.192 [initandlisten] ** Restart with --replSet unless you are doing maintenance and no other clients are connected.
Thu Mar 13 11:36:06.192 [initandlisten] ** The TTL collection monitor will not start because of this.
Thu Mar 13 11:36:06.192 [initandlisten] ** For more info see http://dochub.mongodb.org/core/ttlcollections
Thu Mar 13 11:36:06.192 [initandlisten]
> show dbs;
admin 0.0625GB
local 0.15625GB
maketion 0.0625GB
mydb 0.0625GB
> use test
switched to db test
> db.test.insert({_id:1,name:"name_test"});
> db.test.find()
{ "_id" : 1, "name" : "name_test" }
1.5.打开从库,查看新建立的数据库及表有没有同步:
D:\Program_file\mongo\mongodb\bin>mongo -port 18002
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:18002/test
Server has startup warnings:
Thu Mar 13 11:44:17.236 [initandlisten]
Thu Mar 13 11:44:17.236 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Thu Mar 13 11:44:17.236 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Thu Mar 13 11:44:17.236 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Thu Mar 13 11:44:17.236 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Thu Mar 13 11:44:17.236 [initandlisten]
Thu Mar 13 11:44:17.263 [initandlisten]
Thu Mar 13 11:44:17.263 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.replset
Thu Mar 13 11:44:17.263 [initandlisten] ** Restart with --replSet unless you are doing maintenance and no other clients are connected
Thu Mar 13 11:44:17.263 [initandlisten] ** The TTL collection monitor will not start because of this.
Thu Mar 13 11:44:17.263 [initandlisten] ** For more info see http://dochub.mongodb.org/core/ttlcollections
Thu Mar 13 11:44:17.263 [initandlisten]
> show dbs;
admin 0.0625GB
local 0.09375GB
maketion 0.0625GB
mydb 0.0625GB
test 0.0625GB
> use test;
switched to db test
> show collections;
system.indexes
test
> db.test.find();
{ "_id" : 1, "name" : "name_test" }
>
可以看到,新建立一个库TEST,表:TEST 及数据都已同步过来了。
-----------------------------------------------------------------------------------
2 实验副本集,并验证自动切换primary成功,抓图实验过程
下面配置为前段时间配置的一个 replset 测试环境:
2.1 建立相关目录:
mkdir D:\Program_file\mongo\key
mkdir D:\Program_file\mongo\db1
mkdir D:\Program_file\mongo\db2
mkdir D:\Program_file\mongo\db3
mkdir D:\Program_file\mongo\db4
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r0 -dbpath=D:\Program_file\mongo\db1 -logpath=D:\Program_file\mongo\log\mongodb.log --logappend --journal --port 28010
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r1 -dbpath=D:\Program_file\mongo\db1 -logpath=D:\Program_file\mongo\log\mongodb1.log --logappend --journal --port 28011
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r2 -dbpath=D:\Program_file\mongo\db2 -logpath=D:\Program_file\mongo\log\mongodb2.log --logappend --journal --port 28012
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r3 -dbpath=D:\Program_file\mongo\db3 -logpath=D:\Program_file\mongo\log\mongodb3.log --logappend --journal --port 28013
--文件拷贝后添加--fastsync 参数启动(拷贝的如果不是primary,启动后拷贝前的节点会变为primary)
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r4 -dbpath=D:\Program_file\mongo\db4 -logpath=D:\Program_file\mongo\log\mongodb4.log --logappend --journal --port 28014 --fastsync
#share:
D:\Program_file\mongo\mongodb\bin\mongod.exe --shardsvr --port 20000 -dbpath=D:\Program_file\mongo\shard\shard0\db -logpath=D:\Program_file\mongo\shard\shard0\log\mongo.log --logappend --journal --directoryperdb
D:\Program_file\mongo\mongodb\bin\mongod.exe --shardsvr --port 20001 -dbpath=D:\Program_file\mongo\shard\shard1\db -logpath=D:\Program_file\mongo\shard\shard1\log\mongo.log --logappend --journal --directoryperdb
#config
D:\Program_file\mongo\mongodb\bin\mongod.exe --configsvr --port 30000 -dbpath=D:\Program_file\mongo\shard\config -logpath=D:\Program_file\mongo\shard\config\mongo.log --logappend --journal --directoryperdb
#route
D:\Program_file\mongo\mongodb\bin\mongos.exe --port 40000 --configdb localhost:30000 --logpath=D:\Program_file\mongo\shard\config\route.log --logappend --chunkSize 1
2.2 配置rs
config_rs1={_id:'rs1',members:[
{_id:0,host:'localhost:28010',priority:1},
{_id:0,host:'localhost:28011'},
{_id:0,host:'localhost:28012'},
{_id:0,host:'localhost:28013'},
{_id:0,host:'localhost:28014'}]};
rs.initiat(config_rs1);
2.3 关闭primary,
rs1:PRIMARY> use admin
rs1:PRIMARY> runCommand("shutdown");
2.4 #登录另一节点:
D:\Program_file\mongo\mongodb\bin>mongo -port 28011
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:28011/test
Server has startup warnings:
Thu Mar 13 13:55:23.541 [initandlisten]
Thu Mar 13 13:55:23.541 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Thu Mar 13 13:55:23.541 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Thu Mar 13 13:55:23.541 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Thu Mar 13 13:55:23.541 [initandlisten]
rs1:PRIMARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2014-03-13T05:58:52Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2014-03-13T05:58:50Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 209,
"optime" : Timestamp(1389939638, 1),
"optimeDate" : ISODate("2014-01-17T06:20:38Z"),
"self" : true
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 197,
"optime" : Timestamp(1389939638, 1),
"optimeDate" : ISODate("2014-01-17T06:20:38Z"),
"lastHeartbeat" : ISODate("2014-03-13T05:58:51Z"),
"lastHeartbeatRecv" : ISODate("2014-03-13T05:58:51Z"),
"pingMs" : 0,
"syncingTo" : "localhost:28011"
},
{
"_id" : 3,
"name" : "localhost:28013",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 193,
"optime" : Timestamp(1389939638, 1),
"optimeDate" : ISODate("2014-01-17T06:20:38Z"),
"lastHeartbeat" : ISODate("2014-03-13T05:58:51Z"),
"lastHeartbeatRecv" : ISODate("2014-03-13T05:58:51Z"),
"pingMs" : 0,
"syncingTo" : "localhost:28011"
}
],
"ok" : 1
}
rs1:PRIMARY>