您好,登錄后才能下訂單哦!
系統:centos6.5
IP:10.19.21.244 主節點
10.19.21.245 備節點
10.19.21.248 仲裁點
不只是主節點、副本節點、仲裁節點,還有Secondary-Only、Hidden、Delayed、Non-Voting。
Secondary-Only:不能成為primary節點,只能作為secondary副本節點,防止一些性能不高的節點成為主節點。
Hidden:這類節點是不能夠被客戶端制定IP引用,也不能被設置為主節點,但是可以投票,一般用于備份數據。
Delayed:可以指定一個時間延遲從primary節點同步數據。主要用于備份數據,如果實時同步,誤刪除數據馬上同步到從節點,恢復又恢復不了。
Non-Voting:沒有選舉權的secondary節點,純粹的備份數據節點。
三臺主機安裝mongodb
下載mongodb wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.9.tgz 解壓到目錄 tar xf mongodb-linux-x86_64-2.6.9.tgz -C /usr/local/src 更改名 mv mongodb-linux-x86_64-2.6.9 /usr/local/mongodb cd /usr/local/mongodb/ 創建數據目錄和日志目錄 mkdir -p data log 創建用戶 useradd mongodb -s /sbin/nologin 更改目錄屬主屬組 chown mongod:mongodb /usr/local/mongodb -R 更改環境變量 vim ~/.bash_profile PATH=$PATH:/usr/local/mongodb/bin source ~/.bash_profile
配置文件 vim /etc/mongodb.conf port = 27017 fork = true logpath = /usr/local/mongodb/log/mongodb.log dbpath =/usr/local/mongodb/data/ journal = true #以目錄的形式存數據,如果已經存在,修改此配置會導致原始數據消失(注釋次參數會回來),除非遷移現有數據文件到directoryperdb所產生的數據目錄中。 directoryperdb=true #日志增加形式寫入 logappend=true #配置副本集,指定副本名稱作為參數,所有主機都必須有相同名稱作為同一個副本集 replSet=testrs #使用預分配方式來保證寫入性能的穩定,預分配在后臺進行,并且每個預分配的文件都用0進行填充。這會讓MongoDB始終保持額外的空間和空余的數據文件,從而避免了數據增長過快而帶來的分配磁盤空間引起的阻塞。設置noprealloc= true來禁用預分配的數據文件,會縮短啟動時間,但在正常操作過程中,可能會導致性能顯著下降。 noprealloc=true
啟動服務 mongod -f /etc/mongodb.conf
首先連接主節點 mongo > use admin > conf = {_id:"testrs",members:[{_id:0,host:"10.19.21.244:27017",priority:2},{_id:1,host:"10.19.21.245:27017",priority:1},{_id:2,host:"10.19.21.248:27017",arbiterOnly:true}]}; { "_id" : "testrs", "version" : 1, "members" : [ { "_id" : 0, "host" : "10.19.21.244:27017", "priority" : 2 }, { "_id" : 1, "host" : "10.19.21.245:27017" }, { "_id" : 2, "host" : "10.19.21.248:27017", "arbiterOnly" : true } ] } 注意最初的_id的值就是我們配置文件中設定的replSet的值,arbiterOnly是大寫的O,配置完成后進行初始化: > rs.initiate(conf) { "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } 此步驟必須備節點和仲裁節點的mongo啟動,不然無法執行此步驟。
> rs.status() { "set" : "testrs", "date" : ISODate("2015-05-07T11:55:17Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "10.19.21.244:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 5737, "optime" : Timestamp(1430995665, 1), "optimeDate" : ISODate("2015-05-07T10:47:45Z"), "electionTime" : Timestamp(1430993992, 1), "electionDate" : ISODate("2015-05-07T10:19:52Z"), "self" : true }, { "_id" : 1, "name" : "10.19.21.245:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 3077, "optime" : Timestamp(1430995665, 1), "optimeDate" : ISODate("2015-05-07T10:47:45Z"), "lastHeartbeat" : ISODate("2015-05-07T11:55:17Z"), "lastHeartbeatRecv" : ISODate("2015-05-07T11:55:17Z"), "pingMs" : 0, "syncingTo" : "10.19.21.244:27017" }, { "_id" : 2, "name" : "10.19.21.248:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 5735, "lastHeartbeat" : ISODate("2015-05-07T11:55:17Z"), "lastHeartbeatRecv" : ISODate("2015-05-07T11:55:17Z"), "pingMs" : 0 } ], "ok" : 1 }
> use test 插入數據 > db.testdb.insert({"tset1":"i'm master"})
然后到備份節點查看數據是否有 mongo 10.19.21.245 testrs:SECONDARY> use test switched to db test testrs:SECONDARY> show tables; 2015-05-07T19:57:29.670+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131 需要進行配置 testrs:SECONDARY> db.setSlaveOk() testrs:SECONDARY> show tables; system.indexes testdb testrs:SECONDARY> db.testdb.find() { "_id" : ObjectId("554a4ee4eb804a0f1b6ec347"), "test1" : "i'm master" }
停止主節點,查看整個集群狀態 testrs:PRIMARY> rs.status() { "set" : "testrs", "date" : ISODate("2015-05-07T11:59:42Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "10.19.21.244:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(1430995665, 1), "optimeDate" : ISODate("2015-05-07T10:47:45Z"), "lastHeartbeat" : ISODate("2015-05-07T11:59:40Z"), "lastHeartbeatRecv" : ISODate("2015-05-07T11:59:32Z"), "pingMs" : 0 }, { "_id" : 1, "name" : "10.19.21.245:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 3342, "optime" : Timestamp(1430995665, 1), "optimeDate" : ISODate("2015-05-07T10:47:45Z"), "electionTime" : Timestamp(1430999976, 1), "electionDate" : ISODate("2015-05-07T11:59:36Z"), "self" : true }, { "_id" : 2, "name" : "10.19.21.248:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 3340, "lastHeartbeat" : ISODate("2015-05-07T11:59:42Z"), "lastHeartbeatRecv" : ISODate("2015-05-07T11:59:41Z"), "pingMs" : 0 } ], "ok" : 1 }
主節點已切換過來,此時備節點已經變成了主節點。再插入一條數據 testrs:PRIMARY> db.testdb.insert({"test2":"i'm new master"}) testrs:PRIMARY> db.testdb.find(); { "_id" : ObjectId("554a4ee4eb804a0f1b6ec347"), "test1" : "i'm master" } { "_id" : ObjectId("554a505d494f70c600422b41"), "test2" : "i'm new master" }
重啟原來的主節點查看狀態 testrs:SECONDARY> rs.status() { "set" : "testrs", "date" : ISODate("2015-05-07T14:00:33Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "10.19.21.244:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 15, "optime" : Timestamp(1430995665, 1), "optimeDate" : ISODate("2015-05-07T10:47:45Z"), "electionTime" : Timestamp(1431007222, 1), "electionDate" : ISODate("2015-05-07T14:00:22Z"), "self" : true }, { "_id" : 1, "name" : "10.19.21.245:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 13, "optime" : Timestamp(1430995665, 1), "optimeDate" : ISODate("2015-05-07T10:47:45Z"), "lastHeartbeat" : ISODate("2015-05-07T14:00:32Z"), "lastHeartbeatRecv" : ISODate("2015-05-07T14:00:33Z"), "pingMs" : 0, "lastHeartbeatMessage" : "syncing to: 10.19.21.244:27017", "syncingTo" : "10.19.21.244:27017" }, { "_id" : 2, "name" : "10.19.21.248:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 13, "lastHeartbeat" : ISODate("2015-05-07T14:00:32Z"), "lastHeartbeatRecv" : ISODate("2015-05-07T14:00:33Z"), "pingMs" : 0 } ], "ok" : 1 }
查看數據是否同步過來 testrs:PRIMARY> use test switched to db test testrs:PRIMARY> db.testdb.find() { "_id" : ObjectId("554a4ee4eb804a0f1b6ec347"), "test1" : "i'm master" } { "_id" : ObjectId("554a505d494f70c600422b41"), "test2" : "i'm new master" }
向集群中增加節點 cfg = rs.conf() cfg.members[3] = {_id:3,host:"10.19.21.246:27017",priority:0.5} rs.reconfig(cfg) 如果備份節點無法讀取數據,需要設置db.setSlaveOK()。如果備份節點可讀但不可寫,修改優先級 cfg = rs.conf() cfg.members[0].priority = 0.5 cfg.members[1].priority = 2 cfg.members[2].priority = 2 rs.reconfig(cfg) 需要 reconfig后配置才能生效。
php與mongodb連接 <?php $options = array( 'replicaSet' => 'testrs', 'readPreference' => 'secondaryPreferred', ); $m = new MongoClient("mongodb://10.19.21.244:27017,10.19.21.245:27017/", $options); foreach ( $m->getConnections() as $c ) { echo $c['hash'], ":\n", " - {$c['connection']['connection_type_desc']}, ", "{$c['connection']['ping_ms']} ms\n"; } ?>
primary:默認參數,只從主節點上進行讀取操作;
primaryPreferred:大部分從主節點上讀取數據,只有主節點不可用時從secondary節點讀取數據。
secondary:只從secondary節點上進行讀取操作,存在的問題是secondary節點的數據會比primary節點數據“舊”。
secondaryPreferred:優先從secondary節點進行讀取操作,secondary節點不可用時從主節點讀取數據;
nearest:不管是主節點、secondary節點,從網絡延遲最低的節點上讀取數據。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。