为什么使用MongogDB复制集技术?
mysql中:一主一从,一主多从结构存在的问题
1、 fileover(故障转移)
a) 选主投票
b) 切换
2、 是否对就用透明化
3、 数据补偿的问题
a) 两阶段数据补偿
4、 解决方法 mysql中使用MHA+VIP + binlogserver
mongodb通过复制集技术解决以上所有问题,不需要第三方软件,自已原生支持
MongoDB复制集简介
一组Mongodb复制集,就是一组mongod进程,这些进程维护同一个数据集合。复制集提供了数据冗余和高等级的可靠性,这是生产部署的基础。
保证数据在生产部署时的冗余和可靠性,通过在不同的机器上保存副本来保证数据的不会因为单点损坏而丢失。能够随时应对数据丢失、机器损坏带来的风险,牛逼到不行。换一句话来说,还能提高读取能力,用户的读取服务器和写入服务器在不同的地方,而且,由不同的服务器为不同的用户提供服务,提高整个系统的负载,简直就是云存储的翻版...一组复制集就是一组mongod实例掌管同一个数据集,实例可以在不同的机器上面。实例中包含一个主导,接受客户端所有的写入操作,其他都是副本实例,从主服务器上获得数据并保持同步。
主服务器很重要,包含了所有的改变操作(写)的日志。但是副本服务器集群包含有所有的主服务器数据,因此当主服务器挂掉了,就会在副本服务器上重新选取一个成为主服务器。每个复制集还有一个仲裁者,仲裁者不存储数据,只是负责通过心跳包来确
认集群中集合的数量,并在主服务器选举的时候作为仲裁决定结果。
复制的基本架构
一个包含3个mongod的复制集架构如下所示
如果主服务器失效,会变成:
如果加上可选的仲裁者:
如果主服务器失效:
复制集技术实现
搭建三节点MongoDB复制集
#创建多mongodb多实例
mkdir -p /mongodb/28017/conf /mongodb/28017/data /mongodb/28017/log
mkdir -p /mongodb/28018/conf /mongodb/28018/data /mongodb/28018/log
mkdir -p /mongodb/28019/conf /mongodb/28019/data /mongodb/28019/log
mkdir -p /mongodb/28020/conf /mongodb/28020/data /mongodb/28020/log
#创建mongodb多
vim /mongodb/28017/conf/mongod.conf
systemLog:
destination: file
path: /mongodb/28017/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28017/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
net:
port: 28017
replication:
oplogSizeMB: 2048
replSetName: my_repl
\cp /mongodb/28017/conf/mongod.conf /mongodb/28018/conf/
\cp /mongodb/28017/conf/mongod.conf /mongodb/28019/conf/
\cp /mongodb/28017/conf/mongod.conf /mongodb/28020/conf/
sed 's#28017#28018#g' /mongodb/28018/conf/mongod.conf -i
sed 's#28017#28019#g' /mongodb/28019/conf/mongod.conf -i
sed 's#28017#28020#g' /mongodb/28020/conf/mongod.conf -i
chown -R mongod:mongod /mongodb/
su - mongod
mongod -f /mongodb/28017/conf/mongod.conf
mongod -f /mongodb/28018/conf/mongod.conf
mongod -f /mongodb/28019/conf/mongod.conf
mongod -f /mongodb/28020/conf/mongod.conf
登录
mongo --port=28017
use admin
config = {_id: 'my_repl', members: [
{_id: 0, host: '10.0.0.134:28017'},
{_id: 1, host: '10.0.0.134:28018'},
{_id: 2, host: '10.0.0.134:28019'}]
}
rs.initiate(config)
use test;
db.movies.insert([ { "title" : "Jaws", "year" : 1975, "imdb_rating" : 8.1 },{ "title" : "Batman", "year" : 1989, "imdb_rating" : 7.6 },
] );
#复制集状态查询 在哪个节点都可以看
[mongod@mongo ~]$ mongo --port=28017
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28017/test
my_repl:PRIMARY> rs.status();
{
"set" : "my_repl",
"date" : ISODate("2018-01-04T09:06:52.951Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "10.0.0.134:28017", #ip port
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", "PRIMARY" 主节点
"uptime" : 772,
"optime" : {
"ts" : Timestamp(1515056188, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T08:56:28Z"),
"electionTime" : Timestamp(1515056187, 1),
"electionDate" : ISODate("2018-01-04T08:56:27Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "10.0.0.134:28018", #ip port
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #从节点
"uptime" : 635,
"optime" : {
"ts" : Timestamp(1515056188, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T08:56:28Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:06:52.289Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:06:51.722Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "10.0.0.134:28019", #ip port
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #从节点
"uptime" : 635,
"optime" : {
"ts" : Timestamp(1515056188, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T08:56:28Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:06:52.289Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:06:51.601Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28017",
"configVersion" : 1
}
],
"ok" : 1
}
#查看当前是否为主节点
#主节点检测
my_repl:PRIMARY> rs.isMaster();
{
"hosts" : [
"10.0.0.134:28017",
"10.0.0.134:28018",
"10.0.0.134:28019"
],
"setName" : "my_repl",
"setVersion" : 1,
"ismaster" : true, #主节点
"secondary" : false,
"primary" : "10.0.0.134:28017",
"me" : "10.0.0.134:28017",
"electionId" : ObjectId("7fffffff0000000000000001"),
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2018-01-04T09:11:15.204Z"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1
}
#从节点检测
[mongod@mongo ~]$ mongo --port=28018
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28018/test
my_repl:SECONDARY> rs.isMaster()
{
"hosts" : [
"10.0.0.134:28017",
"10.0.0.134:28018",
"10.0.0.134:28019"
],
"setName" : "my_repl",
"setVersion" : 1,
"ismaster" : false,
"secondary" : true, #从节点
"primary" : "10.0.0.134:28017",
"me" : "10.0.0.134:28018",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2018-01-04T09:11:40.092Z"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1
}
#在主节点上添加节点
my_repl:PRIMARY> rs.add ("10.0.0.134:28020") #添加从节点
{ "ok" : 1 }
my_repl:PRIMARY> rs.status()
{
"set" : "my_repl",
"date" : ISODate("2018-01-04T09:16:33.144Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "10.0.0.134:28017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1353,
"optime" : {
"ts" : Timestamp(1515057249, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:14:09Z"),
"electionTime" : Timestamp(1515056187, 1),
"electionDate" : ISODate("2018-01-04T08:56:27Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "10.0.0.134:28018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1216,
"optime" : {
"ts" : Timestamp(1515057249, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:14:09Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:16:31.632Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:16:31.693Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28017",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "10.0.0.134:28019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1216,
"optime" : {
"ts" : Timestamp(1515057249, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:14:09Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:16:31.632Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:16:31.693Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28017",
"configVersion" : 2
},
{
"_id" : 3,
"name" : "10.0.0.134:28020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #从节点10.0.0.134:28020
"uptime" : 143,
"optime" : {
"ts" : Timestamp(1515057249, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:14:09Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:16:31.647Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:16:30.645Z"),
"pingMs" : NumberLong(0),
"configVersion" : 2
}
],
"ok" : 1
}
#删除节点
my_repl:PRIMARY> rs.remove("10.0.0.134:28020") #删除从节点
{ "ok" : 1 }
my_repl:PRIMARY> rs.status()
{
"set" : "my_repl",
"date" : ISODate("2018-01-04T09:18:10.582Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "10.0.0.134:28017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1450,
"optime" : {
"ts" : Timestamp(1515057484, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:18:04Z"),
"electionTime" : Timestamp(1515056187, 1),
"electionDate" : ISODate("2018-01-04T08:56:27Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.0.0.134:28018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1313,
"optime" : {
"ts" : Timestamp(1515057484, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:18:04Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:18:10.231Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:18:10.241Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.0.0.134:28019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1313,
"optime" : {
"ts" : Timestamp(1515057484, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:18:04Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:18:10.231Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:18:10.243Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28017",
"configVersion" : 3
}
],
"ok" : 1
}
#添加仲裁节点
my_repl:PRIMARY> rs.addArb("10.0.0.134:28020");#添加仲裁节点
{ "ok" : 1 }
my_repl:PRIMARY> rs.status();
{
"set" : "my_repl",
"date" : ISODate("2018-01-04T09:19:32.814Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "10.0.0.134:28017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1532,
"optime" : {
"ts" : Timestamp(1515057570, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:19:30Z"),
"electionTime" : Timestamp(1515056187, 1),
"electionDate" : ISODate("2018-01-04T08:56:27Z"),
"configVersion" : 6,
"self" : true
},
{
"_id" : 1,
"name" : "10.0.0.134:28018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1395,
"optime" : {
"ts" : Timestamp(1515057570, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:19:30Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:19:30.915Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:19:30.926Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28017",
"configVersion" : 5
},
{
"_id" : 2,
"name" : "10.0.0.134:28019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1395,
"optime" : {
"ts" : Timestamp(1515057534, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-01-04T09:18:54Z"),
"lastHeartbeat" : ISODate("2018-01-04T09:19:30.902Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:19:30.922Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28017",
"configVersion" : 5
},
{
"_id" : 4,
"name" : "10.0.0.134:28020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER", #仲裁节点
"uptime" : 34,
"lastHeartbeat" : ISODate("2018-01-04T09:19:30.903Z"),
"lastHeartbeatRecv" : ISODate("2018-01-04T09:19:30.924Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.0.0.134:28019",
"configVersion" : 5
}
],
"ok" : 1
}
#主、从、仲裁节点对比
#主节点操作
[mongod@mongo ~]$ mongo --port=28017
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28017/test
my_repl:PRIMARY> use test
switched to db test
my_repl:PRIMARY> db.anyux.insert([{"language":"C"},{"language":"C++"}]) #插入数据
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
my_repl:PRIMARY> db.anyux.find().pretty() #主节点显示数据
{ "_id" : ObjectId("5a4df337c702312dedac9176"), "language" : "C" }
{ "_id" : ObjectId("5a4df337c702312dedac9177"), "language" : "C++" }
#从节点操作
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28018/test
my_repl:SECONDARY> use test
my_repl:SECONDARY> rs.slaveOk();
my_repl:SECONDARY> db.anyux.find().pretty() #从节点显示数据
{ "_id" : ObjectId("5a4df337c702312dedac9176"), "language" : "C" }
{ "_id" : ObjectId("5a4df337c702312dedac9177"), "language" : "C++" }
#仲裁节点
[mongod@mongo ~]$ mongo --port=28020
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28020/test
my_repl:ARBITER> rs.slaveOk();
my_repl:ARBITER> show dbs;
local 0.000GB
test 0.000GB
my_repl:ARBITER> db.anyux.find().pretty(); #仲裁节点不会存储数据
Error: error: { "ok" : 0, "errmsg" : "node is recovering", "code" : 13436 }