MongoDB集群之分片

时间:2024-11-27 15:04:44

原文:点击打开链接

MongoDB分片

分片(sharding)是将数据拆分,将其分散在不同的机器上的过程。MongoDB支持自动分片

片键(shard
key)设置分片时,需要从集合里面选一个键,用该键作为数据拆分的依据,此键就叫片键

分片一般有以下三部分组成:

片     可以是普通的mongod进程,也可以是副本集。

但是即使一片内有多台服务器,也只能有一个主服务器,其他的服务器保存相同的数据

Mongos

就是mongodb的路由器进程,它路由所有请求,然后将结果聚合。它不保存存储数据或配置信息

它本身并不存储数据或配置信息(但会缓存配置服务器信息)

配置服务器

存储集群的配置信息:数据和片的对应关系

Mongos不会永久存放数据,所以需要个地方存放分片配置

环境规划

 

Dbpath

logpath

port

备注

1

/data/mongodb/conf

/data/mongodb/logs/conf.log

10000

配置服务器

2

——

/data/mongodb/logs/mongos.log

20000

mongos

3

/data/mongodb/shard1

/data/mongodb/logs/shard1.log

30000

分片1

4

/data/mongodb/shard2

/data/mongodb/logs/shard2.log

40000

分片2

创建db|logpath目录

~]# mkdir /data/mongodb/conf /data/mongodb/shard{1,2}

~]# mkdir /data/mongodb/logs/

启动配置服务

#配置服务器要最先启动,因为mongos会用它其上的配置信息

~]# mongod --dbpath /data/mongodb/conf --logpath /data/mongodb/logs/conf.log --logappend --fork --port 10000

about to fork child process, waiting until server is ready for connections.

forked process: 14234

child process started successfully, parent exiting

启动路由服务

#mongos进程不需要数据目录

~]# mongos --port 20000 --configdb 192.168.211.217:10000 --logpath /data/mongodb/logs/mongos.log  --logappend --fork

2015-01-15T17:21:13.758+0800 warning: running with 1 config server should be done only for testing purposes and is not recommended for production

about to fork child process, waiting until server is ready for connections.

forked process: 15361

child process started successfully, parent exiting

启动分片服务

~]#mongod --fork --dbpath /data/mongodb/shard1 --logpath /data/mongodb/logs/shard1.log --logappend -port 30000

about to fork child process, waiting until server is ready for connections.

forked process: 15426

child process started successfully, parent exiting

~]#mongod --fork --dbpath /data/mongodb/shard2 --logpath /data/mongodb/logs/shard2.log --logappend -port 40000

about to fork child process, waiting until server is ready for connections.

forked process: 15515

child process started successfully, parent exiting

添加分片

#查看mongos的端口

~]# netstat  -ntpl|grep mongos

tcp        0      0 0.0.0.0:20000               0.0.0.0:*                  
LISTEN      15361/mongos

#连接刚才启动的mongos(192.168.211.217:20000)

~]# /usr/local/mongodb/bin/mongo --port 20000

MongoDB shell version: 2.6.6

connecting to: 127.0.0.1:20000/test

mongos了mongos> use
admin

switched to db admin

mongos> db.runCommand({addshard:"192.168.211.217:30000",allowLocal:true })

{ "shardAdded" : "shard0000", "ok" : 1 }

mongos> db.runCommand({addshard:"192.168.211.217:40000",allowLocal:true })

{ "shardAdded" : "shard0001", "ok" : 1 }

注:allowLocal:true当路由进程和分片在同一台机器上要指定allowLocal为true

mongos进程

mongos> use config

switched to db config

mongos> db.shards.find()

{ "_id" : "shard0000", "host" : "192.168.211.217:30000" }

{ "_id" : "shard0001", "host" : "192.168.211.217:40000" }

切分数据

默认的是不会将存储的每条数据进行分片处理,需要在数据库和集合的粒度上都开启分片功能

test库的分片功能#连接emp集合#对集合进行分片(

mongos> use admin

switched to db admin

mongos> db.runCommand({"shardcollection":"test.emp","key":{"_id":1}})

{ "collectionsharded" : "test.emp", "ok" : 1 }

mongos> use config

switched to db config

mongos> db.databases.find()

{ "_id" : "admin", "partitioned" : false, "primary" : "config" }

{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }

true则表示开启了分片功“primary”,字符串。这个值与“大本营

mongos> db.collections.find()

{ "_id" : "test.emp", "lastmod" : ISODate("2015-01-15T09:48:49.919Z"), "dropped" : false, "key" : { "_id" : 1 }, "unique" : false, "lastmodEpoch" : ObjectId("54b78d0167f270682893cda6")
}

mongos> db.chunks.find()

{ "_id" : "test.emp-_id_MinKey", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("54b78d0167f270682893cda6"), "ns" : "test.emp", "min" : { "_id" : { "$minKey" : 1 } }, "max" : {
"_id" : { "$maxKey" : 1 } }, "shard" : "shard0000" }

emp集合就按照“_id”片键分散到各个片上                       
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)