CentOS Linux下搭建MongoDB Replica Set(副本集)集群

MongoDB Replica Set集群搭建最少需要3台服务器。现在假设有三台服务器,IP分别是:

192.168.0.81
192.168.0.82
192.168.0.83

一、在三台服务器上搭建MongoDB
参考CentOS Linux下安装MongoDB 3.0安装。

二、配置MongoDB Replica Set集群

vi /etc/mongod.conf

根据下面的配置修改/etc/mongod.conf里的配置

# mongod.conf

#where to log
logpath=/var/log/mongodb/mongod.log

logappend=true

# fork and run in background
fork=true

#port=27017

dbpath=/var/lib/mongo

# location of pidfile
pidfilepath=/var/run/mongodb/mongod.pid

# Listen to local interface only. Comment out to listen on all interfaces. 
#bind_ip=192.168.0.31

# Disables write-ahead journaling
nojournal=true

# Enables periodic logging of CPU utilization and I/O wait
#cpu=true

# Turn on/off security.  Off is currently the default
#noauth=true
#auth=true

# Verbose logging output.
#verbose=true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck=true

# Enable db quota management
#quota=true

# Set oplogging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog=0
# Ignore query hints
#nohints=true

# Enable the HTTP interface (Defaults to port 28017).
#httpinterface=true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting=true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan=true

# Disable data file preallocation.
#noprealloc=true

# Specify .ns file size for new databases.
# nssize=<size>

storageEngine=wiredTiger
wiredTigerCacheSizeGB=10
wiredTigerStatisticsLogDelaySecs=0
wiredTigerJournalCompressor=snappy
wiredTigerDirectoryForIndexes=true
wiredTigerCollectionBlockCompressor=snappy
wiredTigerIndexPrefixCompression=1

# Replication Options

# in replicated mongo databases, specify the replica set name here
replSet=myrs
# maximum size in megabytes for replication operation log
oplogSize=10240
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile

修改后,重新启动MongoDB

/etc/init.d/mongod restart

三、配置主,备,仲裁节点
可以通过客户端连接mongodb,也可以直接在三个节点中选择一个连接mongodb。

mongo --host 192.168.0.81
>use admin  
>cfg={ _id:"myrs", members:[ {_id:0,host:'192.168.0.81:27017',priority:2}, {_id:1,host:'192.168.0.82:27017',priority:1},   
{_id:2,host:'192.168.0.83:27017',arbiterOnly:true}] }; 

使配置生效

>rs.initiate(cfg) 

cfg是可以任意的名字,当然最好不要是mongodb的关键字,conf,config都可以。最外层的_id表示replica set的名字,members里包含的是所有节点的地址以及优先级。优先级最高的即成为主节点,即这里的192.168.0.81:27017。特别注意的是,对于仲裁节点,需要有个特别的配置——arbiterOnly:true。这个千万不能少了,不然主备模式就不能生效。
配置的生效时间根据不同的机器配置会有长有短,配置不错的话基本上十几秒内就能生效,有的配置需要一两分钟。如果生效了,执行rs.status()命令会看到如下信息:

> rs.status();
{
	"set" : "myrs",
	"date" : ISODate("2015-05-26T07:28:56.023Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.0.81:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 20770,
			"optime" : Timestamp(1432623035, 1),
			"optimeDate" : ISODate("2015-05-26T06:50:35Z"),
			"electionTime" : Timestamp(1432604574, 1),
			"electionDate" : ISODate("2015-05-26T01:42:54Z"),
			"configVersion" : 1,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "192.168.0.82:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 20764,
			"optime" : Timestamp(1432623035, 1),
			"optimeDate" : ISODate("2015-05-26T06:50:35Z"),
			"lastHeartbeat" : ISODate("2015-05-26T07:28:55.794Z"),
			"lastHeartbeatRecv" : ISODate("2015-05-26T07:28:55.057Z"),
			"pingMs" : 0,
			"syncingTo" : "192.168.0.241:27017",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.0.83:27017",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 19750,
			"lastHeartbeat" : ISODate("2015-05-26T07:28:54.537Z"),
			"lastHeartbeatRecv" : ISODate("2015-05-26T07:28:54.036Z"),
			"pingMs" : 0,
			"configVersion" : 1
		}
	],
	"ok" : 1
}

四、让secondary支持读操作
对于replica set 中的secondary 节点默认是不可读的。在写多读少的应用中,使用Replica Sets来实现读写分离。通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作。
在secondary的mongodb shell里面支持读操作,进入secondary的mongodb shell

rs.slaveOk();

PHP代码里面支持secondary的读操作需要将readPreference设置为secondaryPreferred

除非注明,本博客文章均为原创,转载请以链接形式标明本文地址
本文地址: http://blog.cnwyhx.com/centos-linux-install-mongodb-replicaset/

Leave a Reply