码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-09-14 18:49:14      阅读:171      评论:0      收藏:0      [点我收藏+]

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


1.环境:

单台服务器上开启四个mongodb实例来实现mongodb的Replica Set副本集方式的集群搭建

2.配置文件:

master主实例配置文件:

[root@localhost ~]# 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实例配置文件:

[root@localhost ~]# 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实例配置文件:

[root@localhost ~]# 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冲裁节点实例部署:

[root@localhost ~]# 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的启动脚本:

[root@localhost 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实例:

[root@localhost 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主实例来配置:

[root@localhost ~]# 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: "Zykj@558996",

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

 }

 )



use admin

db.createUser(

...  {

...  user: "DBA3",

...    pwd: "Zykj@558996",

...     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集群搭建

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

原文地址:http://wujianwei.blog.51cto.com/10931853/1965236

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!