MongoDB 搭建可复制群集

一、概述

MongoDB复制群集支持节点故障自动切换,最小配置应包含3个节点,正常情况下应该至少包含两个数据节点,第三个节点可以是数据节点也可以是仲裁节点。仲裁节点的作用是当出现偶数节点导致无法仲裁的时候参与进来进行投票使之变成奇数个投票点,仲裁节点可以看成是不包含任何数据集的副本节点。仲裁节点并不需要太多系统资源,仅仅只是参与投票,可随意部署在网络通的地方不会占用什么系统资源;而数据节点保存了数据,需要占用比较大的磁盘空间、内存等硬件资源,成本比仲裁节点高很多,但是额外提供了一个数据副本,相当于更多一层保障。

 

 

版本:MongoDB3.6.2

OS:Centos6.7

节点1:192.168.137.10:27010 ;目录/mongos27010

节点2:192.168.137.10:27011;目录/mongos27011

节点3:192.168.137.10:27012;目录/mongos27012

二、搭建

基本架构如下:

 

1.安装Mongodb 

解压

tar -xvf mongodb-linux-x86_64-rhel62-3.6.2.tgz

mv mongodb-linux-x86_64-rhel62-3.6.2 mongos27010

创建相关目录

cd /mongos27010
mkdir -p data/db 
mkdir -p data/log

加入环境变量

vim /etc/profile
export PATH=/mongos27010/bin:$PATH

source /etc/profile

启动服务

/mongos27010/bin/mongod  --maxConns 20000  --config  /mongos27010/mongodb.conf

参数配置

net:
  maxIncomingConnections: 6000
  port: 27017
operationProfiling:
  mode: slowOp
  slowOpThresholdMs: 200
processManagement:
  fork: "true"
replication:
  oplogSizeMB: 20480
  replSetName: test
security:
  authorization: enabled
  clusterAuthMode: keyFile
  keyFile: /var/lib/mongodb-mms-automation/keyfile
setParameter:
  enableLocalhostAuthBypass: "true"
storage:
  dbPath: /data/mongo/test
  directoryPerDB: true
  engine: wiredTiger
  indexBuildRetry: true
  journal:
    enabled: true
  wiredTiger:
    collectionConfig:
      blockCompressor: snappy
    engineConfig:
      journalCompressor: snappy
      cacheSizeGB: 2
    indexConfig:
      prefixCompression: true
systemLog:
  destination: file
  logAppend: true
  path: /data/mongo/test/mongodb.log
  quiet: true

 

生成keyfile文件

openssl rand -base64 756 > /mongodb27010/autokey

chmod 400 /mongodb27010/autokey

3.添加群集

启动三个节点服务

/mongos27010/bin/mongod  --maxConns 20000  --config  /mongos27010/mongodb.conf
/mongos27011/bin/mongod  --maxConns 20000  --config  /mongos27011/mongodb.conf
/mongos27012/bin/mongod  --maxConns 20000  --config  /mongos27012/mongodb.conf

登入到主节点27010,添加其它两个节点

1.登入
mongo --port 27010
2.切换admin数据库
use admin4.初始化复制集,默认会创建当前节点为主节点的副本集
rs.initiate()
5.退出重新登入
6.添加数据节点27011,可以用机器名加端口也可以使用IP加端口
rs.add("192.168.137.10:27011")
7.添加数据节点27012,可以用机器名加端口也可以使用IP加端口
rs.add("192.168.137.10:27012")

或者通过配置文件初始化副本集

conf={_id:'rs-a',members:[
{_id:0,host:'192.168.137.10:27010',priority:5},
{_id:1,host:'192.168.137.10:27011',priority:1},
{_id:2,host:'192.168.137.10:27012',arbiterOnly:true,priority:1}]
}
rs.initiate(conf)
----创建用户
use admin
db.createUser({
    user:"dba",
    pwd:"dba",
    roles:[{role:"root",db:"admin"}]});

 

注意:添加节点后重启所有节点服务

