1. 程式人生 > >Replica Set副本集方式的mongodb集群搭建

Replica Set副本集方式的mongodb集群搭建

replica set副本集方式的mongodb集群搭建


1.環境:

單臺服務器上開啟四個mongodb實例來實現mongodb的Replica Set副本集方式的集群搭建

2.配置文件:

master主實例配置文件:

[[email protected] ~]# cat /usr/local/mongodb/mongod.cnf

logpath=/data/mongodb-master/logs/mongodb.log

logappend = true

#fork and run in background

fork = true

port = 27017

dbpath=/data/mongodb-master/data

#location of pidfile

pidfilepath=/data/mongodb-master/mongod.pid

auth = true

keyFile = /tmp/mongo-keyfile

nohttpinterface=true

replSet=shard1


slave1實例配置文件:

[[email protected] ~]# cat /usr/local/mongodb/mongod1.cnf

logpath=/data/mongodb-slave/logs/mongodb.log

logappend = true

#fork and run in background

fork = true

port = 27018

dbpath=/data/mongodb-slave/data

#location of pidfile

pidfilepath=/data/mongodb-slave/mongod.pid

auth = true

keyFile = /tmp/mongo-keyfile

nohttpinterface=true

replSet=shard1


slave2實例配置文件:

[[email protected] ~]# cat /usr/local/mongodb/mongod2.cnf

logpath=/data/mongodb-slave1/logs/mongodb.log

logappend = true

#fork and run in background

fork = true

port = 27019

dbpath=/data/mongodb-slave1/data

#location of pidfile

pidfilepath=/data/mongodb-slave1/mongod.pid

auth = true

keyFile = /tmp/mongo-keyfile

nohttpinterface=true

replSet=shard1


arbiter沖裁節點實例部署:

[[email protected] ~]# cat /usr/local/mongodb/arbiter.cnf

logpath=/data/mongodb-arbiter/logs/mongodb.log

logappend = true

#fork and run in background

fork = true

port = 27020

dbpath=/data/mongodb-arbiter/data

#location of pidfile

pidfilepath=/data/mongodb-arbiter/mongod.pid

keyFile = /tmp/mongo-keyfile

nohttpinterface=true

replSet=shard1


3.單實例mongodb的啟動腳本:

[[email protected] data]# cat /etc/init.d/mongod

#!/bin/sh

## chkconfig: 2345 66 40

source /etc/profile

CONFIG=/usr/local/mongodb/mongod.cnf

PROGRAM=/usr/local/mongodb/bin/mongod

MONGOPID=`ps -ef | grep ‘mongod --config‘ | grep -v grep | awk ‘{print $2}‘`

test -x $PROGRAM || exit 0

case "$1" in

start)

echo "Starting MongoDB Server..."

$PROGRAM --config $CONFIG &

;;

stop)

echo "Stopping MongoDB Server..."

if [ ! -z "$MONGOPID" ]; then

kill -15 $MONGOPID

fi

;;

status)

if [ -z "$MONGOPID" ]; then

echo "MongoDB is not running!"

else

echo "MongoDB is running!("$MONGOPID")"

fi

;;

restart)

echo "Shutting down MongoDB Server..."

if [ ! -z "$MONGOPID" ]; then

kill -15 $MONGOPID

fi

echo "Starting MongoDB..."

$PROGRAM --config $CONFIG &

;;

*)

log_success_msg "Usage:/etc/init.d/mongod {start|stop|status|restart}"

exit 1

esac

exit 0

4.啟動mongodb實例:

[[email protected] data]# ps -ef|grep mongo

root 7226 1 0 13:12 ? 00:00:41 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/arbiter.cnf

root 11262 1 0 13:54 ? 00:00:29 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod.cnf

root 13097 1 0 14:22 ? 00:00:19 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod1.cnf

root 13214 1 0 14:22 ? 00:00:19 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod2.cnf


5.配置主,備,仲裁節點:

可以通過客戶端連接mongodb,也可以直接在三個節點中選擇一個連接mongodb。

登陸mongodb-master主實例來配置:

[[email protected] ~]# mongo 127.0.0.1:27017

MongoDB shell version: 3.0.5

connecting to: 127.0.0.1:27017/test

shard1:PRIMARY> use admin;

switched to db admin

> cfg={ _id:"shard1", members:[ {_id:0,host:‘127.0.0.1:27017‘,priority:3}, {_id:1,host:‘127.0.0.1:27018‘,priority:2},

... {_id:2,host:‘127.0.0.1:27019‘,priority:1}, {_id:3,host:‘127.0.0.1:27020‘,arbiterOnly:true} ] };

