MongoDB複本集搭建

來源:互聯網
上載者:User

標籤:

一、環境
$ cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) $ uname -aLinux zhaopin-2-201 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux$ mongo --versionMongoDB shell version: 3.0.6
二、準備 1.建立目錄
$ sudo mkdir -p /data/mongodb/{data/db0,backup/db0,log/db0,conf/db0}
2.編寫設定檔
$ sudo vim /data/mongodb/conf/db0/mongodb.conf# baseport = 27017maxConns = 800 filePermissions = 0700fork = truenoauth = truedbpath = /data/mongodb/data/db0pidfilepath = /data/mongodb/data/db0/mongodb.pidjournal = true# securitynohttpinterface = truerest = false# log logpath = /data/mongodb/log/db0/mongodb.loglogRotate = renamelogappend = trueslowms = 50replSet = rs0
三、配置複本集 1.配置primary 1)啟動
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.conf  about to fork child process, waiting until server is ready for connections.forked process: 48583child process started successfully, parent exiting$ mongoMongoDB shell version: 3.0.6connecting to: test> rs.status();{        "info" : "run rs.initiate(...) if not yet done for the set",        "ok" : 0,        "errmsg" : "no replset config has been received",        "code" : 94}
2)初始化
> cfg={_id:"rs0", members:[ {_id:0,host:"172.30.2.201:27017"}] }{        "_id" : "rs0",        "members" : [                {                        "_id" : 0,                        "host" : "172.30.2.201:27017"                }        ]}> rs.initiate( cfg );{ "ok" : 1 }rs0:OTHER> rs.status();{        "set" : "rs0",        "date" : ISODate("2015-09-25T08:31:36.354Z"),        "myState" : 1,        "members" : [                {                        "_id" : 0,                        "name" : "172.30.2.201:27017",                        "health" : 1,                        "state" : 1,                        "stateStr" : "PRIMARY",                        "uptime" : 112,                        "optime" : Timestamp(1443169891, 1),                        "optimeDate" : ISODate("2015-09-25T08:31:31Z"),                        "electionTime" : Timestamp(1443169891, 2),                        "electionDate" : ISODate("2015-09-25T08:31:31Z"),                        "configVersion" : 1,                        "self" : true                }        ],        "ok" : 1}
2.添加節點 1)啟動新的節點
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until server is ready for connections.forked process: 41794child process started successfully, parent exiting$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until server is ready for connections.forked process: 3761child process started successfully, parent exiting
2)添加新的節點

在primary上執行:

