MongoDB複製集之:安裝部署

來源:互聯網
上載者:User

MongoDB複製集之:安裝部署

準備工作
軟體環境

OS:Oracle 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. 驗證複製集同步
分別登入兩從節點查看登入狀態,分別為secondary和arbite

下面登入從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相關教程見以下內容:

CentOS 編譯安裝 MongoDB與mongoDB的php擴充

CentOS 6 使用 yum 安裝MongoDB及伺服器端配置

Ubuntu 13.04下安裝MongoDB2.4.3

MongoDB入門必讀(概念與實戰並重)

Ubunu 14.04下MongoDB的安裝指南

《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

Nagios監控MongoDB分區叢集服務實戰

基於CentOS 6.5作業系統搭建MongoDB服務

MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.