mongodb實現分片儲存
mongodb分片介紹
分片是由副本集組成的系統
分片就是講資料庫進行拆分,將大型集合分割到不同伺服器上,比如:將原本有100G的資料,進行分割成10份儲存到不同的伺服器上,這樣每臺伺服器只儲存有10G的資料
mongodb通過一個mongos的(路由)程序實現分片後的資料儲存與訪問,也就是說mongos是整個分片架構的核心,對前端的程式而言不會清楚是否有分片的,客戶端只需要把讀寫操作轉達給mongos即可
雖然分片會把資料分割到很多臺伺服器上,但是每一個節點都需要有一臺備份的角色,這樣能保證資料的高可用性
當系統需要更多空間或資源的時候,分片可以讓我們按需方便擴充套件,只需要把mongodb服務的機器加入到分片叢集中即可
mongos分片結構
MongoDB分片相關概念
mongos:資料庫群集請求的入口,所有的請求都是通過mongos進行協調,不需要在應用程式上新增一個路由選擇器,mongos自己就是一個請求分發中心,它負責把對應的資料請求轉發到對應的shard伺服器上。在生產環境通常有洞mongos作為請求入口,防止其中一個掛掉後所有的mongodb服務請求都無法訪問
config server :配置伺服器,儲存所有的資料來源資訊(路由、分片)的配置。mongos本身不在硬碟中儲存分片伺服器和資料路由資訊,只是快取在記憶體裡,配置伺服器則實際儲存這些資料。mongos第一次啟動或者關閉重啟就會從config server中載入配置資訊,如果以後配置伺服器資訊發生變化會通知所有的mongos更新自己的狀態,這樣mongos就能繼續準確的路由。在生產環境中通常有多個config server配置伺服器,因為它儲存了分片路由的元資料,防止意外宕機故障造成資料丟失
shard :儲存了一個集合部分資料的MongoDB例項,每個分片是單獨的mongodb服務或者是一個副本集,在生產環境中,所有的分片都應該是副本集
mongodb分片搭建
分片搭建主機
實驗規劃,這裡只為了實現試驗需求,對測試中出現的情況實際上是一臺伺服器相當於部署了一個副本集,實際情況中則是多臺伺服器組成一個副本集,一臺伺服器承當三個服務角色。這裡請注意區分
三臺伺服器:1.115、1.223、1.234
以下是建立三個副本集的執行命令,使用如下命令需要注意副本集名稱和埠的設定!!
1.234搭建:mongods、config server、副本集1的主節點、副本集2的仲裁、副本集3的從節點
config={_id:"shard1",members:[{_id:0,host:"192.168.1.234:27001"},{_id:1,host:"192.168.1.115:27001"},{_id:2,host:"192.168.1.223:27001",arbiterOnly:true}]}
rs.initiate(config)
設定優先順序,仲裁角色設定為最低的優先順序
cfg = rs.conf()
cfg.members[0].priority = 3
cfg.members[1].priority = 2
cfg.members[2].priority = 1 仲裁角色
shard2:PRIMARY> rs.reconfig(cfg)
1.115搭建:mongos、config server、副本集1的從節點、副本集2的主節點、副本集3的仲裁
config={_id:"shard2",members:[{_id:0,host:"192.168.1.115:27002"},{_id:1,host:"192.168.1.234:27002",arbiterOnly:true},{_id:2,host:"192.168.1.223:27002"}]}
rs.initiate(config)
設定優先順序,仲裁角色設定為最低的優先順序
cfg = rs.conf()
cfg.members[0].priority = 3 主優先順序
cfg.members[1].priority = 1 仲裁角色
cfg.members[2].priority = 2
shard2:PRIMARY> rs.reconfig(cfg)
1.223搭建:mongos、config server、副本集1的仲裁、副本集2的從節點、副本集3的主節點
config={_id:"shard3",members:[{_id:0,host:"192.168.1.223:27003"},{_id:1,host:"192.168.1.115:27003",arbiterOnly:true},{_id:2,host:"192.168.1.234:27003"}]}
rs.initiate(config)
設定優先順序,仲裁角色設定為最低的優先順序
cfg = rs.conf()
cfg.members[0].priority = 3 主優先順序
cfg.members[1].priority = 1 仲裁角色
cfg.members[2].priority = 2
shard2:PRIMARY> rs.reconfig(cfg)
埠:mongos埠為20000,config server埠為21000,副本集1埠27001、副本集2埠27002、副本集3埠27003
三臺伺服器全部關閉selinux和firewalld服務,或者增加對應的埠規則
在三臺伺服器上都建立mongos和應用集的儲存目錄
[[email protected] ~]# mkdir -p /data/mongodb/mongos/log
[[email protected] ~]# mkdir -p /data/mongodb/config/{data,log}
[[email protected] ~]# mkdir -p /data/mongodb/shard1/{data,log}
[[email protected] ~]# mkdir -p /data/mongodb/shard2/{data,log}
[[email protected] ~]# mkdir -p /data/mongodb/shard3/{data,log}
建立config server的配置檔案,修改每臺配置。配置檔案除了監聽ip其他配置都相同
[[email protected] ~]# mkdir /etc/mongod
[[email protected] ~]# vim /etc/mongod/config.cnf
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 192.168.1.115
port = 21000
fork = true
configsvr = true #declare this is a config db of a cluster
replSet=configs #副本集名稱
maxConns=20000 #最大連線數
啟動config server服務,並檢視的啟動埠
[[email protected] ~]# mongod -f /etc/mongod/config.conf
2018-11-21T04:04:46.882+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 3474
child process started successfully, parent exiting
[[email protected] ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1750/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2757/master
tcp 0 0 192.168.1.115:21000 0.0.0.0:* LISTEN 3474/mongod
tcp 0 0 192.168.1.115:27017 0.0.0.0:* LISTEN 2797/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 2797/mongod
並登入到config server中,並初始化配置
[[email protected] ~]# mongo --host 192.168.1.115 --port 21000
MongoDB shell version v4.0.4
connecting to: mongodb://192.168.1.115:21000/
Implicit session: session { "id" : UUID("cb007228-2291-4164-9528-c68884a00cee") }
MongoDB server version: 4.0.4
Server has startup warnings:
> config = {_id:"configs",members:[{_id:0,host:"192.168.1.234:21000"},{_id:1,host:"192.168.1.223:21000"},{_id:2,host:"192.168.1.115:21000"}]}
{
"_id" : "configs",
"members" : [
{
"_id" : 0,
"host" : "192.168.1.234:21000"
},
{
"_id" : 1,
"host" : "192.168.1.223:21000"
},
{
"_id" : 2,
"host" : "192.168.1.115:21000"
}
]
}
初始化config server配置
> rs.initiate(config)
{
"ok" : 1,
"operationTime" : Timestamp(1542786565, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(1542786565, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(1542786565, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
為分片建立三個不同的副本集
新增副本集的配置檔案,三臺伺服器中都需要配置。每臺伺服器都要啟動一個副本集,所以監聽的埠和ip都要配置成不同的
副本集1埠27001、副本集2埠27002、副本集3埠27003
shard1的副本集主上配置,剩下的幾臺也是同樣配置,不同地方在shard的名稱和bind_ip 監聽的埠
[[email protected] ~]# cat /etc/mongod/shard1.conf
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log 每個副本集儲存資料目錄名、日誌及配置名,如副本集1和2:shard1、shard2
logappend = true
bind_ip = 192.168.1.234 每臺配置不一樣的監聽ip,ip為本地ip
port = 27001 每條配置不一樣的監聽埠,shard1為27001、shard2為27002、shard3為27003
fork = true
httpinterface=true 開啟web監控
rest=true
replSet=shard2
shardsvr = true
maxConn=20000
接下來幾臺機器可以使用sed批量替換字元來處理配置內容
[[email protected] log]# sed -i 's/shard1/shard2/g' /etc/mongod/shard2.conf
啟動副本集,並檢視監聽埠是否正確(此處只摘出副本集2的埠狀態,其他主機也是一樣的)
[[email protected] log]# mongod -f /etc/mongod/config.conf
2018-11-21T15:36:46.642+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 13109
child process started successfully, parent exiting
[[email protected] log]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1641/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 9267/nginx: master
tcp 0 0 192.168.1.223:21000 0.0.0.0:* LISTEN 13109/mongod
tcp 0 0 192.168.1.223:27017 0.0.0.0:* LISTEN 13040/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 13040/mongod
完成每臺的副本集配置,副本集配置要對每臺機器都配置相同的副本集埠監聽,比如機器1監聽了27001和27002埠,同樣機器2也必須監聽27001和27002埠。而27001屬於副本集1的、27002屬於副本集2的埠
配置截圖cat參考如下(副本集架構請在文章上面瞭解):(補充,圖中伺服器1.115的shard3配置出現一個低階錯誤,也請各位在配置時認真檢查自己的配置檔案,以免出現查不到問題的情況。出現錯誤也可以看mongo產生的日誌)
配置中的bind_ip根據機器網絡卡ip來設定
(配置檔案中的配置項:
httpinterface=true
rest=true
maxConn=20000
在新增這幾個配置項後啟動時不能識別這幾個引數,所以後續我沒有新增這幾個引數來啟動的):
[[email protected] /]# cat /etc/mongod/shard1.conf
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 192.168.1.115 這裡ip隨機器ip做修改,因為這裡是服務啟動監聽的ip
port = 27001 副本集1監聽埠
fork = true
httpinterface=true
rest=true
replSet=shard1
shardsvr = true
maxConn=20000
[[email protected] /]# cat /etc/mongod/shard2.conf
pidfilepath = /var/run/mongodb/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 192.168.1.115
port = 27002 副本集2監聽埠
fork = true
httpinterface=true
rest=true
replSet=shard2
shardsvr = true
maxConn=20000
[[email protected] /]# cat /etc/mongod/shard3.conf
pidfilepath = /var/run/mongodb/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 192.168.1.115
port = 27003 副本集3監聽埠
fork = true
httpinterface=true
rest=true
replSet=shard2
shardsvr = true
maxConn=20000
這裡先啟動shard1的副本集
初始化副本集可以在1.115或者1.234進行,為什麼不能在1.223上進行初始化副本集:因為1.223是作為副本集shard1的仲裁者,所以需要把1.223排除在副本集之外,這也就是為什麼測試至少需要三臺機器的原因,因為如果開始只拿兩臺做這個測試會因為1臺副本集、1臺仲裁的架構而不成立。
啟動完成後登入每臺的副本集中,指定副本集中的主從角色
在副本集中加入其他節點,並執行初始化副本集語句:
[[email protected] /]# mongo -host 192.168.1.234 --port 27001
MongoDB shell version v4.0.4
connecting to: mongodb://192.168.1.234:27001/
Implicit session: session { "id" : UUID("0cbd0b1e-db62-4fe2-b20f-a5d23a60fdf5") }
MongoDB server version: 4.0.4
use admin
> use admin
switched to db admin
> config={_id:"shard1",members:[{_id:0,host:"192.168.1.234:27001"},{_id:1,host:"192.168.1.115:27001"},{_id:2,host:"192.168.1.223:27001", arbiterOnly:true}]}
{
"_id" : "shard1",
"members" : [
{
"_id" : 0,
"host" : "192.168.1.234:27001"
},
{
"_id" : 1,
"host" : "192.168.1.115:27001"
},
{
"_id" : 2,
"host" : "192.168.1.223:27001",
"arbiterOnly" : true
}
]
}
初始化副本集,ok提示配置正確初始化成功
> rs.initiate(config)
{
"ok" : 1,
"operationTime" : Timestamp(1542796014, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1542796014, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
檢視節點資訊
shard1:SECONDARY> rs.status()
{
"set" : "shard1",
"date" : ISODate("2018-11-21T10:27:01.259Z"),
"myState" : 2,
"term" : NumberLong(0),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1542796014, 1),
"t" : NumberLong(-1)
},
"durableOpTime" : {
"ts" : Timestamp(1542796014, 1),
"t" : NumberLong(-1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(0, 0),
"members" : [
{
"_id" : 0,
"name" : "192.168.1.234:27001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 606,
"optime" : {
"ts" : Timestamp(1542796014, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2018-11-21T10:26:54Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.1.115:27001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 7,
"optime" : {
"ts" : Timestamp(1542796014, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1542796014, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2018-11-21T10:26:54Z"),
"optimeDurableDate" : ISODate("2018-11-21T10:26:54Z"),
"lastHeartbeat" : ISODate("2018-11-21T10:27:01.231Z"),
"lastHeartbeatRecv" : ISODate("2018-11-21T10:27:00.799Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.1.223:27001",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 7,
"lastHeartbeat" : ISODate("2018-11-21T10:27:01.231Z"),
"lastHeartbeatRecv" : ISODate("2018-11-21T10:27:00.210Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1542796014, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1542796014, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
其他副本集按照文章頭給的架構來分配,後面只給出2、3副本集中執行的語句:
以下是建立三個副本集的執行命令,使用如下命令需要注意副本集名稱和埠的設定!!
1.234搭建:mongods、config server、副本集1的主節點、副本集2的仲裁、副本集3的從節點
config={_id:"shard1",members:[{_id:0,host:"192.168.1.234:27001"},{_id:1,host:"192.168.1.115:27001"},{_id:2,host:"192.168.1.223:27001",arbiterOnly:true}]}
rs.initiate(config)
設定優先順序,仲裁角色設定為最低的優先順序
cfg = rs.conf()
cfg.members[0].priority = 3
cfg.members[1].priority = 2
cfg.members[2].priority = 1 仲裁角色
shard2:PRIMARY> rs.reconfig(cfg)
1.115搭建:mongos、config server、副本集1的從節點、副本集2的主節點、副本集3的仲裁
config={_id:"shard2",members:[{_id:0,host:"192.168.1.115:27002"},{_id:1,host:"192.168.1.234:27002",arbiterOnly:true},{_id:2,host:"192.168.1.223:27002"}]}
rs.initiate(config)
設定優先順序,仲裁角色設定為最低的優先順序
cfg = rs.conf()
cfg.members[0].priority = 3 主優先順序
cfg.members[1].priority = 1 仲裁角色
cfg.members[2].priority = 2
shard2:PRIMARY> rs.reconfig(cfg)
1.223搭建:mongos、config server、副本集1的仲裁、副本集2的從節點、副本集3的主節點
config={_id:"shard3",members:[{_id:0,host:"192.168.1.223:27003"},{_id:1,host:"192.168.1.115:27003",arbiterOnly:true},{_id:2,host:"192.168.1.234:27003"}]}
rs.initiate(config)
設定優先順序,仲裁角色設定為最低的優先順序
cfg = rs.conf()
cfg.members[0].priority = 3 主優先順序
cfg.members[1].priority = 1 仲裁角色
cfg.members[2].priority = 2
shard2:PRIMARY> rs.reconfig(cfg)
至此三個副本集都建立成功了,並且指定了主從、仲裁角色之間的優先順序
mongodb分片-配置路由服務
mongos配置
mongos最後配置的原因是因為需要使用到副本集和config server的資訊
在三臺機器上建立mongos的配置檔案,配置檔案中的監聽ip根據不同機器進行修改
configdb配置中是每個機器的config server的ip和監聽埠
[[email protected] log]# cat /etc/mongod/mongos.conf
pidfilepath = /var/run/mongodb/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 192.168.1.223
port = 20000
fork = true
configdb = configs/192.168.1.234:21000,192.168.1.115:21000,192.168.1.223:21000
maxConns=20000
啟動三臺機器上的mongos服務並檢視執行的監聽埠
[[email protected] log]# netstat -ntlp |grep mongos
tcp 0 0 192.168.1.223:20000 0.0.0.0:* LISTEN 14327/mongos
啟動每臺的mongos後,隨意登入一臺mongos中(監聽埠20000)
把所有的副本集的分片服務與路由器串聯起來(新增到服務分發表中)
新增每個副本集到mongos中的時候,需要注意副本集名稱和副本集伺服器的監聽埠和ip是否寫入正確
mongos> sh.addShard("shard1/192.168.1.234:27001,192.168.1.115:27001,192.168.1.223:27001")
{
"shardAdded" : "shard1",
"ok" : 1,
"operationTime" : Timestamp(1542893389, 7),
"$clusterTime" : {
"clusterTime" : Timestamp(1542893389, 7),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("shard2/192.168.1.234:27002,192.168.1.115:27002,192.168.1.223:27002")
{
"shardAdded" : "shard2",
"ok" : 1,
"operationTime" : Timestamp(1542893390, 15),
"$clusterTime" : {
"clusterTime" : Timestamp(1542893390, 15),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("shard3/192.168.1.234:27003,192.168.1.115:27003,192.168.1.223:27003")
{
"shardAdded" : "shard3",
"ok" : 1,
"operationTime" : Timestamp(1542893415, 11),
"$clusterTime" : {
"clusterTime" : Timestamp(1542893415, 11),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
檢視mongos分片群集狀態資訊,著重關注Currently enabled: yes和Currently enabled: yes資訊提示。
Currently running: no 未執行是因為這個分片群集中還沒有執行任何庫跟集合,所以這裡顯示狀態為 no running
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5bf50e12a32cd4af8077b386")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.1.115:27001,192.168.1.234:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.1.115:27002,192.168.1.223:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.1.223:27003,192.168.1.234:27003", "state" : 1 }
active mongoses:
"4.0.4" : 1
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" : "config", "primary" : "config", "partitioned" : true }
副本集新增完成後就可以建立資料庫來進行測試
mongodb分片測試
登入任意一臺的mongos20000埠
建立庫和集合,並向集合中插入內容。並且檢視分片群集狀態資訊
建立兩個庫,名字為testdb和db2的庫
使用db.runCommand({enablesharding:"庫名稱"})或者 sh.enableSharding("庫名稱")來建立
結果返回ok: 1表示建立庫成功
mongos> db.runCommand({enablesharding:"testdb"})
{
"ok" : 1,
"operationTime" : Timestamp(1542894725, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1542894725, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.enableSharding("db2")
{
"ok" : 1,
"operationTime" : Timestamp(1542894795, 11),
"$clusterTime" : {
"clusterTime" : Timestamp(1542894795, 11),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
指定資料庫裡需要分片的集合和片鍵
執行db.runCommand({shardcollection:"庫名.集合名",key:{id:1鍵值}})或者sh.shardCollection("庫名2.集合名2",{"id":1鍵值})
建立操作結果如下:
mongos> db.runCommand({shardcollection:"testdb.table1",key:{id:1}})
{
"collectionsharded" : "testdb.table1",
"collectionUUID" : UUID("79968692-36d4-4ea4-b095-15ba3e4c8d87"),
"ok" : 1,
"operationTime" : Timestamp(1542895010, 17),
"$clusterTime" : {
"clusterTime" : Timestamp(1542895010, 17),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.shardCollection("db2.tl2",{"id":1})
{
"collectionsharded" : "db2.tl2",
"collectionUUID" : UUID("9a194657-8d47-48c9-885d-6888d0625f91"),
"ok" : 1,
"operationTime" : Timestamp(1542895065, 16),
"$clusterTime" : {
"clusterTime" : Timestamp(1542895065, 16),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
進入庫中,向集合內插入10000條資料
mongos> use testdb
switched to db testdb
mongos> for (var i=1;i<10000;i++)db.teble1.save({id:1,"test1":"testval1"})
WriteResult({ "nInserted" : 1 })
mongos> db.table1.stats() 檢視teble1這個表的狀態資訊,顯示資訊較多,這裡就不再列舉
檢視分片狀態
檢視建立的庫被分片到了哪個副本集當中,以"_id:"格式開頭的行,在下面可以看到chunks : shard3或者chunks : shard2的內容,表示了建立資料庫會被分片到多個副本集當中儲存,簡單測試完成
db2庫儲存到了shard3副本集中。db3儲存到了shard2副本集當中
當資料量較大的時候,mongos才能更平均的分片儲存資料,測試的資料量太少,本次測試完成不了大資料量儲存的儲存狀態顯示
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
{ "_id" : "db2", "primary" : "shard3", "partitioned" : true, "version" : { "uuid" : UUID("35e3bf1c-cd35-4b43-89fb-04b16c54d00e"), "lastMod" : 1 } }
db2.tl2
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard3 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard3 Timestamp(1, 0)
{ "_id" : "db3", "primary" : "shard2", "partitioned" : true, "version" : { "uuid" : UUID("a3d2c30b-0842-4d27-9de3-ac4a7710789a"), "lastMod" : 1 } }
db3.tl3
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard2 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0)
{ "_id" : "db4", "primary" : "shard2", "parti