注意:当前我已经把节点27010和27011的优先级都设成了10,节点27012的优先级设成了0,所以节点27012永远不会成为主节点。

arbiterOnly:表示该成员是仲裁者,arbiter的唯一作用是就是参与选举,其votes属性是1,arbiter不保存数据,也不会为client提供服务。

buildIndexes:表示是否在该成员上创建Index,该属性不能修改,只能在增加成员时设置该属性。如果一个成员仅仅作为备份,不接收Client的请求,将该成员设置为不创建index,能够提高数据同步的效率。

priority:表示一个成员被选举为Primary节点的优先级,默认值是1,取值范围是从0到100,将priority设置为0有特殊含义:Priority为0的成员永远不能成为Primary 节点。Replica Set中,Priority最高的成员,会优先被选举为Primary 节点,只要其满足条件。

hidden:将成员配置为隐藏成员,要求Priority 为0。Client不会向隐藏成员发送请求,因此隐藏成员不会收到Client的Request。

slaveDelay:单位是秒,将Secondary 成员配置为延迟备份节点,要求Priority 为0,表示该成员比Primary 成员滞后指定的时间,才能将Primary上进行的写操作同步到本地。为了数据读取的一致性,应将延迟备份节点的hidden设置为true,避免用户读取到明显滞后的数据。Delayed members maintain a copy of the data that reflects the state of the data at some time in the past.

votes:有效值是0或1,默认值是1,如果votes是1,表示该成员(voting member)有权限选举Primary 成员。在一个Replica Set中,最多有7个成员,其votes 属性的值是1。

注意:当前我已经把节点27012的优先级(priority)设成了0,所以节点27012在passives下面去了。

 

-----第三个副本节点为仲裁节点,不存储数据只充当仲裁,不存储数据也不接收业务请求
config ={
     "_id" : "abc_set",
     "members" : [
          {
               "_id" : 0,
               "host" : "111:30000"
          },
          {
               "_id" : 1,
               "host" : "222:30000"
          },
          {
               "_id" : 2,
               "host" : "333:30000",
               "arbiterOnly":true
          }
     ]
};
-----第三个副本节点为隐藏节点,要求Priority 为0。不参与投票,不接受业务请求,但是会存储数据
config ={
     "_id" : "abc_set",
     "members" : [
          {
               "_id" : 0,
               "host" : "111:30000"
          },
          {
               "_id" : 1,
               "host" : "222:30000"
          },
          {
               "_id" : 2,
               "host" : "333:30000",
               "priority" : 0,
               "hidden" : true
          }
     ]
}

4.故障测试

停止27010节点服务

登入节点27011查询,rs.status()

 主节点已经切换到了27012节点

三、其它相关配置

1.配置节点优先级

在主节点中配置

cfg = rs.conf();
cfg.members[0].priority = 10
cfg.members[1].priority = 10
cfg.members[2].priority = 5
rs.reconfig(cfg)
rs.conf();

注意:1.members[n]指的是rs.conf()查询中"_id"节点标志。如果将某个节点的priority设为0那么它将永远不会成为Primary节点。

2.这里的cfg只是一个别名,可以随意取。

3.主和备主的优先级建议设置成一样,避免主重启之后又进行主切换。

2.关闭节点投票

cfg = rs.conf()
cfg.members[2].votes = 0;
rs.reconfig(cfg)
rs.conf();

注意:将节点votes设为0那么该节点将不具有投票权。

3.添加仲裁节点

rs.addArb("localhost:27012")

注意:添加节点后需要重启服务

4.删除节点

删除仲裁节点命令相同
rs.remove("localhost:27012")

注意:删除节点后要重启删除节点的服务,同时将删除节点的conf配置文件的replSet参数去掉。

5.查询群集相关状态

rs.status

