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,
"name" : "mongodb2.example.net:27017",
"health" : 1,
"state" : 5,
"stateStr" : "STARTUP2",
"uptime" : 44,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2013-07-18T10:18:56Z"),
"lastHeartbeatRecv" : ISODate("2013-07-18T10:18:54Z"),
"pingMs" : 273
},
{
"_id" : 2,
"name" : "mongodb3.example.net:27017",
"health" : 1,
"state" : 5,
"stateStr" : "STARTUP2",
"uptime" : 44,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2013-07-18T10:18:45Z"),
"lastHeartbeatRecv" : ISODate("2013-07-18T10:18:56Z"),
"pingMs" : 62
}
],
"ok" : 1
}
test:SECONDARY>
新成员初始状态为"stateStr" : "(not reachable/healthy)",然后变成"stateStr" : "STARTUP2",然后变成"stateStr" : "RECOVERING",最后当数据同步成功后,状态变为"stateStr" : "SECONDARY"。
test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2013-07-18T10:28:57Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "mongodb1.example.net:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1681,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"self" : true
},
{
"_id" : 1,
"name" : "mongodb2.example.net:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 638,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"lastHeartbeat" : ISODate("2013-07-18T10:28:55Z"),
"lastHeartbeatRecv" : ISODate("2013-07-18T10:28:55Z"),
"pingMs" : 0,
"syncingTo" : "mongodb1.example.net:27017"
},
{
"_id" : 2,
"name" : "mongodb3.example.net:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 638,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"lastHeartbeat" : ISODate("2013-07-18T10:28:56Z"),
"lastHeartbeatRecv" : ISODate("2013-07-18T10:28:56Z"),
"pingMs" : 0,
"syncingTo" : "mongodb1.example.net:27017"
}
],
"ok" : 1
}
test:PRIMARY>
3. 故障切换
假设224为Primary,其它为Secondary,则可以使用mongo shell连接224,看到下面结果
[root@MongodbF-B App]# /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
test:PRIMARY> rs.isMaster()
{
"setName" : "test",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"mongodb1.example.net:27017",
"mongodb3.example.net:27017",
"mongodb2.example.net:27017"
],
"primary" : "mongodb1.example.net:27017",
"me" : "mongodb1.example.net:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2013-07-18T10:30:33.734Z"),
"ok" : 1
}
test:PRIMARY>
同时可以连接225查看状态;
[root@MongodbF-C App]# /App/mongo/bin/mongo 192.168.155.225
MongoDB shell version: 2.4.5
connecting to: 192.168.155.225/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
test:SECONDARY> rs.isMaster()
{
"setName" : "test",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"mongodb2.example.net:27017",
"mongodb3.example.net:27017",
"mongodb1.example.net:27017"
],
"primary" : "mongodb1.example.net:27017",
"me" : "mongodb2.example.net:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2013-07-18T10:32:03.170Z"),
"ok" : 1
}
test:SECONDARY>
我们停止224的mongod,然后发现已经mongo shell连接不上,而通过另外两个成员可以看到副本集的状态:
[root@MongodbF-A etc]# kill 8513
[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
Thu Jul 18 18:38:08.920 JavaScript execution failed: Error: couldn't connect to server 192.168.155.224:27017 at src/mongo/shell/mongo.js:L114
exception: connect failed
[root@MongodbF-A etc]#
停掉了;
然后查看225,226状态;
[root@MongodbF-C App]# /App/mongo/bin/mongo 192.168.155.226
MongoDB shell version: 2.4.5
connecting to: 192.168.155.226/test
test:PRIMARY> rs.status()
{
"set" : "test",
"date" : ISODate("2013-07-18T10:39:19Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "mongodb1.example.net:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"lastHeartbeat" : ISODate("2013-07-18T10:39:17Z"),
"lastHeartbeatRecv" : ISODate("2013-07-18T10:38:35Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "mongodb2.example.net:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1222,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"lastHeartbeat" : ISODate("2013-07-18T10:39:18Z"),
"lastHeartbeatRecv" : ISODate("2013-07-18T10:39:19Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "syncing to: mongodb3.example.net:27017",
"syncingTo" : "mongodb3.example.net:27017"
},
{
"_id" : 2,
"name" : "mongodb3.example.net:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1961,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"self" : true
}
],
"ok" : 1
}
test:PRIMARY>
225中:
test:SECONDARY> rs.isMaster()
{
"setName" : "test",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"mongodb2.example.net:27017",
"mongodb3.example.net:27017",
"mongodb1.example.net:27017"
],
"primary" : "mongodb3.example.net:27017",
"me" : "mongodb2.example.net:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2013-07-18T10:38:59.686Z"),
"ok" : 1
}
test:SECONDARY>
看到故障切换成功;
故障修复后,重启查看状态;
[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
forked process: 8891
log file [/log/log.log] exists; copied to temporary file [/log/log.log.2013-07-18T10-40-36]
child process started successfully, parent exiting
[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
test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2013-07-18T10:40:48Z"),
"myState" : 2,
"syncingTo" : "mongodb3.example.net:27017",
"members" : [
{
"_id" : 0,
"name" : "mongodb1.example.net:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 12,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"errmsg" : "syncing to: mongodb3.example.net:27017",
"self" : true
},
{
"_id" : 1,
"name" : "mongodb2.example.net:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 12,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"lastHeartbeat" : ISODate("2013-07-18T10:40:48Z"),
"lastHeartbeatRecv" : ISODate("2013-07-18T10:40:47Z"),
"pingMs" : 0,
"syncingTo" : "mongodb3.example.net:27017"
},
{
"_id" : 2,
"name" : "mongodb3.example.net:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 12,
"optime" : Timestamp(1374142698, 1),
"optimeDate" : ISODate("2013-07-18T10:18:18Z"),
"lastHeartbeat" : ISODate("2013-07-18T10:40:48Z"),
"lastHeartbeatRecv" : ISODate("2013-07-18T10:40:48Z"),
"pingMs" : 1
}
],
"ok" : 1
}
test:SECONDARY>
启动成功成为了secondary了
至此,副本集的故障切换完成;
test:SECONDARY> rs.config()
{
"_id" : "test",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "mongodb1.example.net:27017"
},
{
"_id" : 1,
"host" : "mongodb2.example.net:27017"
},
{
"_id" : 2,
"host" : "mongodb3.example.net:27017"
}
]
}
test:SECONDARY>
4、尝试在SECONDARY上插入数据;
test:SECONDARY> db.song.insert({"name":"xixi"})
not master
test:SECONDARY>
返回not master。所以必须使用驱动连接到PRIMARY上进行操作,必须在应用上对这种故障切换做进一步控制,保证是对PRIMARY进行操作。
test:PRIMARY> db.song.insert({"name":"xixi"})
test:PRIMARY> db.song.find()
{ "_id" : ObjectId("51e7c776ec806afa1810aeff"), "name" : "xixi" }
test:SECONDARY> show tables;
Thu Jul 18 18:46:24.018 JavaScript execution failed: error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:L128
test:SECONDARY>
可以通过rs.slaveOk()命令使该SECONDARY可以进行读操作:
test:SECONDARY> show tables;
Thu Jul 18 18:46:24.018 JavaScript execution failed: error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:L128
test:SECONDARY> rs.slaveOk()
test:SECONDARY> show tables;
song
system.indexes
test:SECONDARY> db.song.find()
{ "_id" : ObjectId("51e7c776ec806afa1810aeff"), "name" : "xixi" }
test:SECONDARY> db.song.insert({"name":"xixi"}) -----只读,不能写的类型;
not master
test:SECONDARY>
mongodb副本集自动切换修复节点解决方案的更多相关文章
- mongodb副本集(选举,节点设置,读写分离设置)
1.相对于传统主从模式的优势 传统的主从模式,需要手工指定集群中的Master.如果Master发生故障,一般都是人工介入,指定新的Master.这个过程对于应用一般不是透明的,往往伴随着应用重新修改 ...
- (2)MongoDB副本集自动故障转移原理
前文我们搭建MongoDB三成员副本集,了解集群基本特性,今天我们围绕下图聊一聊背后的细节. 默认搭建的replica set均在主节点读写,辅助节点冗余部署,形成高可用和备份, 具备自动故障转移的能 ...
- (2)MongoDB副本集自动故障转移全流程原理
前文我们搭建MongoDB三成员副本集,了解集群基本特性,今天我们围绕下图聊一聊背后的细节. 默认搭建的replica set均在主节点读写,辅助节点冗余部署,形成高可用和备份, 具备自动故障转移的能 ...
- 分布式文档存储数据库之MongoDB副本集
前文我们聊到了mongodb的索引的相关作用和介绍以及索引的管理,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13950287.html:今天我们来聊下mon ...
- mongodb副本集优先级设置
在设置mongodb副本集时,Primary节点.second节点,仲裁节点,有可能资源配置(CPU或者内存)不均衡,所以要求某些节点不能成为Primary我们知道mongodb的设置: 除了仲裁节 ...
- MongoDB副本集功能及节点属性梳理
副本集的主要功能 副本集是MongoDB高可用的基础,其主要作用 归纳为以下几点: (1)高可用,防止设备(服务器.网络)故障.提供自动FailOver功能. (2)无需配置高可用性虚拟节点:无论是S ...
- java程序连接MongoDB副本集测试
三个节点有一个节点挂掉也不会影响应用程序客户端对整个副本集的读写! public class TestMongoDBReplSet { public static void main(String[] ...
- MongoDB副本集的搭建
副本集是mongodb提供的一种高可用解决方案.相对于原来的主从复制,副本集能自动感知primary节点的下线,并提升其中一个Secondary作为Primary. 整个过程对业务透明,同时也大大降低 ...
- MongoDB副本集(一主两从)读写分离、故障转移功能环境部署记录
Mongodb是一种非关系数据库(NoSQL),非关系型数据库的产生就是为了解决大数据量.高扩展性.高性能.灵活数据模型.高可用性.MongoDB官方已经不建议使用主从模式了,替代方案是采用副本集的模 ...
随机推荐
- robomongo
Robomongo 是一个可视化的mongodb数据库工具,提供对mongodb的操作,javascript执行及语法高亮提示. 安装教程
- 关于Unix时间戳
如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? Java time JavaScript Math.round(new Date().getTime()/1000)ge ...
- 具体解释coredump
一,什么是coredump 我们常常听到大家说到程序core掉了,须要定位解决,这里说的大部分是指相应程序因为各种异常或者bug导致在执行过程中异常退出或者中止,而且在满足一定条件下(这里为什么说须要 ...
- 浏览器兼容——DOM事件封装函数
//封装函数var eventUtil={ //添加事件 addHandler:function(element,type,handler){ if(element.addE ...
- Eclipse新建Android工程,在模拟器运行的时候提示Unfortunately,XXX has stopped.
刚新建好的android工程在模拟器运行的时候出错,提示Unfortunately,XXX has stopped 查看Eclipse下面的错误信息,双击第一条 把ActionBarActivity前 ...
- 使用IDENTITY列属性和Sequence对象
使用IDENTITY列属性 1. 建立表 Sales.MyOrders USE TSQL2012; IF OBJECT_ID(N'Sales.MyOrders', N'U') IS NOT NULL ...
- BZOJ 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛( LIS )
裸的LIS ----------------------------------------------------------------- #include<cstdio> #incl ...
- Oracle数据库的安装详解
1.写在安装前的话 可能有很多的菜鸟十分害怕大型软件的安装,因为安装过程中的一些错误很让他们头疼.下面我就写一个教程,希望能对大家有帮助,在安装ORACLE之前给大家一点点的意见: (1)尽量要安装L ...
- 设计模式值六大原则——迪米特法则(LoD)也称为最少知识原则(LKP)。
定义: 迪米特法则(Law of Demeter,LoD)也称为最少知识原则(Least Knowledge Principle,LKP). 一个对象应该对其他对象有最少的了解.通俗地讲,一个类应该对 ...
- Android 开发笔记 “线程交互(Handler+Thread 和 AsyncTask)”
为什么需要线程 假设需要开发一个联网应用程序,需要从一个网址抓取网页内容,这里读取的网页地址是笔者在本地机器上自己建立的服务器地址.当然在读取网页内容的时候,可以使用HttpClient提供的API, ...