数据分片
分片缘由
分片(sharding)是MongoDB用来将大型集合分割到不同服务器(集群)上所采用的方法。当单台服务器CPU,Memory,IO等无法满足需求,就需要将数据分片存放,减缓服务器压力。
分片架构
实验拓扑
配置过程
因为以上做过实验我们首先来清理一下数据
[root@node1 ~]# service mongod stop
Stopping mongod: [ OK ]
[root@node1 ~]# rm -rf /mongodb/data/*
#各节点都执行以上操作,若第一次做可忽略
Config Server配置
#安装所需包
[root@node2 mongodb]# ls
mongodb-org-server-2.6.10-1.x86_64.rpm mongodb-org-tools-2.6.10-1.x86_64.rpm
mongodb-org-shell-2.6.10-1.x86_64.rpm
[root@node2 mongodb]# yum install *.rpm -y
修改配置文件[root@node2 ~]# vim /etc//mongod.conf
logpath=/var/log/mongodb/mongod.log
logappend=true
fork=true
dbpath=/mongodb/data
configsvr=true #开启config server
pidfilepath=/var/run/mongodb/mongod.pid
#bind_ip=127.0.0.1
httpinterface=true
rest=true
创建数据目录
[root@node2 ~]# mkdir /mongodb/data -pv
mkdir: created directory `/mongodb'
mkdir: created directory `/mongodb/data'
[root@node2 ~]# chown -R mongod.mongod /mongodb
启动服务[root@node2 ~]# service mongod start
Starting mongod: [ OK ]
[root@node2 ~]# ss -tnl | grep 27019
LISTEN 0 128 *:27019 *:*
#监听端口已发生改变,不再是27017
Mongos配置
#安装所需包,Mongos节点只装此包即可,无需装第一次实验中的mongod的相关包
[root@node1 ~]# yum install mongodb-org-mongos-2.6.10-1.x86_64.rpm -y
启动服务
[root@node1 ~]# mongos --configdb=172.16.10.124 --fork --logpath=/var/log/mongodb/mongos.log
2015-07-13T22:22:47.404+0800 warning: running with 1 config server should be done only fo
r testing purposes and is not recommended for production
about to fork child process, waiting until server is ready for connections.
forked process: 3583
child process started successfully, parent exiting
#--configdb指定config server --logpath指定日志位置 --fork后台运行
Shard配置
#以为我们第一次实验安装过软件了,下面直接修改配置文件
[root@node3 ~]# vim /etc/mongod.conf
logpath=/var/log/mongodb/mongod.log
logappend=true
fork=true
dbpath=/mongodb/data
pidfilepath=/var/run/mongodb/mongod.pid
#bind_ip=127.0.0.1
httpinterface=true
rest=true
[root@node3 ~]# service mongod start
Starting mongod: [ OK ]
#两个shard节点都执行以上操作
Mongos节点添加Shard节点
[root@node1 ~]# mongo --host 172.16.10.123
MongoDB shell version: 2.6.10
connecting to: 172.16.10.123:27017/test
mongos> sh.addShard("172.16.10.125")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("172.16.10.126")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("55a3c9ba131b83ff44e19435")
}
shards:
{ "_id" : "shard0000", "host" : "172.16.10.125:27017" }
{ "_id" : "shard0001", "host" : "172.16.10.126:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
对所需对象启用分片功能
#对数据库启用sharding功能
mongos> sh.enableSharding("testdb")
{ "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("55a3c9ba131b83ff44e19435")
}
shards:
{ "_id" : "shard0000", "host" : "172.16.10.125:27017" }
{ "_id" : "shard0001", "host" : "172.16.10.126:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "testdb", "partitioned" : true, "primary" : "shard0000" }
#指定需要分片的Collection及索引
mongos> sh.shardCollection("testdb.students",{"age": 1})
{ "collectionsharded" : "testdb.students", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("55a3c9ba131b83ff44e19435")
}
shards:
{ "_id" : "shard0000", "host" : "172.16.10.125:27017" }
{ "_id" : "shard0001", "host" : "172.16.10.126:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "testdb", "partitioned" : true, "primary" : "shard0000" }
testdb.students
shard key: { "age" : 1 }
chunks:
shard0000 1
{ "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } }
on : shard0000 Timestamp(1, 0)
分片功能已开启,接下来我们手动创建数据来验证是否会分片
mongos> use testdb
switched to db testdb
mongos> for (i=1;i<=100000;i++) db.students.insert({name:"student"+i,age:(i%120),addr:"china"})
WriteResult({ "nInserted" : 1 })
mongos> db.students.find().count()
100000
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("55a3c9ba131b83ff44e19435")
}
shards:
{ "_id" : "shard0000", "host" : "172.16.10.125:27017" }
{ "_id" : "shard0001", "host" : "172.16.10.126:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
{ "_id" : "testdb", "partitioned" : true, "primary" : "shard0000" }
testdb.students
shard key: { "age" : 1 }
chunks:
shard0001 1
shard0000 2
{ "age" : { "$minKey" : 1 } } -->> { "age" : 1 } on : shard0001 Timestamp(2, 0)
{ "age" : 1 } -->> { "age" : 119 } on : shard0000 Timestamp(2, 2)
{ "age" : 119 } -->> { "age" : { "$maxKey" : 1 } } on : shard0000 Timestamp(2, 3)
查看数据状态会发现数据已被分到不同shard上,至此,数据分片成功实现
The end
MongoDB复制集及数据分片就先说到这里了,通过以上简单应用可以看出,MongoDB在无论大数据处理还是复制方面都有着比MySQL更加优秀的性能和更加简单的操作,但由于MongoDB目前还不是很成熟,在实际的应用中还有许多问题有待解决,相信在未来MongoDB会更加出色。