MongoDB_Replica Sets

分别在节点上配置好参数后如下
hapame-db:~ # cat /etc/mongod.conf |grep jieshi
replSet = jieshi
重启MongoDB服务器,并保证服务器没有别的数据库(除admin
local),如果有其他数据库,在这一步rs.initiate(hapameconfig),会报错。

任意登入其中一台MongoDB服务器,做一下配置:
hapame-db:~ # mongo
MongoDB shell version: 1.6.5-rc
connecting to: test
> show dbs
admin
local
> hapameconfig= {_id:"jieshi", members: [
... {_id:0,host:"192.168.16.234"},
... {_id:1,host:"192.168.16.103"},
... {_id:2,host:"192.168.16.216"}
... ]}
{
"_id" : "jieshi",
"members" : [
{
"_id" : 0,
"host" : "192.168.16.234"
},
{
"_id" : 1,
"host" : "192.168.16.103"
},
{
"_id" : 2,
"host" : "192.168.16.216"
}
]
}
> rs.initiate(hapameconfig)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
> db.system.replset.find()
{ "_id" : "jieshi", "version" : 1, "members" : [
{
"_id" : 0,
"host" : "192.168.16.234"
},
{
"_id" : 1,
"host" : "192.168.16.103"
},
{
"_id" : 2,
"host" : "192.168.16.216"
}
] }
在192.168.16.234服务器上:(SUSE Linux Enterprise Server 11 (x86_64))

> rs.isMaster()
{
"setName" : "jieshi",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"192.168.16.234",
"192.168.16.216",
"192.168.16.103"
],
"ok" : 1
}
> rs.status()
{
"set" : "jieshi",
"date" : "Fri Dec 10 2010 12:48:46 GMT+0800 (CST)",
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "hapame-db:27017",
"health" : 1,
"state" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.16.103",
"health" : 1,
"state" : 2,
"uptime" : 133,
"lastHeartbeat" : "Fri Dec 10 2010 12:48:45 GMT+0800 (CST)"
},
{
"_id" : 2,
"name" : "192.168.16.216",
"health" : 1,
"state" : 2,
"uptime" : 127,
"lastHeartbeat" : "Fri Dec 10 2010 12:48:45 GMT+0800 (CST)"
}
],
"ok" : 1
}

在192.168.16.103服务器上:(CentOS release 5.5)
[root@jieshiyeskey ~]# cat /etc/mongod.conf |grep jieshi
replSet=jieshi
[root@jieshiyeskey mongo]# mongo
MongoDB shell version: 1.6.4
connecting to: test
> use local
switched to db local
> show collections
me
oplog.rs
replset.minvalid
system.indexes
system.replset
> db.system.replset.find()
{ "_id" : "jieshi", "version" : 1, "members" : [
{
"_id" : 0,
"host" : "192.168.16.234"
},
{
"_id" : 1,
"host" : "192.168.16.103"
},
{
"_id" : 2,
"host" : "192.168.16.216"
}
] }
> rs.isMaster()
{
"setName" : "jieshi",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"192.168.16.103",
"192.168.16.216",
"192.168.16.234"
],
"primary" : "192.168.16.234",
"ok" : 1
}
> rs.status()
{
"set" : "jieshi",
"date" : "Fri Dec 10 2010 12:52:17 GMT+0800 (CST)",
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "192.168.16.234",
"health" : 1,
"state" : 1,
"uptime" : 344,
"lastHeartbeat" : "Fri Dec 10 2010 12:52:16 GMT+0800 (CST)"
},
{
"_id" : 1,
"name" : "jieshiyeskey.hapaworld.com.cn:27017",
"health" : 1,
"state" : 2,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.16.216",
"health" : 1,
"state" : 2,
"uptime" : 338,
"lastHeartbeat" : "Fri Dec 10 2010 12:52:16 GMT+0800 (CST)"
}
],
"ok" : 1
}
在192.168.16.216服务器上:(FreeBSD 8.1-RELEASE)
[root@jieshi ~]# cat /usr/local/etc/mongodb.conf |grep jieshi
replSet=jieshi
[root@jieshi /var/db/mongodb]# mongo
MongoDB shell version: 1.6.3
connecting to: test
> show dbs
admin
local
> use local
switched to db local
> show collections
me
oplog.rs
replset.minvalid
system.indexes
system.replset
> db.system.replset.find()
{ "_id" : "jieshi", "version" : 1, "members" : [
{
"_id" : 0,
"host" : "192.168.16.234"
},
{
"_id" : 1,
"host" : "192.168.16.103"
},
{
"_id" : 2,
"host" : "192.168.16.216"
}
] }
> rs.isMaster()
{
"setName" : "jieshi",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"192.168.16.216",
"192.168.16.103",
"192.168.16.234"
],
"primary" : "192.168.16.234",
"ok" : 1
}
> rs.status()
{
"set" : "jieshi",
"date" : "Fri Dec 10 2010 04:58:15 GMT+0800 (CST)",
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "192.168.16.234",
"health" : 1,
"state" : 1,
"uptime" : 101,
"lastHeartbeat" : "Fri Dec 10 2010 04:58:14 GMT+0800 (CST)"
},
{
"_id" : 1,
"name" : "192.168.16.103",
"health" : 1,
"state" : 2,
"uptime" : 101,
"lastHeartbeat" : "Fri Dec 10 2010 04:58:14 GMT+0800 (CST)"
},
{
"_id" : 2,
"name" : "jieshi.hapame.com.cn:27017",
"health" : 1,
"state" : 2,
"self" : true
}
],
"ok" : 1
}
@@添加新成员192.168.16.211:(Red Hat Enterprise Linux Server release 6.0 (Santiago))

