MongoDB GridFS
@camelmasa
GridFSGridFS - -ReplicaSet,ShardingmongofilesGridFS
@camelmasa(86)Livlis (kamado, inc.)(paul bassett etc.)
LivlisLithium + MongoDB ()
GridFS
MongoDBMongoDB BSON MongoDBBSON4MB
GridFS
GridFS http://www.mongodb.org/display/DOCSJP/GridFS
GridFS
PHP, Java, Python, Ruby, Perl,earlang(github)
GridFS
Amazon EC2 micro Instance Basic 64-bit Amazon Linux AMI 2011.02.1 Beta(1 core CPU, 613MB)Large
MongosMongod(config)
MongodMongodMongod
ReplicaSet
MongodMongodMongodReplicaSet
Sharding1
Sharding2
Config
[/mongo/mongo-config.conf]configsvr=truedbpath=/mongo/db/configlogpath=/mongo/log/config/mongo.loglogappend=truefork=true
[]mongod -f /mongo/mongo-config.conf
Mongos
[/mongo/mongos.conf]logpath=/mongo/log/mongos/mongos.logconfigdb=localhost:27019port=27021fork=true
[]mongos -f /mongo/mongos.conf
ReplicaSet
[/mongo/mongo-s1r1.conf]dbpath=/mongo/db/s1_r1port=28201logpath=/mongo/log/s1_r1.loglogappend=truerest=truereplSet=s1journal=truefork=true
[]mongo -f /mongo/mongo-s1r1.conf
ReplicaSet
[]config = {_id: 's1', members: [{_id: 0, host: 'localhost:28201'},{_id: 1, host: 'localhost:28202'},{_id: 2, host: 'localhost:28203'}]}rs.initiate(config);
Sharding
[/mongo/mongo-s1r1.conf]dbpath=/mongo/db/s1_r1port=28201logpath=/mongo/log/s1_r1.loglogappend=truerest=truereplSet=s1journal=truefork=true
[]mongo -f /mongo/mongo-s1r1.conf
Sharding
[]db.runCommand( { enablesharding : "test" });db.fs.chunks.ensureIndex({'files_id':1});db.runCommand( { shardcollection : "test.fs.chunks", key : { files_id : 1 } } )
ReplicaSet
[mongos]mongofiles --host localhost:27021 put replica.set connected to: localhost:27021added file: { _id: ObjectId('4e3368d3c629125977fed2b7'), filename: "replica.set", chunkSize: 262144, uploadDate: new Date(1311992019116), md5: "d41d8cd98f00b204e9800998ecf8427e", length: 0 }Done!
ReplicaSet,Sharding
ReplicaSet
[mongos]> db.fs.files.find({'filename':'replica.set'}){ "_id" : ObjectId("4e3368d3c629125977fed2b7"), "filename" : "replica.set", "chunkSize" : 262144, "uploadDate" : ISODate("2011-07-30T02:13:39.116Z"), "md5" : "d41d8cd98f00b204e9800998ecf8427e", "length" : 0 }
[sharding1master]> db.fs.files.find({'filename':'replica.set'}){ "_id" : ObjectId("4e3368d3c629125977fed2b7"), "filename" : "replica.set", "chunkSize" : 262144, "uploadDate" : ISODate("2011-07-30T02:13:39.116Z"), "md5" : "d41d8cd98f00b204e9800998ecf8427e", "length" : 0 }
[sharding1slave1]> db.fs.files.find({'filename':'replica.set'}){ "_id" : ObjectId("4e3368d3c629125977fed2b7"), "filename" : "replica.set", "chunkSize" : 262144, "uploadDate" : ISODate("2011-07-30T02:13:39.116Z"), "md5" : "d41d8cd98f00b204e9800998ecf8427e", "length" : 0 }
[sharding1slave2]> db.fs.files.find({'filename':'replica.set'}){ "_id" : ObjectId("4e3368d3c629125977fed2b7"), "filename" : "replica.set", "chunkSize" : 262144, "uploadDate" : ISODate("2011-07-30T02:13:39.116Z"), "md5" : "d41d8cd98f00b204e9800998ecf8427e", "length" : 0 }
ReplicaSet,Sharding
ReplicaSet
[sharding2master]> db.fs.files.find({'filename':'replica.set'})
[sharding2slave1]> db.fs.files.find({'filename':'replica.set'})
[sharding2slave2]> db.fs.files.find({'filename':'replica.set'})
ReplicaSet,Sharding
Sharding
> db.printShardingStatus() --- Sharding Status --- sharding version: { "_id" : 1, "version" : 3 } shards: { "_id" : "s1", "host" : "s1/localhost:28201,localhost:28202,localhost:28203"} { "_id" : "s2", "host" : "s2/localhost:28204,localhost:28205,localhost:28206"} databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "test", "partitioned" : true, "primary" : "s2" } test.fs.chunks chunks: s1 10 s2 10 too many chunksn to print, use verbose if you want to force print { "_id" : "fs", "partitioned" : false, "primary" : "s2" }
ReplicaSet,Sharding
Sharding
> db.printShardingStatus() --- Sharding Status --- sharding version: { "_id" : 1, "version" : 3 } shards: { "_id" : "s1", "host" : "s1/localhost:28201,localhost:28202,localhost:28203"} { "_id" : "s2", "host" : "s2/localhost:28204,localhost:28205,localhost:28206"} databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "test", "partitioned" : true, "primary" : "s2" } test.fs.chunks chunks: s1 10 s2 10 too many chunksn to print, use verbose if you want to force print { "_id" : "fs", "partitioned" : false, "primary" : "s2" }
ReplicaSet,Sharding
[PUT]
mongofiles --host localhost:27021 put a.txt connected to: localhost:27021added file: { _id: ObjectId('4e3349480eed7346d277d523'), filename: "a.txt", chunkSize: 262144, uploadDate: new Date(1311983944316), md5: "d41d8cd98f00b204e9800998ecf8427e", length: 0 }Done!
mongofiles
[PUT]
mongofiles --host localhost:27021 -r put a.txt connected to: localhost:27021added file: { _id: ObjectId('4e3349480eed7346d277d523'), filename: "a.txt", chunkSize: 262144, uploadDate: new Date(1311983944316), md5: "d41d8cd98f00b204e9800998ecf8427e", length: 0 }done!
mongofiles
[GET]
mongofiles --host localhost:27021 get a.txt connected to: localhost:27021done write to: a.txt
mongofiles
[LIST]
mongofiles --host localhost:27021 lista.txt 1000
(,byte)
mongofiles
[SEARCH]
mongofiles --host localhost:27021 search a.txtconnected to: localhost:27021a.txt 0
mongofiles --host localhost:27021 search txtconnected to: localhost:27021a.txt 0
->
mongofiles
[DELETE]
mongofiles --host localhost:27021 delete a.txtconnected to: localhost:27021Done!
mongofiles
ReplicaSetEC2root device8GBReplicaSet8GBvim.swp
100GBVolumesmount->100G48G
Shardingsharding1,sharding2sharding2sharding
ReplicaSetmongofies
mongofiles host [master server] search test.txt
mongofiles host [slave server] search test.txt
db.test.fs.files.find({'filename':'test.txt'})master,slave(db.getMongo().setSlaveOk())
nginx-gridfsnginxwebgridfs
[github]https://github.com/mdirolf/nginx-gridfs
[]http://mybikejp.blogspot.com/2011/01/nginxgridfs.html
GridFS!
rack-gridfsnginx-gridfsrack
[github]https://github.com/skinandbones/rack-gridfs
GridFS!
GridFSReplicaSet,Sharding(GridFS)()
web
Recommended