在CentOS6.6上以replSet方式部署MongoDB集群
此文已由作者袁欢授权网易云社区发布。
欢迎访问网易云社区,了解更多网易技术产品运营经验。
部署方式
在3台centos6.6系统上以Replica Set方式部署mongodb3.0.2集群。
官方参考资料:
http://docs.mongodb.org/manual/tutorial/deploy-replica-set/
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/
集群的IP地址分配
# node01: 192.168.35.173(PRIMARY node)
# node02: 192.168.35.96(SECONDARY node)
# node03: 192.168.35.174(SECONDARY node)
# 添加mongodb的yum源(所有节点)
echo "[mongodb-org-3.0]" > /etc/yum.repos.d/mongodb-org-3.0.repo
echo "name=MongoDB Repository" >> /etc/yum.repos.d/mongodb-org-3.0.repo
echo "baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/" >> /etc/yum.repos.d/mongodb-org-3.0.repo
echo "gpgcheck=0" >> /etc/yum.repos.d/mongodb-org-3.0.repo
echo "enabled=1" >> /etc/yum.repos.d/mongodb-org-3.0.repo
# yum安装mongodb(所有节点)
yum install -y mongodb-org-3.0.2 mongodb-org-server-3.0.2 mongodb-org-shell-3.0.2 mongodb-org-mongos-3.0.2 mongodb-org-tools-3.0.2
mkdir -p /data/db && chown -R mongod.mongod /data/
# 修改主机名和hosts文件(所有节点)
# vim /etc/sysconfig/network
# sysctl kernel.hostname=node0001.nemongo.com
# sysctl kernel.hostname=node0002.nemongo.com
# sysctl kernel.hostname=node0003.nemongo.com
echo "192.168.35.173 node0001.nemongo.com" >> /etc/hosts
echo "192.168.35.96 node0002.nemongo.com" >> /etc/hosts
echo "192.168.35.174 node0003.nemongo.com" >> /etc/hosts
# 修改mongodb的配置文件(所有节点)
sed -i 's/bind_ip=127.0.0.1/#bind_ip=127.0.0.1/g' /etc/mongod.conf
sed -i 's/#replSet=setname/replSet=ne_aws_01/g' /etc/mongod.conf
# 启动mongodb(所有节点)
service mongod start
# 在mongodb主节点上配置集群(PRIMARY 节点)
mongo
> rs.initiate() # 只有node0001上面需要执行rs.initiate()
> rs.conf()
> rs.add("node0002.nemongo.com") # node0002上面不要执行rs.initiate(),否则无法添加
> rs.add("node0003.nemongo.com") # node0003上面不要执行rs.initiate(),否则无法添加
> rs.status() # 每个节点上都可以查看集群状态
> rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "node0001.nemongo.com:27017", "ok" : 0, "errmsg" : "No host described in new configuration 1 for replica set ne_aws_01 maps to this node", "code" : 93 } ne_aws_01:PRIMARY> rs.add("node0002.nemongo.com:27017") { "ok" : 1 } ne_aws_01:PRIMARY> rs.add("node0003.nemongo.com") { "ok" : 1 } ne_aws_01:PRIMARY> rs.status() { "set" : "ne_aws_01", "date" : ISODate("2015-05-08T04:49:30.768Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "node0001.nemongo.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1471, "optime" : Timestamp(1431060383, 1), "optimeDate" : ISODate("2015-05-08T04:46:23Z"), "electionTime" : Timestamp(1431059099, 1), "electionDate" : ISODate("2015-05-08T04:24:59Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "node0002.nemongo.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 192, "optime" : Timestamp(1431060383, 1), "optimeDate" : ISODate("2015-05-08T04:46:23Z"), "lastHeartbeat" : ISODate("2015-05-08T04:49:29.756Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T04:49:30.263Z"), "pingMs" : 1, "configVersion" : 3 }, { "_id" : 2, "name" : "node0003.nemongo.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 187, "optime" : Timestamp(1431060383, 1), "optimeDate" : ISODate("2015-05-08T04:46:23Z"), "lastHeartbeat" : ISODate("2015-05-08T04:49:29.756Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T04:49:29.795Z"), "pingMs" : 1, "lastHeartbeatMessage" : "could not find member to sync from", "configVersion" : 3 } ], "ok" : 1 } |
将mongodb主节点停掉:service mongod stop,这时候mongodb集群将会重新选举产生新的primary节点,在node0002上查看集群状态:
ne_aws_01:PRIMARY> rs.status() { "set" : "ne_aws_01", "date" : ISODate("2015-05-08T04:51:13.359Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "node0001.nemongo.com:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2015-05-08T04:51:11.361Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T04:51:11.951Z"), "pingMs" : 1, "lastHeartbeatMessage" : "Failed attempt to connect to node0001.nemongo.com:27017; couldn't connect to server node0001.nemongo.com:27017 (192.168.35.173), connection attempt failed", "configVersion" : -1 }, { "_id" : 1, "name" : "node0002.nemongo.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 364, "optime" : Timestamp(1431060383, 1), "optimeDate" : ISODate("2015-05-08T04:46:23Z"), "electionTime" : Timestamp(1431060617, 1), "electionDate" : ISODate("2015-05-08T04:50:17Z"), "configVersion" : 3, "self" : true }, { "_id" : 2, "name" : "node0003.nemongo.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 284, "optime" : Timestamp(1431060383, 1), "optimeDate" : ISODate("2015-05-08T04:46:23Z"), "lastHeartbeat" : ISODate("2015-05-08T04:51:13.159Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T04:51:12.703Z"), "pingMs" : 1, "configVersion" : 3 } ], "ok" : 1 } |
将node0001重新启动:service mongod start,再次查看mongodb集群状态,就会发现node0001变成了SECONDARY节点:
ne_aws_01:SECONDARY> rs.status() { "set" : "ne_aws_01", "date" : ISODate("2015-05-08T04:53:54.550Z"), "myState" : 2, "members" : [ { "_id" : 0, "name" : "node0001.nemongo.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 168, "optime" : Timestamp(1431060383, 1), "optimeDate" : ISODate("2015-05-08T04:46:23Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "node0002.nemongo.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 167, "optime" : Timestamp(1431060383, 1), "optimeDate" : ISODate("2015-05-08T04:46:23Z"), "lastHeartbeat" : ISODate("2015-05-08T04:53:53.474Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T04:53:52.881Z"), "pingMs" : 1, "electionTime" : Timestamp(1431060617, 1), "electionDate" : ISODate("2015-05-08T04:50:17Z"), "configVersion" : 3 }, { "_id" : 2, "name" : "node0003.nemongo.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 167, "optime" : Timestamp(1431060383, 1), "optimeDate" : ISODate("2015-05-08T04:46:23Z"), "lastHeartbeat" : ISODate("2015-05-08T04:53:53.474Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T04:53:54.382Z"), "pingMs" : 1, "configVersion" : 3 } ], "ok" : 1 } |
更多网易技术、产品、运营经验分享请点击。
相关文章:
【推荐】 NOS直传加速服务
【推荐】 【工程实践】服务器数据解析