[root@6 mongo]# cat /etc/mongod.conf |grep jieshi
replSet=jieshi
root@6 mongo]# /etc/init.d/mongod start
Starting mongod: forked process: 8131
all output going to: /var/log/mongo/mongod.log
[ OK ]
[root@6 mongo]# tail /var/log/mongo/mongod.log
Fri Dec 10 13:18:50 [startReplSets] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Fri Dec 10 13:18:54 [snapshotthread] cpu: elapsed:4000 writelock: 0%
Fri Dec 10 13:18:58 [snapshotthread] cpu: elapsed:4000 writelock: 0%
Fri Dec 10 13:19:00 [startReplSets] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Fri Dec 10 13:19:02 [snapshotthread] cpu: elapsed:4000 writelock: 0%
Fri Dec 10 13:19:06 [snapshotthread] cpu: elapsed:4000 writelock: 0%
Fri Dec 10 13:19:10 [snapshotthread] cpu: elapsed:4000 writelock: 0%
Fri Dec 10 13:19:10 [startReplSets] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG) ##每个节点上都会报这个警告
Fri Dec 10 13:19:14 [snapshotthread] cpu: elapsed:4000 writelock: 0%
Fri Dec 10 13:19:18 [snapshotthread] cpu: elapsed:4000 writelock: 0%
要在primary 上才能添加新成员:(当前为192.168.16.234为primary)
hapame-db:~ # mongo
MongoDB shell version: 1.6.5-rc1
connecting to: test
> rs.add("192.168.16.211")
{ "ok" : 1 }
> rs.conf()
{
"_id" : "jieshi",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "192.168.16.234"
},
{
"_id" : 1,
"host" : "192.168.16.103"
},
{
"_id" : 2,
"host" : "192.168.16.216"
},
{
"_id" : 3,
"host" : "192.168.16.211"
}
]
}
> rs.isMaster()
{
"setName" : "jieshi",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"192.168.16.234",
"192.168.16.211",
"192.168.16.216",
"192.168.16.103"
],
"ok" : 1
}
> rs.status()
{
"set" : "jieshi",
"date" : "Fri Dec 10 2010 13:23:10 GMT+0800 (CST)",
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "hapame-db:27017",
"health" : 1,
"state" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.16.103",
"health" : 1,
"state" : 2,
"uptime" : 46,
"lastHeartbeat" : "Fri Dec 10 2010 13:23:08 GMT+0800 (CST)"
},
{
"_id" : 2,
"name" : "192.168.16.216",
"health" : 1,
"state" : 2,
"uptime" : 46,
"lastHeartbeat" : "Fri Dec 10 2010 13:23:08 GMT+0800 (CST)"
},
{
"_id" : 3,
"name" : "192.168.16.211",
"health" : 1,
"state" : 2,
"uptime" : 38,
"lastHeartbeat" : "Fri Dec 10 2010 13:23:08 GMT+0800 (CST)"
}
],
"ok" : 1
}
>
在192.168.16.211上查看如下:
[root@6 mongo]# mongo
MongoDB shell version: 1.6.4
connecting to: test
> show dbs
admin
local
> use local
switched to db local
> show collections
me
oplog.rs
replset.minvalid
system.indexes
system.replset
> db.system.replset.find()
{ "_id" : "jieshi", "version" : 2, "members" : [
{
"_id" : 0,
"host" : "192.168.16.234"
},
{
"_id" : 1,
"host" : "192.168.16.103"
},
{
"_id" : 2,
"host" : "192.168.16.216"
},
{
"_id" : 3,
"host" : "192.168.16.211"
}
] }
> rs.isMaster()
{
"setName" : "jieshi",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"192.168.16.211",
"192.168.16.216",
"192.168.16.103",
"192.168.16.234"
],
"primary" : "192.168.16.234",
"ok" : 1
}
> rs.status()
{
"set" : "jieshi",
"date" : "Fri Dec 10 2010 13:24:54 GMT+0800 (CST)",
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "192.168.16.234",
"health" : 1,
"state" : 1,
"uptime" : 142,
"lastHeartbeat" : "Fri Dec 10 2010 13:24:52 GMT+0800 (CST)"
},
{
"_id" : 1,
"name" : "192.168.16.103",
"health" : 1,
"state" : 2,
"uptime" : 142,
"lastHeartbeat" : "Fri Dec 10 2010 13:24:52 GMT+0800 (CST)"
},
{
"_id" : 2,
"name" : "192.168.16.216",
"health" : 1,
"state" : 2,
"uptime" : 142,
"lastHeartbeat" : "Fri Dec 10 2010 13:24:52 GMT+0800 (CST)"
},
{
"_id" : 3,
"name" : "6.hapame.com:27017",
"health" : 1,
"state" : 2,
"self" : true
}
],
"ok" : 1
}
测试:
在192.168.16.234上:
> rs.isMaster()
{
"setName" : "jieshi",
"ismaster" : true, #表示为primary
"secondary" : false,
"hosts" : [
"192.168.16.234",
"192.168.16.211",
"192.168.16.216",
"192.168.16.103"
],
"ok" : 1
}
在primary可以有全部的权限,但在辅上就没有全部的权限如下:
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
hapameworld
system.indexes
> db.hapameworld.find()
{ "_id" : ObjectId("4d01bed1356d93b46d48ba34"), "name" : "markgeng" }
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
Fri Dec 10 14:46:57 uncaught exception: error: { "$err" : "not master", "code" : 10107 }

