365Tools
    发布时间:2024-03-29 20:30:02
分片是跨多台机器存储数据的过程,它是 MongoDB 满足数据增长需求的方法。随着数据的不断增加,单台机器可能不足以存储全部数据,也无法提供足够的读写吞吐量。通过分片,您可以添加更多计算机来满足数据增长和读/写操作的需求。
	Shard Server 1:27020
	Shard Server 2:27021
	Shard Server 3:27022
	Shard Server 4:27023
	Config Server :27100
	Route Process:40000
	[root@100 /]# mkdir -p /www/mongoDB/shard/s0
	[root@100 /]# mkdir -p /www/mongoDB/shard/s1
	[root@100 /]# mkdir -p /www/mongoDB/shard/s2
	[root@100 /]# mkdir -p /www/mongoDB/shard/s3
	[root@100 /]# mkdir -p /www/mongoDB/shard/log
	[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27020 --dbpath=/www/mongoDB/shard/s0 --logpath=/www/mongoDB/shard/log/s0.log --logappend --fork
	....
	[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27023 --dbpath=/www/mongoDB/shard/s3 --logpath=/www/mongoDB/shard/log/s3.log --logappend --fork
	[root@100 /]# mkdir -p /www/mongoDB/shard/config
	[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27100 --dbpath=/www/mongoDB/shard/config --logpath=/www/mongoDB/shard/log/config.log --logappend --fork
注意:这里我们完全可以像启动普通 mongodb 服务一样启动,不需要添加 shardsvr 和 configsvr 参数。因为这两个参数的作用就是改变启动端口的,所以我们自行指定端口就可以了。
步骤 3) 启动 Route Process/usr/local/mongoDB/bin/mongos --port 40000 --configdb localhost:27100 --fork --logpath=/www/mongoDB/shard/log/route.log --chunkSize 500
mongos 启动参数中,chunkSize 这一项是用来指定 chunk 的大小的,单位是 MB,默认大小为 200MB。
	[root@100 shard]# /usr/local/mongoDB/bin/mongo admin --port 40000
	MongoDB shell version: 2.0.7
	connecting to: 127.0.0.1:40000/admin
	mongos> db.runCommand({ addshard:"localhost:27020" })
	{ "shardAdded" : "shard0000", "ok" : 1 }
	......
	mongos> db.runCommand({ addshard:"localhost:27029" })
	{ "shardAdded" : "shard0009", "ok" : 1 }
	mongos> db.runCommand({ enablesharding:"test" }) #设置分片存储的数据库
	{ "ok" : 1 }
	mongos> db.runCommand({ shardcollection: "test.log", key: { id:1,time:1}})
	{ "collectionsharded" : "test.log", "ok" : 1 }