mongodb 分片集群 備份恢復
一、問題:當已經分片的集合備份後,恢復資料時不能按片鍵分片
二、解決方法:在恢復資料前建立集合分片規則
三、測試環境:
os: centos linux release 7.4.1708 (core)
mongo: mongodb shell version v4.0.22-rc0
物理機三颱:192.168.126.11、192.168.126.12、192.168.126.13
shard1: 192.168.126.11:27017(p)、192.168.126.12:27017(s)、192.168.126.13:27017(a) #p為主,s為從,a為仲
shard2: 192.168.126.11:27018(a)、192.168.126.12:27018(p)、192.168.126.13:27018(s)
shard3: 192.168.126.11:27019(s)、192.168.126.12:27019(a)、192.168.126.13:27019(p)
config: 192.168.126.11:27100(p)、192.168.126.12:27100(s)、192.168.126.13:27100(s)
mongos: 192.168.126.11:27000、192.168.126.12:27000、192.168.126.13:27000
測試集合z1.a4已經有集合分片策略
z1.a4 #z1庫a4集合
shard key:
unique: false
balancing: true
chunks: #在shard1、2、3分別有chunk
shard1 2
shard2 2
shard3 2
} -->> on : shard1 timestamp(1, 0)
-->> on : shard1 timestamp(1, 1)
-->> on : shard2 timestamp(1, 2)
-->> on : shard2 timestamp(1, 3)
-->> on : shard3 timestamp(1, 4)
-->> } on : shard3 timestamp(1, 5)
四、操作步驟:
1、停止balancer
mongos> sh.getbalancerstate() #balancer狀態,可用
true
mongos> sh.stopbalancer() #停止balancer }}
mongos> sh.getbalancerstate() #balancer狀態,不可用
false
2、匯出資料庫z1
[root@v1 ~]# mongodump -h 192.168.126.11:27000 -d z1 -o ~/z1_mongos_202102151206.bak
2021-02-15t12:06:45.118+0800 writing z1.a4 to
2021-02-15t12:06:45.118+0800 writing z1.a1 to
2021-02-15t12:06:45.118+0800 writing z1.a3 to
2021-02-15t12:06:45.118+0800 writing z1.a2 to
2021-02-15t12:06:45.247+0800 done dumping z1.a2 (1000 documents)
2021-02-15t12:06:45.248+0800 writing z1.tt to
2021-02-15t12:06:45.248+0800 done dumping z1.a3 (1000 documents)
2021-02-15t12:06:45.248+0800 writing z1.a5 to
2021-02-15t12:06:45.268+0800 done dumping z1.tt (1 document)
2021-02-15t12:06:45.270+0800 done dumping z1.a5 (0 documents)
2021-02-15t12:06:45.279+0800 done dumping z1.a1 (2000 documents)
2021-02-15t12:06:45.969+0800 done dumping z1.a4 (11000 documents) #11000條資料
3、刪除a4集合,不建立集合分片策略直接匯入
#刪除a4集合
mongos> use z1
switched to db z1
mongos> db.a4.drop()
true
#匯入a4集合
[root@v1 ~]# mongorestore -h 192.168.126.11:27000 -d z1 -c a4 ~/z1_mongos_202102151206.bak/z1/a4.bson
2021-02-15t12:11:59.879+0800 checking for collection data in /root/z1_mongos_202102151206.bak/z1/a4.bson
2021-02-15t12:11:59.883+0800 reading metadata for z1.a4 from /root/z1_mongos_202102151206.bak/z1/a4.metadata.json
2021-02-15t12:11:59.938+0800 restoring z1.a4 from /root/z1_mongos_202102151206.bak/z1/a4.bson
2021-02-15t12:12:00.984+0800 restoring indexes for collection z1.a4 from metadata
2021-02-15t12:12:01.054+0800 finished restoring z1.a4 (11000 documents)
2021-02-15t12:12:01.054+0800 done
#檢視a4集合狀態
mongos> db.a4.stats() }}
#建立z1.a4集合的分片策略,根據name欄位的hash
mongos> sh.shardcollection('z1.a4', ) }}
#sh.status()
z1.a4 #可看到分片
shard key:
unique: false
balancing: true
chunks:
shard1 7
shard2 8
shard3 7
too many chunks to print, use verbose if you want to force print
#匯入集合
[root@v1 ~]# mongorestore -h 192.168.126.11:27000 -d z1 -c a4 ~/z1_mongos_202102151206.bak/z1/a4.bson
2021-02-15t12:54:58.421+0800 checking for collection data in /root/z1_mongos_202102151206.bak/z1/a4.bson
2021-02-15t12:54:58.431+0800 reading metadata for z1.a4 from /root/z1_mongos_202102151206.bak/z1/a4.metadata.json
2021-02-15t12:54:58.431+0800 restoring z1.a4 from /root/z1_mongos_202102151206.bak/z1/a4.bson
2021-02-15t12:54:59.884+0800 restoring indexes for collection z1.a4 from metadata
2021-02-15t12:54:59.886+0800 finished restoring z1.a4 (11000 documents)
2021-02-15t12:54:59.886+0800 done
#驗證mongos> db.a4.stats() }}
mongos> sh.status()
--- sharding status ---
sharding version:
shards:
active mongoses:
"4.0.22-rc0" : 3
autosplit:
currently enabled: yes
balancer:
currently enabled: yes #驗證當前balancer狀態
currently running: no
failed balancer rounds in last 5 attempts: 0
MongoDB集群分片
什麼是sharding?說白了就是把海量資料水平擴充套件的集群系統,資料分表儲存在sharding的各個節點上。mongodb的資料分開分為chunk,每個chunk都是collection中的一段連續的資料記錄,一般為200mb,超出則生成新的資料塊。構建sharding需要三種角色,shard伺...
mongodb分片 集群
目前在乙個機器上部署,ip 10.1.2.197,埠列表如下 埠埠埠路由服務 27061 路由服務 27062 路由服務 27063 配置服務 27071 配置服務 27072 配置服務 27073 副本集1 27011 副本集2 27021 副本集3 27031 副本集1 27012 副本集2 2...
MongoDB 分片集群
在mongodb裡面存在另一種集群,就是分片技術,可以滿足mongodb資料量大量增長的需求。當mongodb儲存海量的資料時,一台機器可能不足以儲存資料,也可能不足以提供可接受的讀寫吞吐量。這時,我們就可以通過在多台機器上分割資料,使得資料庫系統能儲存和處理更多的資料。下圖展示了在mongodb中...