mongodb副本集搭建

副本集使用了2台服务器,做的是一个比较简单的副本集。1台服务器为主服务器,使用中有数据,另外一台新增一个SECONDARY和ARBITER,主要为了备份数据和容错。

 

需要注意的地方是 bindIp 一定得写自己的内网地址,服务器之间的交流是内网地址。网上有的教程写0.0.0.0经过实验是不可行的,单纯写localhost会限制副本集必须是本机的。遇到问题多看官网!!!

主服务配置文件
storage:                                                                                                             
  dbPath: /data/mongodb                                                                                              
  journal:                                                                                                           
    enabled: true                                                                                                    
#  engine:                                                                                                           
#  mmapv1:                                                                                                           
#  wiredTiger:                                                                                                       
                                                                                                                     
# where to write logging data.                                                                                       
systemLog:                                                                                                           
  destination: file                                                                                                  
  logAppend: true                                                                                                    
  path: /var/log/mongodb/mongod.log                                                                                  
                                                                                                                     
# network interfaces                                                                                                 
net:                                                                                                                 
  port: 27055                                                                                                        
  bindIp: 127.0.0.1,10.28.57.152                                                                                     
                                                                                                                     
processManagement:                                                                                                   
  fork: true                                                                                                         
#security:                                                                                                           
#  authorization: enabled                                                                                            
                                                                                                                     
#operationProfiling:                                                                                                 
                                                                                                                     
replication:                                                                                                         
  replSetName: rs                                                                                                    
                                                                                                                     
#sharding:                                                                                                           

## Enterprise-Only Options:

#auditLog:

#snmp:
                                                       

  

SECONDARY配置文件

# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /var/lib/mongodb journal: enabled: true # engine: # mmapv1: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1,10.51.65.37 processManagement: fork: true #security: #operationProfiling: replication: replSetName: rs #sharding:

  

仲裁器配置文件

# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /var/lib/mongodbArbiter journal: enabled: true # engine: # mmapv1: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodbArbiter/mongod.log # network interfaces net: port: 27018 bindIp: 127.0.0.1,10.51.65.37 processManagement: fork: true #security: #operationProfiling: replication: replSetName: rs #sharding:

 

启动的时候建议直接使用配置文件启动,配置参数在配置文件里面写清楚。这样一目了然,不容易出错。启动代码类似:sudo mongod --config /etc/mongod.conf

replication下面的replSetName就是副本集名称都得写成一样的,这里统一写rs。有个地方需要注意:主机启动的时候host得改成内网IP不要使用localhost或者127.0.0.1,这样是无法把副本集加入的。这个地方花了我不少时间,需要注意!!!下图host里面不应该存在127.0.0.1之类本地配置

加副本集的时候可以通过config或者add都可以,怎么方便怎么来。需要注意副本集启动的时候会自动去主机备份数据和结构,不需要把旧数据导入。经过测试发现就算导入了数据,SECONDARY还是会去主机备份数据结构。内存不够会导致SECONDARY很快就挂掉。

添加仲裁服务器 rs.add({_id:3,host:'10.51.65.37:27018',arbiterOnly:true}) 

 

posted @ 2018-07-23 17:19  cshhs  阅读(333)  评论(0编辑  收藏  举报