设为首页 收藏本站
查看: 2343|回复: 0

[经验分享] mongodb复制集部署

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-30 08:59:00 | 显示全部楼层 |阅读模式
部署复制集
由三个节点组成的 复制集 为网络故障或是其他的系统故障提供了足够的冗余。该复制集也有足够的分布式读操作的能力。复制集应该保持奇数个节点,这也就保证了 选举 可以正常的进行
用3台已有的 mongod 实例来部署一个由三个节点组成的 复制集
192.168.1.3 hadoop1.abc.com hadoop1
192.168.1.4 hadoop2.abc.com hadoop2
192.168.1.5 hadoop3.abc.com hadoop3


部署复制集的注意事项架构在生产环境中,我们应该将每个节点部署在独立的机器上,并使用标准的MongoDB端口 27017 。使用 bind_ip 参数来限制访问MongoDB的应用程序的地址。
若使用了异地分布式架构的复制集,请确保多数 mongod 实例节点位于主数据中心中。

连通性确保各个节点之间可以正常通讯,且各个客户端都处于安全的可信的网络环境中。可以考虑以下事项:
  • 建立虚拟的专用网络。确保各个节点之间的流量是在本地网络范围内路由的。(Establish a virtual private network. Ensure that your network topology routes all traffic between members within a single site over the local area network.)
  • 配置连接限制来防止未知的客户端连接到复制集。
  • 配置网络设置和防火墙规则来对将MongoDB的端口仅开放给应用程序,来让应用程序发的进出数据包可以与MongoDB正常交流。

最后请确保复制集各节点可以互相通过DNS或是主机名解析。我们需要配置DNS域名或是设置 /etc/hosts 文件来配置。

这里实验,是关闭防火墙,并把selinux设置成setenforce 0
详细步骤1、建立每个节点都建立据据目录
[iyunv@hadoop1 ~]# mkidr -pv /mongodb/data/
[iyunv@hadoop1 ~]# chown mongod.mongod /mongodb/data/
2将复制集中的每个节点以适当的配置参数启动。在每个节点上启动 mongod 并通过制定 replSet 参数来指定其复制集名,并可以指定其他需要的参数

[iyunv@hadoop1 ~]# vim /etc/mongod.conf
//添加如下
#Replica Set
replSet = testrs0
或者
[iyunv@hadoop1 ~]# mongod --replSet "testrs0"

确保每个节点都有相同复制集名称
[iyunv@hadoop1 ~]# scp /etc/mongod.conf root@hadoop2:/etc/;scp /etc/mongod.conf root@hadoop2:/etc/;

注意了,如果解决启动mongod 时,出现addr already in use错误,原因启动端口被占用
[iyunv@hadoop1 data]# mongod
2015-07-29T19:15:51.728+0800 E NETWORK  [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2015-07-29T19:15:51.728+0800 E NETWORK  [initandlisten]   addr already in use
2015-07-29T19:15:51.729+0800 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2015-07-29T19:15:51.729+0800 I CONTROL  [initandlisten] dbexit:  rc: 100

把端口找出来,kill掉
[iyunv@hadoop1 ~]# netstat -anp|more
unix  2      [ ACC ]     STREAM     LISTENING     15588  2174/mongod         /tmp/mongodb-27017.sock

[iyunv@hadoop1 ~]# kill 2174
[iyunv@hadoop1 ~]# /etc/init.d/mongod start
Starting mongod:                                           [确定]
3、打开 mongo 界面并连接到复制集的某个节点。[iyunv@hadoop1 ~]# mongo
4、初始化复制集。//使用rs.initiate()命令,MongoDB将初始化一个由当前节点构成、拥有默认配置的复制集。

> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "hadoop1.abc.com:27017",
"info" : "try querying local.system.replset to see current configuration",
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23
}
> rs.status()
{
"state" : 10,
"stateStr" : "REMOVED",
"uptime" : 38,
"optime" : Timestamp(1438168698, 1),
"optimeDate" : ISODate("2015-07-29T11:18:18Z"),
"ok" : 0,
"errmsg" : "Our replica set config is invalid or we are not a member of it",
"code" : 93
}
查看日志 文件
2015-07-29T20:00:45.433+0800 W NETWORK  [ReplicationExecutor] Failed to connect to 192.168.1.3:27017, reason: errno:111 Connection refused
2015-07-29T20:00:45.433+0800 W REPL     [ReplicationExecutor] Locally stored replica set configuration does not have a valid entry for the current node; waiting for reconfig or remote heartbeat; Got "NodeNotFound No host described in new configuration 1 for replica set testrs0 maps to this node" while validating { _id: "testrs0", version: 1, members: [ { _id: 0, host: "hadoop1.abc.com:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatTimeoutSecs: 10, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }
2015-07-29T20:00:45.433+0800 I REPL     [ReplicationExecutor] New replica set config in use: { _id: "testrs0", version: 1, members: [ { _id: 0, host: "hadoop1.abc.com:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatTimeoutSecs: 10, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }
2015-07-29T20:00:45.433+0800 I REPL     [ReplicationExecutor] This node is not a member of the config
2015-07-29T20:00:45.433+0800 I REPL     [ReplicationExecutor] transition to REMOVED
2015-07-29T20:00:45.433+0800 I REPL     [ReplicationExecutor] Starting replication applier threads
2015-07-29T20:00:49.067+0800 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:58852 #1 (1 connection now open)
2015-07-29T20:01:17.436+0800 I COMMAND  [conn1] replSet info initiate : no configuration specified.  Using a default configuration for the set
2015-07-29T20:01:17.436+0800 I COMMAND  [conn1] replSet created this configuration for initiation : { _id: "testrs0", version: 1, members: [ { _id: 0, host: "hadoop1.abc.com:27017" } ] }
2015-07-29T20:01:17.436+0800 I REPL     [conn1] replSetInitiate admin command received from client

[iyunv@hadoop1 ~]# service mongod stop
Stopping mongod:                                           [确定]
You have new mail in /var/spool/mail/root
[iyunv@hadoop1 ~]# vim /etc/mongod.conf
开启了
bind 127.0.0.1 把本地限制访问了
#bind 127.0.0.1

[iyunv@hadoop1 data]# service mongod start
Starting mongod:                                           [确定]

> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "hadoop1.abc.com:27017",
"info" : "try querying local.system.replset to see current configuration",
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23

testrs0:PRIMARY> rs.status()
{
"set" : "testrs0",
"date" : ISODate("2015-07-29T12:13:27.839Z"),
"myState" : 1,
"members" : [
  {
   "_id" : 0,
   "name" : "hadoop1.abc.com:27017",
   "health" : 1,
   "state" : 1,
   "stateStr" : "PRIMARY",
   "uptime" : 232,
   "optime" : Timestamp(1438168698, 1),
   "optimeDate" : ISODate("2015-07-29T11:18:18Z"),
   "electionTime" : Timestamp(1438171776, 1),
   "electionDate" : ISODate("2015-07-29T12:09:36Z"),
   "configVersion" : 1,
   "self" : true
  }
],
"ok" : 1
}

5、将其他的节点加入复制集。通过 rs.add() 来将剩下的节点加入复制集。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-92062-1-1.html 上篇帖子: mongodb搭建 下篇帖子: monogdb复制原理详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表