建議:mongo備份盡量使用mongodump和mongorestore
博主使用json文件的原因:后期會用json文件更新以前的老數據,會用到upsert
-----不說廢話了。
1、首先關閉集群自動平衡
(1) 連接到路由mongos節點
(2) 停止balance
sh.stopBalancer()
(3) 查看balance狀態
sh.getBalancerState()
(4)停止balance 后,沒有遷移進程正在遷移,可以執行下列命令
use config
while( sh.isBalancerRunning() ) {
print("waiting...");
sleep(1000);
}
1.1、 如果balance開啟,查看是否正在有數據的遷移
連接mongo集群的路由節點
mongos> sh.isBalancerRunning()
false
2、導入json
mongoimport -d xxx -h 127.0.0.1:20000 --username xxx --password xxx -c xxx --file /xxx/xxx.json --numInsertionWorkers 10
【詳解】
-d 數據庫名稱
-h 主機ip:端口
--username 用戶名
--password 密碼
-c 集合
--file 文件路徑
--numInsertionWorkers N條線程執行
3、 打開balance
(1) 連接到路由mongos節點
(2) 打開balance
sh.setBalancerState(true)
4、集合分片
use dbName
sh.enableSharding("dbName")
創建集合
db.createCollection("collectionName")
創建索引
db['collectionName'].createIndex({"x":1,"y":1})
指定分片鍵
sh.shardCollection("dbName.collectionName",{"x":1})
開啟自動分割
sh.enableAutoSplit()