mongodb副本集自动切换修复节点解决方案
副本集部署
1.启动mongod
在每台运行mongod服务的机器上增加配置文件/etc/mongodb-rs.conf,内容为:
[root@MongodbF-A etc]# vi /etc/mongodb-rs.conf
port = 27017
dbpath = /data/db
logpath = /log/log.log
fork = true
replSet = test
通过下面命令启动mongod:
[root@MongodbF-A etc]# /App/mongo/bin/mongod -f /etc/mongodb-rs.conf
about to fork child process, waiting until server is ready for connections.
all output going to: /log/log.log
log file [/log/log.log] exists; copied to temporary file [/log/log.log.2013-07-18T10-00-56]
forked process: 8513
child process started successfully, parent exiting
[root@MongodbF-A etc]#
[root@MongodbF-B App]# /App/mongo/bin/mongod -f /etc/mongodb-rs.conf
about to fork child process, waiting until server is ready for connections.
all output going to: /log/log.log
log file [/log/log.log] exists; copied to temporary file [/log/log.log.2013-07-18T10-00-06]
forked process: 8314
child process started successfully, parent exiting
[root@MongodbF-B App]#
[root@MongodbF-C App]# /App/mongo/bin/mongod -f /etc/mongodb-rs.conf
about to fork child process, waiting until server is ready for connections.
all output going to: /log/log.log
log file [/log/log.log] exists; copied to temporary file [/log/log.log.2013-07-18T10-06-38]
forked process: 8083
[Achild process started successfully, parent exiting
[root@MongodbF-C App]#
2. 修改每个机器的/etc/hosts
[root@MongodbF-A etc]# vim /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 MongodbF-A localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.155.224 mongodb1.example.net
192.168.155.225 mongodb2.example.net
192.168.155.226 mongodb3.example.net
~
2. 使用mongo shell连接mongod,进行配置:
[root@MongodbF-A etc]# /App/mongo/bin/mongo 192.168.155.224
MongoDB shell version: 2.4.5
connecting to: 192.168.155.224/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
> config = {_id:'test',members:[
... _id:0,host:'mongodb1.example.net'},
... _id:1,host:'mongodb2.example.net'},
Thu Jul 18 18:10:52.802 JavaScript execution failed: SyntaxError: Unexpected token :
> config = {_id:'test',members:[ _id:0,host:'mongodb1.example.net'}, _id:1,host:'mongodb2.example.net'},{_id:2,host:'mongodb3.example.net'}]}
Thu Jul 18 18:11:54.575 JavaScript execution failed: SyntaxError: Unexpected token :
> config_test={"_id":"test",members:[ --------------定义配置信息
... {_id:0,host:"mongodb1.example.net"},
... {_id:1,host:"mongodb2.example.net"},
... {_id:2,host:"mongodb3.example.net"}]
... }
{
"_id" : "test",
"members" : [
{
"_id" : 0,
"host" : "mongodb1.example.net"
},
{
"_id" : 1,
"host" : "mongodb2.example.net"
},
{
"_id" : 2,
"host" : "mongodb3.example.net"
}
]
}
>
> rs.initiate(config_test) -----启动副本集
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
test:STARTUP2>
test:STARTUP2>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY>
test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2013-07-18T10:19:03Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "mongodb1.example.net:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1087,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"self" : true
},
{
"_id" : 1,
&nb