--配置config server [mongodb@rudy_01 2.6]$ mkdir 27018 [mongodb@rudy_01 2.6]$ mkdir log/27018 --mongo配置文件 dbpath=/usr/local/mongodb/2.6/27018 fork=1 logpath=/usr/local/mongodb/2.6/log/27018/mongo.log port=27018 maxConns=100 oplogSize=100 logappend=true directoryperdb=1 --启动配置服务器,配置服务器一定要先启动 mongod -f /usr/local/mongodb/2.6/27018/mongodb.conf --mongos路由配置文件 port=4000 configdb=rudy_01:27018 fork=1 logpath=/usr/local/mongodb/2.6/log/4000/mongo.log chunkSize=1 --启动mongos路由 mongos -f /usr/local/mongodb/2.6/mongos.conf --配置shard server [mongodb@rudy_01 2.6]$ mkdir 27019 [mongodb@rudy_01 2.6]$ mkdir 27020 [mongodb@rudy_01 2.6]$ mkdir 27021 [mongodb@rudy_01 2.6]$ mkdir log/27019 [mongodb@rudy_01 2.6]$ mkdir log/27020 [mongodb@rudy_01 2.6]$ mkdir log/27021 --配置保个节点的配置文件 dbpath=/usr/local/mongodb/2.6/27019 fork=1 logpath=/usr/local/mongodb/2.6/log/27019/mongo.log port=27019 maxConns=100 oplogSize=100 logappend=true directoryperdb=1 dbpath=/usr/local/mongodb/2.6/27020 fork=1 logpath=/usr/local/mongodb/2.6/log/27020/mongo.log port=27020 maxConns=100 oplogSize=100 logappend=true directoryperdb=1 dbpath=/usr/local/mongodb/2.6/27021 fork=1 logpath=/usr/local/mongodb/2.6/log/27021/mongo.log port=27021 maxConns=100 oplogSize=100 logappend=true directoryperdb=1 --启动数据库 mongod -f /usr/local/mongodb/2.6/27019/mongodb.conf mongod -f /usr/local/mongodb/2.6/27020/mongodb.conf mongod -f /usr/local/mongodb/2.6/27021/mongodb.conf --配置sharding --登陆到路由节点 mongo admin --port 4000 --添加shard节点 db.runCommand({"addshard":"rudy_01:27019"}) db.runCommand({"addshard":"rudy_01:27020"}) db.runCommand({"addshard":"rudy_01:27021"}) --设置分片集合名称 db.runCommand({"enablesharding":"test"}) db.runCommand({"shardcollection":"test.personalinfo",key:{_id:1}}) --验证sharding for (var i=1;i<=100000;i++){ db.personalinfo.insert({age:i,name:"rudy"+i,addr:"上海宝山区"+i+"路",country:"中国"}); } --查看文档状态,可知数据已经被分片处理 db.personalinfo.stats() mongos> db.personalinfo.stats() { "sharded" : true, "systemFlags" : 1, "userFlags" : 1, "ns" : "test.personalinfo", "count" : 100000, "numExtents" : 18, "size" : 11200000, "storageSize" : 33546240, "totalIndexSize" : 3270400, "indexSizes" : { "_id_" : 3270400 }, "avgObjSize" : 112, "nindexes" : 1, "nchunks" : 12, "shards" : { "shard0000" : { "ns" : "test.personalinfo", "count" : 36540, "size" : 4092480, "avgObjSize" : 112, "storageSize" : 11182080, "numExtents" : 6, "nindexes" : 1, "lastExtentSize" : 8388608, "paddingFactor" : 1, "systemFlags" : 1, "userFlags" : 1, "totalIndexSize" : 1193696, "indexSizes" : { "_id_" : 1193696 }, "ok" : 1 }, "shard0001" : { "ns" : "test.personalinfo", "count" : 31994, "size" : 3583328, "avgObjSize" : 112, "storageSize" : 11182080, "numExtents" : 6, "nindexes" : 1, "lastExtentSize" : 8388608, "paddingFactor" : 1, "systemFlags" : 0, "userFlags" : 1, "totalIndexSize" : 1046528, "indexSizes" : { "_id_" : 1046528 }, "ok" : 1 }, "shard0002" : { "ns" : "test.personalinfo", "count" : 31466, "size" : 3524192, "avgObjSize" : 112, "storageSize" : 11182080, "numExtents" : 6, "nindexes" : 1, "lastExtentSize" : 8388608, "paddingFactor" : 1, "systemFlags" : 0, "userFlags" : 1, "totalIndexSize" : 1030176, "indexSizes" : { "_id_" : 1030176 }, "ok" : 1 } }, "ok" : 1 } --列出所有的shard server db.runCommand({listshards:1}) --查看数据库文档分片的详细信息 db.printShardingStatus() --判断是否是分片 db.runCommand({isdbgrid:1}) --对未分片的集合进行分片 --构建测试数据 for (var i=1;i<=100000;i++){ db.user.insert({age:i,name:"未分片的id:"+i,country:"中国"}); } --查看文档状态 db.user.status() --对文档进行分片 use admin db.runCommand({"shardcollection":"test.user","key":{_id:1}}) --移除一个分片数据库,要多执行几次,查看数据库的进程状态,直到提示已完成 use admin db.runCommand({"removeshard":"rudy_01:27021"}) --注意最后一个移除主机上的count=0,size=0 mongos> db.user.stats() { "sharded" : true, "systemFlags" : 1, "userFlags" : 1, "ns" : "test.user", "count" : 100000, "numExtents" : 19, "size" : 11200000, "storageSize" : 44871680, "totalIndexSize" : 3981712, "indexSizes" : { "_id_" : 3981712 }, "avgObjSize" : 112, "nindexes" : 1, "nchunks" : 22, "shards" : { "shard0000" : { "ns" : "test.user", "count" : 48499, "size" : 5431888, "avgObjSize" : 112, "storageSize" : 22507520, "numExtents" : 7, "nindexes" : 1, "lastExtentSize" : 11325440, "paddingFactor" : 1, "systemFlags" : 1, "userFlags" : 1, "totalIndexSize" : 1945888, "indexSizes" : { "_id_" : 1945888 }, "ok" : 1 }, "shard0001" : { "ns" : "test.user", "count" : 51501, "size" : 5768112, "avgObjSize" : 112, "storageSize" : 11182080, "numExtents" : 6, "nindexes" : 1, "lastExtentSize" : 8388608, "paddingFactor" : 1, "systemFlags" : 0, "userFlags" : 1, "totalIndexSize" : 2027648, "indexSizes" : { "_id_" : 2027648 }, "ok" : 1 }, "shard0002" : { "ns" : "test.user", "count" : 0, "size" : 0, "storageSize" : 11182080, "numExtents" : 6, "nindexes" : 1, "lastExtentSize" : 8388608, "paddingFactor" : 1, "systemFlags" : 0, "userFlags" : 1, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1 } }, "ok" : 1 } mongos> --新增分片数据库, db.runCommand({addshard:"rudy_01:27021"}) --注意新添加的分片中不能存在已经分片的数据库,如果有要删除,否则会报错 "errmsg" : "can't add shard rudy_01:27021 because a local database 'test' exists in another shard0000:rudy_01:27019" --此时登陆到相应的主机删除数据库则可 use test db.runCommand( { dropDatabase: 1 } )