mongodb集群导入数据大的json文件

建议: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()
原文地址:https://www.cnblogs.com/cchilei/p/12752162.html