当前位置:Gxlcms > 数据库问题 > mongodb复制集部署

mongodb复制集部署

时间:2021-07-01 10:21:17 帮助过:24人阅读

实例来部署一个由三个节点组成的 复制集

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、建立每个节点都建立据据目录

[root@hadoop1 ~]# mkidr -pv /mongodb/data/

[root@hadoop1 ~]# chown mongod.mongod /mongodb/data/

2

将复制集中的每个节点以适当的配置参数启动。

在每个节点上启动 mongod 并通过制定 replSet 参数来指定其复制集名,并可以指定其他需要的参数

 

[root@hadoop1 ~]# vim /etc/mongod.conf

//添加如下

#Replica Set
replSet = testrs0

或者

[root@hadoop1 ~]# mongod --replSet "testrs0"

 

确保每个节点都有相同复制集名称

[root@hadoop1 ~]# scp /etc/mongod.conf root@hadoop2:/etc/;scp /etc/mongod.conf root@hadoop2:/etc/;

 

注意了,如果解决启动mongod 时,出现addr already in use错误,原因启动端口被占用

[root@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掉

[root@hadoop1 ~]# netstat -anp|more

unix  2      [ ACC ]     STREAM     LISTENING     15588  2174/mongod         /tmp/mongodb-27017.sock

 

[root@hadoop1 ~]# kill 2174

[root@hadoop1 ~]# /etc/init.d/mongod start
Starting mongod:                                           [确定]

[root@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

 

[root@hadoop1 ~]# service mongod stop
Stopping mongod:                                           [确定]
You have new mail in /var/spool/mail/root
[root@hadoop1 ~]# vim /etc/mongod.conf

开启了

bind 127.0.0.1 把本地限制访问了

#bind 127.0.0.1

 

[root@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() 来将剩下的节点加入复制集。

 

 

mongodb复制集部署

标签:mongodb复制集部署

人气教程排行