Home  >  Article  >  Database  >  mongoDB第八讲:分片

mongoDB第八讲:分片

WBOY
WBOYOriginal
2016-06-07 15:54:401144browse

1.插入负载技术-分片架构图 2.片键的概念和用处 看下面这个普通的集合和分片后的结果:利用key为片键进行自动分片 3.什么时候用到分片呢? 3.1机器的磁盘空间不足 3.2单个的mongoDB服务器已经不能满足大量的插入操作 3.3想通过把大数据放到内存中来提高性能 4.

1.插入负载技术->分片架构图

2.片键的概念和用处

看下面这个普通的集合和分片后的结果:利用key为片键进行自动分片

3.什么时候用到分片呢?

3.1机器的磁盘空间不足

3.2单个的mongoDB服务器已经不能满足大量的插入操作

3.3想通过把大数据放到内存中来提高性能

4.分片步骤

4.1创建一个配置服务器

//配置服务器.conf

dbpath = D:\app\mongodata\config
port = 2000
bind_ip = 127.0.0.1

//配置服务器.bat

mongod --config 配置服务器.conf

4.2创建路由服务器,并且连接配置服务器,路由器是调用mongos命令

mongos --port 1000 --configdb 127.0.0.1:2000

4.3添加2个分片数据库:8081和8082

//分片数据库_01.conf

dbpath = D:\app\mongodata\01
port = 8081
bind_ip = 127.0.0.1

//分片数据库_01.bat

mongod --config 分片数据库_01.conf

//8082同上

4.5利用路由为集群添加分片(允许本地访问)

db.runCommand({addshard:"127.0.0.1:8081",allowLocal:true})

db.runCommand({addshard:"127.0.0.1:8081",allowLocal:true})

切记之前不能使用任何数据库语句

4.6打开数据分片功能,为数据库foobar打开分片功能

db.runCommand({"enablesharding":"foobar"})

4.7对集合进行分片

db.runCommand({"shardcollection":"foobar.bar","key":{"_id":1}})

4.8利用大数据量进行测试 (800000条)

function add(){
var i = 0;
for(;i db.bar.insert({"age":i+10,"name":"jim"})
}
}

function add2(){
var i = 0;
for(;i db.bar.insert({"age":12,"name":"tom"+i})
}
}

function add3(){
var i = 0;
for(;i db.bar.insert({"age":12,"name":"lili"+i})
}
}

//查看状态
db.printShardingStatus()

5.查看配置库对于分片服务器的配置存储

db.printShardingStatus()

6.查看集群对bar的自动分片机制配置信息

mongos> db.shards.find()

{ "_id" :"shard0000", "host" : "127.0.0.1:8081" }

{ "_id" :"shard0001", "host" : "127.0.0.1:8082" }

7.保险起见的配置服务器集群

8.分片与副本集一起使用

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn