mongodb
主从模式就是一个 单副本的应用;没有很好的扩展性和容错性;
副本集的多个副本保证了容错性;
主服务器负责整个副本集的读写,副本集定时同步数据;主节点挂掉;副本集会自动选举一个主的服务器;
1,安装配置;
主节点:
172.16.0.191
从点节点:
172.16.0.192
172.16.0.193
# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.10.tgz
# tar -xf mongodb-linux-x86_64-rhel62-3.2.10.tgz -C /opt
# mv mongodb-linux-x86_64-rhel62-3.2.10.tgz mongod
# mkdir /data/replset/db # 设置副本集文件目录 db数据目录
2,启动三台mongod;(防火墙开放 27017 port )
/opt/mongod/bin/mongod --fork --port 27017 --dbpath /data/db --logpath /opt/mongod/log/mongodb.log --replSet oneset
这边用172.16.0.191 作为主节点服务;
3,进入mongod 配置副本集;
# /opt/mongod/bin/mongo
# 使用admin 数据库
> use admin
switched to db admin
#定义副本集配置变量,这里的 _id:”oneset” 和上面命令参数“ –replSet oneset” 要保持一样。(可以自定义)
> config = { _:"oneset", members:[ {_id:0,host:"172.16.0.191:27017"}, {_id:1,host:"172.16.0.192:27017"}, {_id:2,host:"172.16.0.193:27017"}] }
{
"_id" : "oneset",
"members" : [
{
"_id" : 0,
"host" : "172.16.15.191:27017"
},
{
"_id" : 1,
"host" : "172.16.15.192:27017"
},
{
"_id" : 2,
"host" : "172.16.15.19327017"
}
]
}
#初始化副本集配置
>rs.initiate(config);
4,查阅副本集
>rs.conf() 或者 >rs.status()
5,任意一个从节点连接查看副本集状态:
[root@mongodb3 mongod]# bin/mongo 172.16.0.191:27017
repset:PRIMARY> rs.status()
{
"set" : "repset",
"date" : ISODate("2016-10-12T08:21:20.622Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "172.16.0.191:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1534,
"optime" : {
"ts" : Timestamp(1476260204, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-10-12T08:16:44Z"),
"electionTime" : Timestamp(1476259079, 1),
"electionDate" : ISODate("2016-10-12T07:57:59Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "172.16.0.192:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1411,
"optime" : {
"ts" : Timestamp(1476260204, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-10-12T08:16:44Z"),
"lastHeartbeat" : ISODate("2016-10-12T08:21:19.392Z"),
"lastHeartbeatRecv" : ISODate("2016-10-12T08:21:18.816Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.16.0.191:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.16.0.193:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1411,
"optime" : {
"ts" : Timestamp(1476260204, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-10-12T08:16:44Z"),
"lastHeartbeat" : ISODate("2016-10-12T08:21:19.390Z"),
"lastHeartbeatRecv" : ISODate("2016-10-12T08:21:18.941Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.16.0.191:27017",
"configVersion" : 1
}
],
"ok" : 1
}
6, 主节点插入一条测试数据;
repset:PRIMARY> use abcd
switched to db abcd
repset:PRIMARY> db.abcddb.insert({"name":"字母"})
WriteResult({ "nInserted" : 1 })
7,从节点查看;
# /opt/mongod/bin/mongo 172.16.0.191:27017
再:
# /opt/mongod/bin/mongo
# mongodb默认是从主节点读写数据的,副本节点上不允许读,需要设置副本节点可以读。
repset:SECONDARY> db.getMongo().setSlaveOk();
repset:SECONDARY> use abcd;
switched to db abcd
repset:SECONDARY> db.abcddb.find();
{ "_id" : ObjectId("57fe09dd38d5a26b011dfe06"), "name" : "字母" }}
8,加添节点
添加节点,需要在主节点进行
PRIMARY>>rs.add(hostname:port)
9,删减节点
删减节点,需要在主节点进行
PRIMARY>rs.remove(hostname:port)
本文出自 “logs” 博客,请务必保留此出处http://51log.blog.51cto.com/6076767/1861215
原文地址:http://51log.blog.51cto.com/6076767/1861215