MongoDB复制集之:安装部署



准备工作

软件环境

OSOracle Linux 6.5

MongoDB:mongodb3.2.3

 

部署架构

模拟有三台服务器

10.1.5.123:28001 主节点

10.1.5.123:28002 从节点

10.1.5.123:28003 选举节点

 

复制集架构:一主,一从,一选举节点

 

下载安装

下载地址:https://www.mongodb.org/downloads

mongodb-linux-x86_64-rhel62-3.2.3.tgz

 

部署步骤

1.创建目录及用户

[root@devtest mongodb]# mkdir conf data log

[root@devtest data]# mkdir 28001 28002 28003

--创建mongo用户

[root@devtest conf]# groupadd mongod

[root@devtest conf]# useradd -g mongod mongod

 

2.创建配置文件

--配置三个实例的配置文件(修改相应的端口号即可)

--节点1

$ vi /home/mongod/mongodb3.2.3/conf/28001.conf

port=28001

bind_ip=10.1.5.123

logpath=/home/mongod/mongodb3.2.3/log/28001.log

dbpath=/home/mongod/mongodb3.2.3/data/28001/

logappend=true

pidfilepath=/home/mongod/mongodb3.2.3/data/28001/28001.pid

fork=true

oplogSize=1024

replSet=MyMongo

 

--节点2

$ vi /home/mongod/mongodb3.2.3/conf/28002.conf

port=28002

bind_ip=10.1.5.123

logpath=/home/mongod/mongodb3.2.3/log/28002.log

dbpath=/home/mongod/mongodb3.2.3/data/28002/

logappend=true

pidfilepath=/home/mongod/mongodb3.2.3/data/28002/28002.pid

fork=true

oplogSize=1024

replSet=MyMongo

 

--节点3

$ vi /home/mongod/mongodb3.2.3/conf/28003.conf

port=28003

bind_ip=10.1.5.123

logpath=/home/mongod/mongodb3.2.3/log/28003.log

dbpath=/home/mongod/mongodb3.2.3/data/28003/

logappend=true

pidfilepath=/home/mongod/mongodb3.2.3/data/28003/28003.pid

fork=true

oplogSize=1024

replSet=MyMongo

 

3.启动mongo复制集

$ mongod -f /home/mongod/mongodb3.2.3/conf/28001.conf

$ mongod -f /home/mongod/mongodb3.2.3/conf/28002.conf

$ mongod -f /home/mongod/mongodb3.2.3/conf/28003.conf

 

4.初始化复制集

[mongod@devtest ~]$ mongo 10.1.5.123:28001/admin

MongoDB shell version: 3.2.3

connecting to: 10.1.5.123:28001/admin

Welcome to the MongoDB shell.

For interactive help, type"help".

For more comprehensive documentation, see

        http://docs.mongodb.org/

Questions? Try the support group

       http://groups.google.com/group/mongodb-user

> db

admin

> config = {

... "_id":"MyMongo",

... members:[

...{"_id":0,host:"10.1.5.123:28001"},

...{"_id":1,host:"10.1.5.123:28002"},

... {"_id":2,host:"10.1.5.123:28003"}]

... }

{

       "id" : "MyMongo",

       "members" : [

                {

                        "_id" : 0,

                        "host" :"10.1.5.123:28001"

                },

                {

                        "_id" : 1,

                        "host" :"10.1.5.123:28002"

                },

                {

                        "_id" : 2,

                        "host" :"10.1.5.123:28003"

                }

       ]

}

 

--查看复制集成员

> config.members

[

       {

                "_id" : 0,

                "host" :"10.1.5.123:28001"

       },

       {

                "_id" : 1,

                "host" :"10.1.5.123:28002"

       },

       {

                "_id" : 2,

                "host" :"10.1.5.123:28003"

       }

]

 

--把节点3修改为arbiter选举节点

> config.members[2]

{ "_id" : 2, "host" :"10.1.5.123:28003" }

> config.members[2] ={"_id":2,"host":"10.1.5.123:28003",arbiterOnly:true}

{ "id" : 0, "host" :"10.1.5.123:28003", "arbiterOnly" : true }

 

--进行复制集初始化

> rs.initiate(config)

{ "ok" : 1 }

 

--查看复制集状态

MyMongo:OTHER> rs.status()

