Mongodb分片主要包括
(1)shard server /分片,可多个
(2)config server /配置服务器
(3)router server /mongos
创建顺序无所谓,我用两台机器搭建了环境,仅分片不包含复制集,给出配置
192.168.10.82:27510 shard
192.168.10.41:25510 config
192.168.10.41:26510 mongos
192.168.10.41:27510 shard
192.168.10.41
mongod --dbpath C:\data\config --logpath C:\log\mongodb.log --port 25510 --directoryperdb --logappend
mongos --port 26510 --configdb 192.168.10.41:25510
mongod --dbpath C:\data\shard --logpath C:\log\config.log --port 27510 --directoryperdb --logappend
config 和shard在同一台主机,要指定不同的文件夹名,否则无法启动
192.168.10.82
mongod --dbpath C:\data\shard --logpath C:\log\mongodb.log --port 25510 --directoryperdb --logappend
在shard分片上建立片键,如果collection已经存在数据,指定的片键上必须存在索引。如果没有数据,那么在插入数据的时候,会自动创建该索引。
添加分片
连接mongos,添加分片
mongo 192.168.10.41:26510
use admin
sh.addShared("192.168.10.41:27510")
sh.addShared("192.168.10.82:27510")
使用sh.status()查看分片结果
在shard分片上建立散列片键
首先建立散列索引
db.collection.ensureIndex({"field":"hashed"})
collection 和field 换成对应的表名和字段名。
允许集合分片
sh.enableSharding(dbname)
对集合分片
sh.shardCollection(fullName,key,unique)
如:sh.shardCollection("app.users",{"username":"hashed"})
使用sh.status()查看分片结果
目前,暂时没有查到关于删除片键的方法,所以建立的时候请注意
给出最终截图
(1)shard server /分片,可多个
(2)config server /配置服务器
(3)router server /mongos
创建顺序无所谓,我用两台机器搭建了环境,仅分片不包含复制集,给出配置
192.168.10.82:27510 shard
192.168.10.41:25510 config
192.168.10.41:26510 mongos
192.168.10.41:27510 shard
192.168.10.41
mongod --dbpath C:\data\config --logpath C:\log\mongodb.log --port 25510 --directoryperdb --logappend
mongos --port 26510 --configdb 192.168.10.41:25510
mongod --dbpath C:\data\shard --logpath C:\log\config.log --port 27510 --directoryperdb --logappend
config 和shard在同一台主机,要指定不同的文件夹名,否则无法启动
192.168.10.82
mongod --dbpath C:\data\shard --logpath C:\log\mongodb.log --port 25510 --directoryperdb --logappend
在shard分片上建立片键,如果collection已经存在数据,指定的片键上必须存在索引。如果没有数据,那么在插入数据的时候,会自动创建该索引。
添加分片
连接mongos,添加分片
mongo 192.168.10.41:26510
use admin
sh.addShared("192.168.10.41:27510")
sh.addShared("192.168.10.82:27510")
使用sh.status()查看分片结果
在shard分片上建立散列片键
首先建立散列索引
db.collection.ensureIndex({"field":"hashed"})
collection 和field 换成对应的表名和字段名。
允许集合分片
sh.enableSharding(dbname)
对集合分片
sh.shardCollection(fullName,key,unique)
如:sh.shardCollection("app.users",{"username":"hashed"})
使用sh.status()查看分片结果
目前,暂时没有查到关于删除片键的方法,所以建立的时候请注意
给出最终截图
