本篇文章為大家展示了MongoDB中怎么搭建shard_replica集群,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
目前創(chuàng)新互聯(lián)建站已為超過千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、犍為網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
參考官方文檔,大致步驟
第一步:創(chuàng)建Config Server實(shí)例,配置Config Server實(shí)例成Replica Set
第二步:創(chuàng)建Shard Server實(shí)例,配置Shard Server實(shí)例成Replica Set
第三步:創(chuàng)建Route Server實(shí)例,把Config Server實(shí)例和Shard Server實(shí)例配置進(jìn)來
第四步:創(chuàng)建數(shù)據(jù)庫并執(zhí)行db.printShardingStatus()命令驗(yàn)證
涉及replSet、shardsvr、configsvr幾個(gè)參數(shù)
replSet:配置replica set,replica set中的所有主機(jī)必須具有相同的replica set名稱。
shardsvr:將此mongod實(shí)例配置為分片集群中的分片。默認(rèn)端口是27018。
configsvr:聲明此mongod實(shí)例作為分片集群的配置服務(wù)器。默認(rèn)端口是27019,默認(rèn)的dbpath目錄是/data/configdb,配置服務(wù)器存儲(chǔ)集群的元數(shù)據(jù)和配置設(shè)置。從MongoDB 3.4開始,配置服務(wù)器必須部署為一個(gè)replicate set副本集,為了防止這個(gè)配置服務(wù)是單點(diǎn)服務(wù)。如果你的集群只有一個(gè)配置服務(wù)器,一旦這個(gè)配置服務(wù)器出現(xiàn)故障則集群將不可用
涉及Config Server、Shard Server、Route Server幾個(gè)實(shí)例
1. Config Server: mongod實(shí)例,存儲(chǔ)了整個(gè)Cluster Metadata,其中包括Chunk信息。
2. Shard Server: mongod實(shí)例,用于存儲(chǔ)實(shí)際的數(shù)據(jù)塊,實(shí)際生產(chǎn)環(huán)境中一個(gè)Shard Server角色可由幾臺(tái)機(jī)器組個(gè)一個(gè)Replica Set承擔(dān),防止主機(jī)單點(diǎn)故障。
3. Route Server: mongos實(shí)例,前端路由,客戶端由此接入,且讓整個(gè)集群看上去像單一數(shù)據(jù)庫,前端應(yīng)用可以透明使用。
部署規(guī)劃
2臺(tái)物理設(shè)備服務(wù)器,ip分別為172.22.138.157、172.22.138.158
兩臺(tái)服務(wù)器上分別創(chuàng)建端口是29001的Config Server實(shí)例,這兩臺(tái)服務(wù)器上的Config Server實(shí)例組成Replica Set模式
兩臺(tái)服務(wù)器上分別創(chuàng)建端口分別是28001、28002、28003的shard server實(shí)例,兩臺(tái)服務(wù)器上相同端口的shard server實(shí)例組成Replica Set模式
兩臺(tái)服務(wù)器上分別創(chuàng)建端口是27001的Route Server實(shí)例
部署架構(gòu)
外部程序-->Route Server實(shí)例-->Config Server實(shí)例-->shard server實(shí)例
Route Server實(shí)例不是單點(diǎn),兩臺(tái)服務(wù)器上都有,配置都是一樣,都是指向一樣的Config Server實(shí)例
Config Server實(shí)例不是單點(diǎn),是Replica Set模式
shard server實(shí)例不是單點(diǎn),是Replica Set模式
第一步:
創(chuàng)建Config Server實(shí)例,配置Config Server實(shí)例成Replica Set
1.1、在兩臺(tái)服務(wù)器上分別編輯如下文件(兩個(gè)重點(diǎn)參數(shù)configsvr、replSet)
cat /mongodb/configsvr29001.conf
port=29001
dbpath=/mongodb/mongoconfig29001
logpath=/mongodb/log/mongoconfig29001.log
configsvr=true
replSet=config29001
fork=true
logappend=true
directoryperdb=true
bind_ip=0.0.0.0
pidfilepath=/mongodb/mongoconfig29001.pid
1.2、兩臺(tái)服務(wù)器上分別啟動(dòng)Config Server實(shí)例
mongod -f /mongodb/configsvr29001.conf
1.3、配置兩臺(tái)服務(wù)器的Config Server實(shí)例成Replica Set
登錄任意一臺(tái)服務(wù)器執(zhí)行如下操作即可
mongo --host 172.22.138.157 --port 29001
rs.initiate(
{
_id: "config29001",
configsvr: true,
members: [
{ _id : 0, host : "172.22.138.157:29001" },
{ _id : 1, host : "172.22.138.158:29001" }
]
}
)
第二步
創(chuàng)建Shard Server實(shí)例,配置Shard Server實(shí)例成Replica Set
2.1、在兩臺(tái)服務(wù)器上分別編輯如下三個(gè)文件(兩個(gè)重點(diǎn)參數(shù)shardsvr、replSet)
cat /mongodb/shardsvr28001.conf
port=28001
dbpath=/mongodb/mongoshard28001
logpath=/mongodb/log/mongoshard28001.log
shardsvr=true
replSet=shard28001
fork=true
logappend=true
directoryperdb=true
verbose=true
bind_ip=0.0.0.0
pidfilepath=/mongodb/mongoshard28001.pid
cat /mongodb/shardsvr28002.conf
port=28002
dbpath=/mongodb/mongoshard28002
logpath=/mongodb/log/mongoshard28002.log
shardsvr=true
replSet=shard28002
fork=true
logappend=true
directoryperdb=true
verbose=true
bind_ip=0.0.0.0
pidfilepath=/mongodb/mongoshard28002.pid
cat /mongodb/shardsvr28003.conf
port=28003
dbpath=/mongodb/mongoshard28003
logpath=/mongodb/log/mongoshard28003.log
shardsvr=true
replSet=shard28003
fork=true
logappend=true
directoryperdb=true
verbose=true
bind_ip=0.0.0.0
pidfilepath=/mongodb/mongoshard28003.pid
2.2、兩臺(tái)服務(wù)器上分別啟動(dòng)Shard Server實(shí)例
mongod -f /mongodb/shardsvr28001.conf
mongod -f /mongodb/shardsvr28002.conf
mongod -f /mongodb/shardsvr28003.conf
2.3、配置兩臺(tái)服務(wù)器的3個(gè)Shard Server實(shí)例成Replica Set
登錄任意一臺(tái)服務(wù)器執(zhí)行如下操作即可
mongo --host 172.22.138.157 --port 28001
rs.initiate(
{
_id: "shard28001",
members: [
{ _id : 0, host : "172.22.138.157:28001" },
{ _id : 1, host : "172.22.138.158:28001" }
]
}
)
mongo --host 172.22.138.157 --port 28002
rs.initiate(
{
_id: "shard28002",
members: [
{ _id : 0, host : "172.22.138.157:28002" },
{ _id : 1, host : "172.22.138.158:28002" }
]
}
)
mongo --host 172.22.138.157 --port 28003
rs.initiate(
{
_id: "shard28003",
members: [
{ _id : 0, host : "172.22.138.157:28003" },
{ _id : 1, host : "172.22.138.158:28003" }
]
}
)
第三步
創(chuàng)建Route Server實(shí)例
3.1、在兩臺(tái)服務(wù)器上分別編輯如下文件(重點(diǎn)參數(shù)configdb,把上面第一步的Config Server實(shí)例配置進(jìn)來)
cat /mongodb/Routesvr27001.conf
port=27001
logpath=/mongodb/log/mongoRoute27001.log
configdb=config29001/172.22.138.157:29001,172.22.138.158:29001
fork=true
bind_ip=0.0.0.0
pidfilepath=/mongodb/mongoRoute27001.pid
3.2、兩臺(tái)服務(wù)器上分別啟動(dòng)Config Server實(shí)例
mongos -f /mongodb/Routesvr27001.conf
3.3、添加shard分片到集群,把上面第二步的Shard Server實(shí)例配置進(jìn)來
登錄任意一臺(tái)服務(wù)器執(zhí)行如下操作即可
mongo --host 172.22.138.157 --port 27001
sh.addShard( "shard28001/172.22.138.157:28001,172.22.138.158:28001")
sh.addShard( "shard28002/172.22.138.157:28002,172.22.138.158:28002")
sh.addShard( "shard28003/172.22.138.157:28003,172.22.138.158:28003")
第四步
驗(yàn)證
登錄任意一臺(tái)mongos實(shí)例,創(chuàng)建四個(gè)數(shù)據(jù)庫
mongo --host 172.22.138.157 --port 27001
mongos> use TDB1
mongos> db.createCollection("test01")
mongos> db.test01.insert({hid:1,hname:"w001"})
mongos> use TDB2
mongos> db.createCollection("test02")
mongos> db.test02.insert({hid:11,hname:"x001"})
mongos> use TDB3
mongos> db.createCollection("test03")
mongos> db.test03.insert({hid:111,hname:"y001"})
mongos> use TDB4
mongos> db.createCollection("test04")
mongos> db.test04.insert({hid:1111,hname:"z001"})
連接任意一臺(tái)mongos實(shí)例,都可以看到這四個(gè)數(shù)據(jù)庫,執(zhí)行db.printShardingStatus()看到四個(gè)數(shù)據(jù)庫被分到了不同的shard上了,TDB1分到了shard28001上,TDB2\TDB3分到了shard28002上,TDB4分到了shard28003上,登錄mongod實(shí)例shard集群分片28001端口只看到TDB1數(shù)據(jù)庫,登錄mongod實(shí)例集群分片28002端口只看到TDB2、TDB3數(shù)據(jù)庫,登錄mongod實(shí)例集群分片28003端口只看到TDB4數(shù)據(jù)庫,通過mongos,用戶看到所有數(shù)據(jù)庫,底下的shard server對用戶而言是可以不用了解的
mongo --host 172.22.138.158 --port 27001
mongos> show dbs
TDB1 0.000GB
TDB2 0.000GB
TDB3 0.000GB
TDB4 0.000GB
admin 0.000GB
config 0.001GB
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5d09cd1c38fe676e9fb94a92")
}
shards:
{ "_id" : "shard28001", "host" : "shard28001/172.22.138.157:28001,172.22.138.158:28001", "state" : 1 }
{ "_id" : "shard28002", "host" : "shard28002/172.22.138.157:28002,172.22.138.158:28002", "state" : 1 }
{ "_id" : "shard28003", "host" : "shard28003/172.22.138.157:28003,172.22.138.158:28003", "state" : 1 }
active mongoses:
"3.6.12" : 2
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "TDB1", "primary" : "shard28001", "partitioned" : false }
{ "_id" : "TDB2", "primary" : "shard28002", "partitioned" : false }
{ "_id" : "TDB3", "primary" : "shard28002", "partitioned" : false }
{ "_id" : "TDB4", "primary" : "shard28003", "partitioned" : false }
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard28001 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard28001 Timestamp(1, 0)
mongo --host 172.22.138.157 --port 28001
shard28001:PRIMARY> show dbs
TDB1 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
mongo --host 172.22.138.157 --port 28002
shard28002:PRIMARY> show dbs
TDB2 0.000GB
TDB3 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
mongo --host 172.22.138.157 --port 28003
shard28003:PRIMARY> show dbs
TDB4 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
第五步
如果想對某個(gè)數(shù)據(jù)庫里面的表分片存儲(chǔ),即A表一部分放入shared1,一部分放入shard2,就需要先使用sh.enableSharding("db_name")對這個(gè)數(shù)據(jù)庫啟用分片,數(shù)據(jù)庫啟用分片后,上面第四步驗(yàn)證結(jié)果"partitioned" : false就變成了"partitioned" : true。如果該表已包含數(shù)據(jù),則必須在使用shardCollection()對表分片之前使用db.collection.createIndex()方法在分片鍵上創(chuàng)建索引。如果表為空,MongoDB將創(chuàng)建索引作為shardCollection()的一部分。
mongo --host 172.22.138.158 --port 27001
mongos> use testdb
mongos> db.createCollection("table1")
mongos> db.printShardingStatus() --此時(shí)testdb對應(yīng)的數(shù)據(jù)庫"partitioned" : false
mongos> sh.enableSharding("testdb")
mongos> db.printShardingStatus() --此時(shí)testdb對應(yīng)的數(shù)據(jù)庫"partitioned" : true
對表testdb.table1按age字段范圍分片(代號為1),指定散列分片鍵對空集合進(jìn)行分片時(shí)最初要?jiǎng)?chuàng)建的塊數(shù)為5
mongos> sh.shardCollection("testdb.table1",{age:1},{numInitialChunks:5})
mongos> for (var i=0;i<150000;i++) db.table1.save({name:"u_"+i,age:0+i,sex:i%2,class:"number2",grade:"number3",birthd:"shanghai",motk:"shanghai",controyload:"chian",birthn:3030303030303030303,horbity:"footboll"})
對表testdb.table2按age字段范圍分片(代號為1),指定散列分片鍵對空集合進(jìn)行分片時(shí)最初要?jiǎng)?chuàng)建的塊數(shù)為1
mongos> db.createCollection("table2")
mongos> sh.shardCollection("testdb.table2",{age:1},{numInitialChunks:1})
mongos> for (var i=0;i<100000;i++) db.table2.save({name:"u_"+i,age:0+i,sex:i%2,class:"number2",grade:"number3",birthd:"shanghai",motk:"shanghai",controyload:"chian",birthn:3030303030303030303,horbity:"footboll"})
對表testdb.table3按age字段范圍分片(代號為hashed),指定散列分片鍵對空集合進(jìn)行分片時(shí)最初要?jiǎng)?chuàng)建的塊數(shù)為1
mongos> db.createCollection("table3")
mongos> sh.shardCollection("testdb.table2",{age:"hashed"},{numInitialChunks:1})
mongos> for (var i=0;i<100000;i++) db.table3.save({name:"u_"+i,age:0+i,sex:i%2,class:"number2",grade:"number3",birthd:"shanghai",motk:"shanghai",controyload:"chian",birthn:3030303030303030303,horbity:"footboll"})
上述內(nèi)容就是mongodb中怎么搭建shard_replica集群,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:mongodb中怎么搭建shard_replica集群
網(wǎng)頁地址:http://muchs.cn/article20/ghggjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)