现在关掉192.168.16.234服务器:
hapame-db:~ # ps -ef |grep mongod
root 2377 1 7 12:35 ? 00:09:55 /opt/mongodb/bin/mongod -f /etc/mongod.conf
root 11035 10139 0 14:51 pts/5 00:00:00 grep mongod
hapame-db:~ # killall mongod
hapame-db:~ # ps -ef |grep mongod
root 11039 10139 0 14:51 pts/5 00:00:00 grep mongod
现在192.168.16.211服务器为primary如下:
[root@6 ~]# mongo
MongoDB shell version: 1.6.4
connecting to: test
> rs.isMaster()
{
"setName" : "jieshi",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"192.168.16.211",
"192.168.16.216",
"192.168.16.103",
"192.168.16.234"
],
"ok" : 1
}
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
hapameworld
system.indexes
> db.hapameworld.find()
{ "_id" : ObjectId("4d01bed1356d93b46d48ba34"), "name" : "markgeng" }
> db.hapameworld.insert({name:"jieshi"})
> db.hapameworld.find()
{ "_id" : ObjectId("4d01bed1356d93b46d48ba34"), "name" : "markgeng" }
{ "_id" : ObjectId("4d01d812b68ce41287454183"), "name" : "jieshi" }
在辅的服务器上:
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
Fri Dec 10 15:36:13 uncaught exception: error: { "$err" : "not master", "code" : 10107 }
现在开启以前的primary:(192.168.16.234)
hapame-db:~ # mongod -f /etc/mongod.conf
hapame-db:~ # forked process: 12997
all output going to: /opt/mongodb_logs/mongod.log

hapame-db:~ # ps -ef |grep mongod
root 12997 1 0 15:37 ? 00:00:00 mongod -f /etc/mongod.conf
root 13039 12417 0 15:37 pts/2 00:00:00 grep mongod
hapame-db:~ # mongo
MongoDB shell version: 1.6.5-rc1
connecting to: test
> rsMaster()
Fri Dec 10 15:38:10 ReferenceError: rsMaster is not defined (shell):0
> rs.isMaster()
{
"setName" : "jieshi",
"ismaster" : false, #已变成辅,primary并不会自己迁移回来
"secondary" : true,
"hosts" : [
"192.168.16.234",
"192.168.16.211",
"192.168.16.216",
"192.168.16.103"
],
"primary" : "192.168.16.211",
"ok" : 1
}
> show dbs
admin
hapameworld
local
> use hapameworld
switched to db hapameworld
> show collections
Fri Dec 10 15:38:43 uncaught exception: error: { "$err" : "not master", "code" : 10107 }