mongodb 操作记录
mongod --dbpath "f:\mongodb\configs" --port 23017
mongos --port 25017 --configdb 127.0.0.1:23017 --chunkSize 5 --logpath F:\mongodb\data\mongos.log --logappend
mongos 通过追加 --chunkSize (单位是M 例如 --chunkSize 1 代表每个chunk大小为1M)
mongod --shardsvr --port 27017 --dbpath "f:\mongodb\data\shard27017" --oplogSize 100 --logpath "f:\mongodb\data\shard27017\shard27017.log" --logappend
mongod --shardsvr --port 27018 --dbpath "f:\mongodb\data\shard27018" --oplogSize 100 --logpath "f:\mongodb\data\shard27018\shard27018.log" --logappend
mongod --shardsvr --port 27019 --dbpath "f:\mongodb\data\shard27019" --oplogSize 100 --logpath "f:\mongodb\data\shard27019\shard27019.log" --logappend
mongod --shardsvr --port 27020 --dbpath "f:\mongodb\data\shard27020" --oplogSize 100 --logpath "f:\mongodb\data\shard27020\shard27020.log" --logappend
mongod --shardsvr --port 27021 --dbpath "f:\mongodb\data\shard27021" --oplogSize 100 --logpath "f:\mongodb\data\shard27021\shard27021.log" --logappend
mongod --shardsvr --port 27022 --dbpath "f:\mongodb\data\shard27022" --oplogSize 100 --logpath "f:\mongodb\data\shard27022\shard27022.log" --logappend
mongod --shardsvr --port 27023 --dbpath "f:\mongodb\data\shard27023" --oplogSize 100 --logpath "f:\mongodb\data\shard27023\shard27023.log" --logappend
mongod --shardsvr --port 27024 --dbpath "f:\mongodb\data\shard27024" --oplogSize 100 --logpath "f:\mongodb\data\shard27024\shard27024.log" --logappend
mongod --shardsvr --port 27025 --dbpath "f:\mongodb\data\shard27025" --oplogSize 100 --logpath "f:\mongodb\data\shard27025\shard27025.log" --logappend
mongod --shardsvr --port 27026 --dbpath "f:\mongodb\data\shard27026" --oplogSize 100 --logpath "f:\mongodb\data\shard27026\shard27026.log" --logappend
mongo localhost:25017/admin
db.runCommand( { addshard : "localhost:27017", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27018", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27019", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27020", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27021", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27022", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27023", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27024", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27025", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27026", allowLocal : 1, maxsize:100} )
maxSize:指定各个shard可使用的最大磁盘空间,单位megabytes
使用db.fs.chunks.stats()命令查看chunks的分块状态得知mongos优先将文件块放在了shard1上,当shard1的大小超过一定规模后(这个规模又不是maxSize设定的100M)才会将文件块迁移向Shard2。
当chunks这个collection在Shard2上面占用的空间大于100M之后(实际上是108906496字节),mongos不断提示“[Balancer] no availalable shards to take chunks”,然后我又新建了shard3同样设置为“maxsize:100”并且添加到集群中,这时mongos自动将一些块迁移到shard3中。等到停止迁移后我新增块,新增的块还是先写入shard1并且不断地有chunks迁移到shard3,但是最终shard1的数据大小远大于100M。
另外,db.fs.chunks在shard2的分布情况如下:
"shard0001" : {
"ns" : "test.fs.chunks",
"count" : 340,
"size" : 88506100,
"avgObjSize" : 260312.0588235294,
"storageSize" : 108906496,
"numExtents" : 11,
"nindexes" : 3,
"lastExtentSize" : 21645312,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 90112,
"indexSizes" : {
"_id_" : 32768,
"files_id_1" : 24576,
"files_id_1_n_1" : 32768
},
"ok" : 1
}
由状态信息可知,实际储存的文件大小是88.5M,但是占用的储存空间确是108.9M,可能chunks多余的未用部分用来做了对齐操作。
db.runCommand( { listshards : 1 } )
激活数据库分片
db.runCommand({"enablesharding":"dnt_mongodb"})
通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作
db.runCommand( { shardcollection : "dnt_mongodb.posts1", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts2", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts3", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts4", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts5", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts6", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts7", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts8", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts9", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts10", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts11", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts12", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts13", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts14", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts15", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts16", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts17", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts18", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts19", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts20", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts21", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts22", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts23", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts24", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts25", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts26", key : {_id : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts27", key : {_id : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts28", key : {_id : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts29", key : {_id : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts30", key : {_id : 1}, unique: true } )
生产环境建议使用配置文件来启动mongod
mongod --dbpath "f:\mongodb\configs" --port 23017
mongos --port 25017 --configdb 127.0.0.1:23017 --chunkSize 5 --logpath F:\mongodb\data\mongos.log --logappend
mongos 通过追加 --chunkSize (单位是M 例如 --chunkSize 1 代表每个chunk大小为1M)
mongod --shardsvr --port 27017 --dbpath "f:\mongodb\data\shard27017" --oplogSize 100 --logpath "f:\mongodb\data\shard27017\shard27017.log" --logappend
mongod --shardsvr --port 27018 --dbpath "f:\mongodb\data\shard27018" --oplogSize 100 --logpath "f:\mongodb\data\shard27018\shard27018.log" --logappend
mongod --shardsvr --port 27019 --dbpath "f:\mongodb\data\shard27019" --oplogSize 100 --logpath "f:\mongodb\data\shard27019\shard27019.log" --logappend
mongod --shardsvr --port 27020 --dbpath "f:\mongodb\data\shard27020" --oplogSize 100 --logpath "f:\mongodb\data\shard27020\shard27020.log" --logappend
mongod --shardsvr --port 27021 --dbpath "f:\mongodb\data\shard27021" --oplogSize 100 --logpath "f:\mongodb\data\shard27021\shard27021.log" --logappend
mongod --shardsvr --port 27022 --dbpath "f:\mongodb\data\shard27022" --oplogSize 100 --logpath "f:\mongodb\data\shard27022\shard27022.log" --logappend
mongod --shardsvr --port 27023 --dbpath "f:\mongodb\data\shard27023" --oplogSize 100 --logpath "f:\mongodb\data\shard27023\shard27023.log" --logappend
mongod --shardsvr --port 27024 --dbpath "f:\mongodb\data\shard27024" --oplogSize 100 --logpath "f:\mongodb\data\shard27024\shard27024.log" --logappend
mongod --shardsvr --port 27025 --dbpath "f:\mongodb\data\shard27025" --oplogSize 100 --logpath "f:\mongodb\data\shard27025\shard27025.log" --logappend
mongod --shardsvr --port 27026 --dbpath "f:\mongodb\data\shard27026" --oplogSize 100 --logpath "f:\mongodb\data\shard27026\shard27026.log" --logappend
mongo localhost:25017/admin
db.runCommand( { addshard : "localhost:27017", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27018", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27019", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27020", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27021", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27022", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27023", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27024", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27025", allowLocal : 1, maxsize:100} )
db.runCommand( { addshard : "localhost:27026", allowLocal : 1, maxsize:100} )
maxSize:指定各个shard可使用的最大磁盘空间,单位megabytes
使用db.fs.chunks.stats()命令查看chunks的分块状态得知mongos优先将文件块放在了shard1上,当shard1的大小超过一定规模后(这个规模又不是maxSize设定的100M)才会将文件块迁移向Shard2。
当chunks这个collection在Shard2上面占用的空间大于100M之后(实际上是108906496字节),mongos不断提示“[Balancer] no availalable shards to take chunks”,然后我又新建了shard3同样设置为“maxsize:100”并且添加到集群中,这时mongos自动将一些块迁移到shard3中。等到停止迁移后我新增块,新增的块还是先写入shard1并且不断地有chunks迁移到shard3,但是最终shard1的数据大小远大于100M。
另外,db.fs.chunks在shard2的分布情况如下:
"shard0001" : {
"ns" : "test.fs.chunks",
"count" : 340,
"size" : 88506100,
"avgObjSize" : 260312.0588235294,
"storageSize" : 108906496,
"numExtents" : 11,
"nindexes" : 3,
"lastExtentSize" : 21645312,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 90112,
"indexSizes" : {
"_id_" : 32768,
"files_id_1" : 24576,
"files_id_1_n_1" : 32768
},
"ok" : 1
}
由状态信息可知,实际储存的文件大小是88.5M,但是占用的储存空间确是108.9M,可能chunks多余的未用部分用来做了对齐操作。
db.runCommand( { listshards : 1 } )
激活数据库分片
db.runCommand({"enablesharding":"dnt_mongodb"})
通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作
db.runCommand( { shardcollection : "dnt_mongodb.posts1", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts2", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts3", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts4", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts5", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts6", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts7", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts8", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts9", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts10", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts11", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts12", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts13", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts14", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts15", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts16", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts17", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts18", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts19", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts20", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts21", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts22", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts23", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts24", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts25", key : {url : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts26", key : {_id : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts27", key : {_id : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts28", key : {_id : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts29", key : {_id : 1}, unique: true } )
db.runCommand( { shardcollection : "dnt_mongodb.posts30", key : {_id : 1}, unique: true } )
生产环境建议使用配置文件来启动mongod