{

        "set" : "MyMongo",

        "date" :ISODate("2016-03-15T09:42:47.643Z"),

        "myState" : 1,

        "term" : NumberLong(1),

        "heartbeatIntervalMillis" :NumberLong(2000),

        "members" : [

                {

                        "_id" : 0,

                        "name" :"10.1.5.123:28001",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" :"PRIMARY",

                        "uptime" :1812,

                        "optime" : {

                                "ts": Timestamp(1458034899, 1),

                                "t" :NumberLong(1)

                        },

                        "optimeDate": ISODate("2016-03-15T09:41:39Z"),

                        "infoMessage": "could not find member to sync from",

                       "electionTime" : Timestamp(1458034898, 1),

                        "electionDate": ISODate("2016-03-15T09:41:38Z"),

                       "configVersion" : 1,

                        "self" : true

                },

                {

                        "_id" : 1,

                        "name" :"10.1.5.123:28002",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" :"SECONDARY",

                        "uptime" :80,

                        "optime" : {

                                "ts": Timestamp(1458034899, 1),

                                "t" :NumberLong(1)

                        },

                        "optimeDate": ISODate("2016-03-15T09:41:39Z"),

                       "lastHeartbeat" :ISODate("2016-03-15T09:42:46.947Z"),

                        "lastHeartbeatRecv" :ISODate("2016-03-15T09:42:46.445Z"),

                        "pingMs" :NumberLong(0),

                        "syncingTo" :"10.1.5.123:28001",

                       "configVersion" : 1

                },

                {

                        "_id" : 2,

                        "name" :"10.1.5.123:28003",

                        "health" : 1,

                        "state" : 7,

                        "stateStr" :"ARBITER",

                        "uptime" :80,

                       "lastHeartbeat" :ISODate("2016-03-15T09:42:46.946Z"),

                       "lastHeartbeatRecv" :ISODate("2016-03-15T09:42:44.428Z"),

                        "pingMs" :NumberLong(0),

                        "configVersion": 1

                }

        ],

        "ok" : 1

}

MyMongo:PRIMARY>

 

5. 验证复制集同步

分别登录两从节点查看登录状态,分别为secondaryarbite

下面登录从secondary节点进行测试:

在从节点进行查询时报下面的错误,因为在默认情况下,不通过驱动连接mongodb从节点数据库时,如果不开始slaveOK=true,是无法读取从节点数据的。

[mongod@devtest conf]$ mongo10.1.5.123:28002/admin

MongoDB shell version: 3.2.3

connecting to:10.1.5.123:28002/admin

MyMongo:SECONDARY> show dbs

2016-03-15T17:49:47.854+0800 EQUERY    [thread1] Error: listDatabasesfailed:{ "ok" : 0, "errmsg" : "not master andslaveOk=false", "code" : 13435 } :

_getErrorWithCode@src/mongo/shell/utils.js:23:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:53:1

shellHelper.show@src/mongo/shell/utils.js:700:19

shellHelper@src/mongo/shell/utils.js:594:15

@(shellhelp2):1:1

MyMongo:SECONDARY>rs.slaveOk(true)

MyMongo:SECONDARY> show dbs

local  0.000GB

test   0.000GB

MyMongo:SECONDARY> use test

switched to db test

MyMongo:SECONDARY> showtables

t1

MyMongo:SECONDARY>db.t1.find()

{ "_id" :ObjectId("56e7dc310ebd75f9ef3546c1"), "name" :"licz" }

 

 

下面登录从arbite节点进行测试:

然后登录arbite节点,会发现数据没有同步,因为arbite不参与数据库的同步,即不存储数据

local数据库大小已经改变了,因为local库存储命名空间(local.ns文件)的内容。

 

MyMongo:ARBITER> rs.slaveOk(true)

MyMongo:ARBITER> show dbs

local 0.000GB

 

 

6.模拟主-从故障切换

--杀掉当前主节点为28001端口

[mongod@devtest ~]$ ps -ef|grep mongo

root     7186  7150  0 09:15 pts/2    00:00:00 su - mongod

mongod   7187  7186  0 09:15 pts/2    00:00:00 -bash

mongod   7306  7187  0 09:16 pts/2    00:00:00 ps -ef

mongod   7307  7187  0 09:16 pts/2    00:00:00 grep mongo

mongod  16253     1  2 Mar15 ?        00:22:00 mongod -f/home/mongod/mongodb3.2.3/conf/28001.conf

mongod  16299     1  2 Mar15 ?        00:21:50 mongod -f/home/mongod/mongodb3.2.3/conf/28002.conf

mongod  16345     1  2 Mar15 ?        00:20:05 mongod -f/home/mongod/mongodb3.2.3/conf/28003.conf

[mongod@devtest ~]$ kill -9 16253

 

--发现主节点已经切换至28002端口

[mongod@devtest ~]$ mongo 10.1.5.123:28002/admin

MongoDB shell version: 3.2.3

connecting to: 10.1.5.123:28002/admin

MyMongo:PRIMARY>

 

--再次启动28001端口并登录,发现28001已为从节点

[mongod@devtest ~]$ mongod -f /home/mongod/mongodb3.2.3/conf/28001.conf

about to fork child process, waiting untilserver is ready for connections.

forked process: 21410

child process started successfully, parentexiting

[mongod@devtest ~]$ mongo 10.1.5.123:28001/admin                      

MongoDB shell version: 3.2.3

connecting to: 10.1.5.123:28001/admin

MyMongo:SECONDARY>

你可能感兴趣的:(MongoDB复制集之:安装部署)