"_id" :  #集群中节点编号,从0开始  
"name" :  #成员服务器名称及端口  
"health" :  #表示成员中的健康状态(0:down;1:up)  
"state" :  #为0~10,表示成员的当前状态  
"stateStr" :  #描述该成员是主库(PRIMARY)还是备库(SECONDARY)  
"uptime" :  #该成员在线时间(秒),从成员可达到现在所经历的时间。对于当前self成员,该时间是从成员启动一直到现在的时间。 
"optime" :  #成员最后一次应用日志(oplog)的信息  
"optimeDate" :  #成员最后一次应用日志(oplog)的时间
"electionTime" : #当前primary从操作日志中选举信息 
"electionDate" :  #当前primary被选定为primary的日期  
"configVersion" :  #mongodb版本  
"self" :  #为true 表示当前节点 
-------===========非当前成员信息===========-----------
"lastHeartbeat" :#当前服务器最后一次收到拥有该信息成员心跳的时间
"pingMs" : 心跳从当前服务器到到达拥有该信息成员服务器所花费的平均时间,可以从这个字段选择从哪个成员进行同步 。
"syncingTo":当前成员正在从哪个成员处进行复制。

参考:https://docs.mongodb.com/manual/reference/command/replSetGetStatus/

rs.conf()

查询群集相关配置,包括优先级、投票等

参考:https://docs.mongodb.com/manual/reference/replica-configuration/

db.isMaster()

"passives" :priority=0的标志
"setName":群集名称
"ismaster":是否是主节点
"secondary":是否是从节点
 

参考:https://docs.mongodb.com/manual/reference/command/isMaster/

NameDescription
applyOps Internal command that applies oplog entries to the current data set.
isMaster Displays information about this member’s role in the replica set, including whether it is the master.
replSetAbortPrimaryCatchUp Forces the elected primary to abort sync (catch up) then complete the transition to primary.
replSetFreeze Prevents the current member from seeking election as primary for a period of time.
replSetGetConfig Returns the replica set’s configuration object.
replSetGetStatus Returns a document that reports on the status of the replica set.
replSetInitiate Initializes a new replica set.
replSetMaintenance Enables or disables a maintenance mode, which puts a secondary node in a RECOVERING state.
replSetReconfig Applies a new configuration to an existing replica set.
replSetResizeOplog Dynamically resizes the oplog for a replica set member. Available for WiredTiger storage engine only.
replSetStepDown Forces the current primary to step down and become a secondary, forcing an election.
replSetSyncFrom Explicitly override the default logic for selecting a member to replicate from.
resync Forces a mongod to re-synchronize from the master. For master-slave replication only.

参考:https://docs.mongodb.com/manual/reference/command/nav-replication/

rs.printReplicationInfo()

查看oplog状态

rs.printSlaveReplicationInfo()

查看复制延迟

db.serverStatus()

查看服务状态详情

6.关闭服务

db.shutdownServer()

如果当前只剩下主节点需要执行带“force:true”参数的关闭服务命令

db.shutdownServer({"force":true});

7.配置启动服务

vim /etc/init.d/mongos27010

ulimit -SHn 655350
#!/bin/sh
# chkconfig: - 64 36
# description:mongod
case $1 in
start)
/mongos27010/bin/mongod  --maxConns 20000  --config  /mongos27010/mongodb.conf
;;
stop)
/mongos27010/bin/mongo -u dba -p dba 192.168.137.10:27010/admin --eval "db.shutdownServer()"
;;
status)
/mongos27010/bin/mongo -u dba -p dba  192.168.137.10:27010/admin --eval "db.stats()"
;;
esac

加入开启自动启动

chkconfig --level 345 mongos27010 on

四、总结 

如果是首次搭建可能会遇到各种小问题,但是只要细心查找还是很容易解决;整体来说搭建一个MongoDB复制群集还是比较简单。

 

 

 

备注:

    作者:pursuer.chen

    博客:http://www.cnblogs.com/chenmh

本站点所有随笔都是原创,欢迎大家转载;但转载时必须注明文章来源,且在文章开头明显处给明链接,否则保留追究责任的权利。

《欢迎交流讨论》

 

posted @ 2018-03-01 08:57  pursuer.chen  阅读(2491)  评论(0编辑  收藏  举报