{

"_id" : "shard1",

"members" : [

{

"_id" : 0,

"host" : "127.0.0.1:27017",

"priority" : 3

},

{

"_id" : 1,

"host" : "127.0.0.1:27018",

"priority" : 2

},

{

"_id" : 2,

"host" : "127.0.0.1:27019",

"priority" : 1

},

{

"_id" : 3,

"host" : "127.0.0.1:27020",

"arbiterOnly" : true

}

]

}


> rs.initiate(cfg); (初始化配置

{ "ok" : 1 }


shard1:PRIMARY> rs.conf(); (查看配置信息

{

"_id" : "shard1",

"version" : 1,

"members" : [

{

"_id" : 0,

"host" : "127.0.0.1:27017",

"arbiterOnly" : false,

"buildIndexes" : true,

"hidden" : false,

"priority" : 3,

"tags" : {

},

"slaveDelay" : 0,

"votes" : 1

},

{

"_id" : 1,

"host" : "127.0.0.1:27018",

"arbiterOnly" : false,

"buildIndexes" : true,

"hidden" : false,

"priority" : 2,

"tags" : {

},

"slaveDelay" : 0,

"votes" : 1

},

{

"_id" : 2,

"host" : "127.0.0.1:27019",

"arbiterOnly" : false,

"buildIndexes" : true,

"hidden" : false,

"priority" : 1,

"tags" : {

},

"slaveDelay" : 0,

"votes" : 1

},

{

"_id" : 3,

"host" : "127.0.0.1:27020",

"arbiterOnly" : true,

"buildIndexes" : true,

"hidden" : false,

"priority" : 1,

"tags" : {

},

"slaveDelay" : 0,

"votes" : 1

}

],

"settings" : {

"chainingAllowed" : true,

"heartbeatTimeoutSecs" : 10,

"getLastErrorModes" : {

},

"getLastErrorDefaults" : {

"w" : 1,

"wtimeout" : 0

}

}

}


shard1:OTHER> rs.status() (查看副本集

{

"set" : "shard1",

"date" : ISODate("2017-09-14T05:23:16.893Z"),

"myState" : 1,

"members" : [

{

"_id" : 0,

"name" : "127.0.0.1:27017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 1117,

"optime" : Timestamp(1505366494, 1),

"optimeDate" : ISODate("2017-09-14T05:21:34Z"),

"electionTime" : Timestamp(1505366495, 1),

"electionDate" : ISODate("2017-09-14T05:21:35Z"),

"configVersion" : 1,

"self" : true

},

{

"_id" : 1,

"name" : "127.0.0.1:27018",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 102,

"optime" : Timestamp(1505366494, 1),

"optimeDate" : ISODate("2017-09-14T05:21:34Z"),

"lastHeartbeat" : ISODate("2017-09-14T05:23:16.113Z"),

"lastHeartbeatRecv" : ISODate("2017-09-14T05:23:16.663Z"),

"pingMs" : 0,

"configVersion" : 1

},

{

"_id" : 2,

"name" : "127.0.0.1:27019",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 102,

"optime" : Timestamp(1505366494, 1),

"optimeDate" : ISODate("2017-09-14T05:21:34Z"),

"lastHeartbeat" : ISODate("2017-09-14T05:23:16.127Z"),

"lastHeartbeatRecv" : ISODate("2017-09-14T05:23:16.668Z"),

"pingMs" : 0,

"configVersion" : 1

},

{

"_id" : 3,

"name" : "127.0.0.1:27020",

"health" : 1,

"state" : 7,

"stateStr" : "ARBITER",

"uptime" : 102,

"lastHeartbeat" : ISODate("2017-09-14T05:23:16.055Z"),

"lastHeartbeatRecv" : ISODate("2017-09-14T05:23:16.674Z"),

"pingMs" : 1,

"configVersion" : 1

}

],

"ok" : 1

}

shard1:PRIMARY>


到此處配置成功:


6.測試驗證:

參考文檔:http://blog.csdn.net/zhang_yanan/article/details/25972693 進行測試驗證

在master上創建用戶和數據庫進行測試驗證集群是否成功


db.createUser(

{

user: "root",

pwd: "[email protected]",

roles: [ { role: "root", db: "admin" } ]

}

)



use admin

db.createUser(

... {

... user: "DBA3",

... pwd: "[email protected]",

... roles: [ { role: "readWrite", db: "dbtest001" } ]

... }

... )

也可以安裝mongodb客戶端軟件來操作進行驗證集群搭建是否成功


7.部署參考文檔:

http://blog.csdn.net/luonanqin/article/details/8497860;

http://suifu.blog.51cto.com/9167728/1853478

http://blog.csdn.net/zhang_yanan/article/details/25972693

MongoDB高可用集群配置的幾種方案參考:https://yq.aliyun.com/articles/61516

本文出自 “10931853” 博客,轉載請與作者聯系!

Replica Set副本集方式的mongodb集群搭建