rs0:PRIMARY> rs.add( "172.30.2.202:27017" );{ "ok" : 1 }rs0:PRIMARY> rs.status();{        "set" : "rs0",        "date" : ISODate("2015-09-25T08:34:48.161Z"),        "myState" : 1,        "members" : [                {                        "_id" : 0,                        "name" : "172.30.2.201:27017",                        "health" : 1,                        "state" : 1,                        "stateStr" : "PRIMARY",                        "uptime" : 304,                        "optime" : Timestamp(1443170060, 1),                        "optimeDate" : ISODate("2015-09-25T08:34:20Z"),                        "electionTime" : Timestamp(1443169891, 2),                        "electionDate" : ISODate("2015-09-25T08:31:31Z"),                        "configVersion" : 2,                        "self" : true                },                {                        "_id" : 1,                        "name" : "172.30.2.202:27017",                        "health" : 1,                        "state" : 2,                        "stateStr" : "SECONDARY",                        "uptime" : 27,                        "optime" : Timestamp(1443170060, 1),                        "optimeDate" : ISODate("2015-09-25T08:34:20Z"),                        "lastHeartbeat" : ISODate("2015-09-25T08:34:46.884Z"),                        "lastHeartbeatRecv" : ISODate("2015-09-25T08:34:46.896Z"),                        "pingMs" : 0,                        "configVersion" : 2                }        ],        "ok" : 1}rs0:PRIMARY> rs.add( "172.30.2.203:27017" );{ "ok" : 1 }rs0:PRIMARY> rs.status();{        "set" : "rs0",        "date" : ISODate("2015-09-25T08:36:22.579Z"),        "myState" : 1,        "members" : [                {                        "_id" : 0,                        "name" : "172.30.2.201:27017",                        "health" : 1,                        "state" : 1,                        "stateStr" : "PRIMARY",                        "uptime" : 398,                        "optime" : Timestamp(1443170158, 1),                        "optimeDate" : ISODate("2015-09-25T08:35:58Z"),                        "electionTime" : Timestamp(1443169891, 2),                        "electionDate" : ISODate("2015-09-25T08:31:31Z"),                        "configVersion" : 3,                        "self" : true                },                {                        "_id" : 1,                        "name" : "172.30.2.202:27017",                        "health" : 1,                        "state" : 2,                        "stateStr" : "SECONDARY",                        "uptime" : 121,                        "optime" : Timestamp(1443170158, 1),                        "optimeDate" : ISODate("2015-09-25T08:35:58Z"),                        "lastHeartbeat" : ISODate("2015-09-25T08:36:22.268Z"),                        "lastHeartbeatRecv" : ISODate("2015-09-25T08:36:20.949Z"),                        "pingMs" : 0,                        "syncingTo" : "172.30.2.201:27017",                        "configVersion" : 3                },                {                        "_id" : 2,                        "name" : "172.30.2.203:27017",                        "health" : 1,                        "state" : 2,                        "stateStr" : "SECONDARY",                        "uptime" : 24,                        "optime" : Timestamp(1443170158, 1),                        "optimeDate" : ISODate("2015-09-25T08:35:58Z"),                        "lastHeartbeat" : ISODate("2015-09-25T08:36:22.267Z"),                        "lastHeartbeatRecv" : ISODate("2015-09-25T08:36:22.275Z"),                        "pingMs" : 0,                        "configVersion" : 3                }        ],        "ok" : 1}
四、驗證 1.資料同步 1) primary:
$ mongoMongoDB shell version: 3.0.6connecting to: testrs0:PRIMARY> use aaa;switched to db aaars0:PRIMARY> db.createCollection( "test" );{ "ok" : 1 }rs0:PRIMARY> show collections;system.indexestest


2) secondary:
$ mongoMongoDB shell version: 3.0.6connecting to: testrs0:SECONDARY> rs.slaveOk();rs0:SECONDARY> use aaa;switched to db aaars0:SECONDARY> show collections;system.indexestest$ mongoMongoDB shell version: 3.0.6connecting to: testrs0:SECONDARY> rs.slaveOk();rs0:SECONDARY> use aaa;switched to db aaars0:SECONDARY> show collections;system.indexestest
2.讀寫驗證

由上可知,primary是可讀寫的

在secondary上寫測試:

$ mongoMongoDB shell version: 3.0.6connecting to: test> use aaa;switched to db aaa> db.createCollection("test2");{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }> show collections;2015-09-23T15:57:49.240+0800 E QUERY    Error: listCollections failed: { "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }    at Error (<anonymous>)    at DB._getCollectionInfosCommand (src/mongo/shell/db.js:646:15)    at DB.getCollectionInfos (src/mongo/shell/db.js:658:20)    at DB.getCollectionNames (src/mongo/shell/db.js:669:17)    at shellHelper.show (src/mongo/shell/utils.js:625:12)    at shellHelper (src/mongo/shell/utils.js:524:36)    at (shellhelp2):1:1 at src/mongo/shell/db.js:646> rs.slaveOk();> show collections;system.indexestest>

證明secondary節點是不能寫,預設也是不可讀的,需要執行rs.slaveOk();或者db.getMongo().setSlaveOk();並且只對當前session有效,所以每次串連從庫都需要執行。


五、說明

mongodb的複本集至少需要3台以上才能實現高可用,並且節點的個數最好是基數。


MongoDB複本集搭建

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.