您好,登錄后才能下訂單哦!
本文分兩部分介紹mongodb3.2.1分片部署配置及故障模擬驗證。
第一部分 安裝配置
一、實驗環境
兩組副本集做分片
版本3.2.1
副本集1:192.168.115.11:27017,192.168.115.12:27017,192.168.115.11:47017(arbiter)
副本集2:192.168.115.11:37017,192.168.115.12:37017,192.168.115.12:47017(arbiter)
configserver:192.168.115.11:10000,192.168.115.11:10001,192.168.115.12:10000
mongos:192.168.115.11:20000
二、分片介紹
1.邏輯圖
片(shard):每一個分片一個副本集
配置服務器(config server):存儲集群的配置信息,3.2以上版本支持副本集模式部署
路由進程(mongos):路由所有請求,然后將結果聚合。它不保存存儲數據或配置信息,配置信息從配置服務器上加載到內存中。
副本集方式部署confiserver
一、部署條件
1.集群中不能有仲裁節點
2.集群中不能有延遲節點
3.每個成員必須可以創建索引
二、configserver安裝配置
1.修改配置文件(其他兩個節點配置文件類似,主要修改監聽端口,及數據路徑,如果一臺機器上運行多個實例,注意配置文件名稱要不一樣)
cat config.conf fork = true quiet = true port = 10000 dbpath = /data/config logpath = /usr/local/mongodb/logs/config.log logappend = true directoryperdb = true configsvr = true replSet = hnrconfig/192.168.115.11:10000,192.168.115.11:10001,192.168.115.12:10000
2.服務啟動和停止
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/config.conf
/usr/local/mongodb/bin/mongod --shutdown --port 10000 --dbpath=/data/config
3.配置副本集
連接任意一個節點進行配置
> show dbs
2016-11-17T09:06:08.088+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=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
出現以上錯誤,需要執行
> rs.slaveOk()
> use admin
> db.runCommand({"replSetInitiate" : { "_id" : "hnrconfig" ,"members" : [ { "_id" : 1, "host" : "192.168.115.11:10000"},{ "_id" : 2, "host" : "192.168.115.12:10000"},{"_id" : 3, "host" : "192.168.115.11:10001"}]}})
{ "ok" : 1 }
三、mongos配置
1.配置文件
cat mongos.conf fork = true quiet = true port = 20000 logpath = /usr/local/mongodb/logs/mongos.log logappend = true configdb = 192.168.115.11:10000,192.168.115.11:10001,192.168.115.12:10000
2.啟動mongos服務
/usr/local/mongodb/bin/mongos -f /usr/local/mongodb/etc/mongos.conf
四、往集群中添加分片
連接mongos
mongos> sh.addShard("hnrtest1/192.168.115.11:27017")
{ "shardAdded" : "hnrtest1", "ok" : 1 }
mongos>
mongos> sh.addShard("hnrtest2/192.168.115.12:37017")
{ "shardAdded" : "hnrtest2", "ok" : 1 }
mongos>
五、開啟分片
1.先對數據庫啟用分片功能
mongos> sh.enableSharding("shardtest")
{ "ok" : 1 }
mongos>
2.對集合開啟分片(自動分片建)
mongos> sh.shardCollection("shardtest.student",{"cre_id":1})
{ "collectionsharded" : "shardtest.student", "ok" : 1 }
mongos>
3.修改默認chunk大小(默認為64M),自動分片測試效果不好,需要插入大量數據,將其修改為1M
mongos> use config
mongos> db.settings.save({ "_id" : "chunksize", "value" : NumberLong(1) })
修改后對student2集合進行分片
mongos> sh.shardCollection("shardtest.student2",{"cre_id":1})
插入5萬條數據
直接在后端分片副本集上查詢
hnrtest2:PRIMARY> db.student2.find().count()
27081
hnrtest2:PRIMARY>
hnrtest1:PRIMARY> db.student2.find().count()
22918
hnrtest1:PRIMARY>
4.采用哈希分片
修改chunk為默認值64M
mongos> db.settings.save({ "_id" : "chunksize", "value" : NumberLong(64) })
student3集合在cre_id字段使用哈希分片
mongos> sh.shardCollection("shardtest.student3",{"cre_id":"hashed"})
{ "collectionsharded" : "shardtest.student3", "ok" : 1 }
mongos> sh.status()
shardtest.student3
shard key: { "cre_id" : "hashed" }
unique: false
balancing: true
chunks:
hnrtest1 2
hnrtest2 2
{ "cre_id" : { "$minKey" : 1 } } -->> { "cre_id" : NumberLong("-4611686018427387902") } on : hnrtest1 Timestamp(2, 2)
{ "cre_id" : NumberLong("-4611686018427387902") } -->> { "cre_id" : NumberLong(0) } on : hnrtest1 Timestamp(2, 3)
{ "cre_id" : NumberLong(0) } -->> { "cre_id" : NumberLong("4611686018427387902") } on : hnrtest2 Timestamp(2, 4)
{ "cre_id" : NumberLong("4611686018427387902") } -->> { "cre_id" : { "$maxKey" : 1 } } on : hnrtest2 Timestamp(2, 5)
往student3插入1萬條數據,在每個分片上查詢
hnrtest1:PRIMARY> db.student3.find().count()
4952
hnrtest1:PRIMARY>
hnrtest2:PRIMARY> db.student3.find().count()
5047
hnrtest2:PRIMARY>
第二部分 故障模擬驗證
一、模擬config服務副本集primary節點宕機
1.關閉服務
/usr/local/mongodb/bin/mongod --shutdown --port 10000 --dbpath=/data/config
2.副本集重新選舉一個primary節點
3.讀取數據,所有數據均正常返回
mongos> use shardtest
switched to db shardtest
mongos>
mongos> db.student.find().count()
99999
mongos> db.student2.find().count()
49999
mongos> db.student3.find().count()
9999
mongos>
4.對新的集合進行分片,插入5千條數據
mongos> sh.shardCollection("shardtest.student4",{"cre_id":"hashed"})
{ "collectionsharded" : "shardtest.student4", "ok" : 1 }
mongos>
在每個分片上查詢數據
hnrtest2:PRIMARY> db.student4.find().count()
2525
hnrtest2:PRIMARY>
hnrtest1:PRIMARY> db.student4.find().count()
2474
hnrtest1:PRIMARY>
二、config服務數據備份恢復
1.數據備份
/usr/local/mongodb/bin/mongodump -h 192.168.115.11:10001 -o configdata
2.關閉所有config服務節點
/usr/local/mongodb/bin/mongod --shutdown --port 10000 --dbpath=/data/config
/usr/local/mongodb/bin/mongod --shutdown --port 10001 --dbpath=/data/config1
3.數據讀取操作
由于mongos是將config的配置信息全部加載到內存中運行,因此此時通過mongos查詢數據一切正常,但是不能對新的集合進行分片操作
mongos> db.student.find().count()
99999
mongos> db.student2.find().count()
49999
mongos> db.student3.find().count()
9999
mongos> db.student4.find().count()
4999
mongos>
4.對集合進行分片操作,無法完成
mongos> sh.shardCollection("shardtest.student5",{"cre_id":"hashed"})
{
"ok" : 0,
"errmsg" : "None of the hosts for replica set hnrconfig could be contacted.",
"code" : 71
}
mongos>
5.關閉mongos服務,刪除config節點所有數據
6.重新啟動三個config服務
7.重新初始化副本集
> rs.slaveOk()
> use admin
> db.runCommand({"replSetInitiate" : { "_id" : "hnrconfig" ,"members" : [ { "_id" : 1, "host" : "192.168.115.11:10000"},{ "_id" : 2, "host" : "192.168.115.12:10000"},{"_id" : 3, "host" : "192.168.115.11:10001"}]}})
8.啟動mongos服務,此時沒有任何數據
9.導入備份的config數據
/usr/local/mongodb/bin/mongorestore -h 192.168.115.11:10000 -d config configdata/config/
在mongos查詢,但是查詢數據會出現超時,數據無法查詢
10.在mongos執行如下命令
mongos> sh.enableSharding("shardtest")
{ "ok" : 0, "errmsg" : "Operation timed out", "code" : 50 }
mongos日志
2016-11-17T14:46:21.197+0800 I SHARDING [Balancer] about to log metadata event into actionlog: { _id: "node1.hnr.com-2016-11-17T14:46:21.197+0800-582d523ded1c4b679a84877b", server: "node1.hnr.com", clientAddr: "", time: new Date(1479365181197), what: "balancer.round", ns: "", details: { executionTimeMillis: 30007, errorOccured: true, errmsg: "could not get updated shard list from config server due to ExceededTimeLimit Operation timed out" } }
官網上說是bug,恢復失敗
https://jira.mongodb.org/browse/SERVER-22392
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。