GridFS
GridFS是一种将大型文件存储在MongoDB的文件规范。 GridFS 规范提供了一种透明的机制,可以将一个大文件分割成为多个较小的文档。这将容许我们有效的保存大的文件对象,特别对于那些巨大的文件,比如视频。
用于在数据库里存储二进制大文件
可以统一用数据库处理数据,而无需借助外部的文件系统
可以利用MongoDB的复制或分片机制,故障恢复和可扩展性较好
避免使用文件系统的某些限制(例如linux在同一目录下的文件数限制)
避免文件碎片(MongoDB分配空间以2GB作为单位)
#使用mongofiles操作GridFS
[root@linux bin]# echo "Hello, word" > foo.txt
[root@linux bin]# cat foo.txt
Hello, word
[root@linux bin]# ./mongofiles put foo.txt
connected to: 127.0.0.1
added file: { _id: ObjectId('52baeba606b420c225b78a09'), filename: "foo.txt", chunkSize: 262144, uploadDate: new Date(1387981734453), md5: "60b8a03005777c37107ba3babd27739d", length: 12 }
done!
[root@linux bin]# ./mongofiles list
connected to: 127.0.0.1
foo.txt 12
[root@linux bin]# rm -rf foo.txt
[root@linux bin]# ./mongofiles get foo.txt
connected to: 127.0.0.1
done write to: foo.txt
[root@linux bin]# cat foo.txt
Hello, word
#GridFS原理
文件被分成若干块(chunk),每个块作为一个文档存储
有一个单独的文档存储分块的信息,以及文件的元数据
fs.chunks集合
fs.files集合
分片
#何时分片
单个节点的磁盘不足
单个mongod不能满足写数据的性能要求
将大量数据放到内存中提高性能
#片键
什么是片键 (就是文档的属性,我们通常说的列)
选择递增片键还是随机片键 (当然是随机的好,跟B*tree索引倾斜一样的道理)
片键对操作和性能的影响
##实施分片
1、启动配置服务器
./mongod --dbpath /nosql/mongodb/dbs/config --port 20000
2、启动mongos
./mongos --port 30000 --configdb 127.0.0.1:20000
3、添加mongod实例(片)
./mongod --dbpath /nosql/mongodb/dbs/shard1 --port 10000
#使用连接mongos
./mongo 127.0.0.1:30000/admin
mongos> db.runCommand({addshard:"127.0.0.1:10000",allowLocal:true})
{ "shardAdded" : "shard0000", "ok" : 1 }
4、对数据库启用分片
mongos> db.runCommand({"enablesharding":"foo"})
{ "ok" : 1 }
##对foo数据库启用分片
5、对集合进行分片
mongos> db.runCommand({"shardcollection":"foo.bar","key":{"_id":1}})
{ "collectionsharded" : "foo.bar", "ok" : 1 }
##foo是数据库 bar是集合 _id是片键
#健壮的集群规划
可以多配置服务器
可以启动多个mongos
可以每个片都是副本集
#查看所有的片
mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "shard0000", "host" : "127.0.0.1:10000" }
#database集合
mongos> db.databases.find()
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "foo", "partitioned" : true, "primary" : "shard0000" }
#chunks集合
mongos> db.chunks.find()
{ "_id" : "foo.bar-_id_MinKey", "lastmod" : { "t" : 1000, "i" : 0 }, "ns" : "foo.bar", "min" : { "_id" : { $minKey : 1 } }, "max" : { "_id" : { $maxKey : 1 } }, "shard" : "shard0000" }
#获得概要
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "127.0.0.1:10000" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "foo", "partitioned" : true, "primary" : "shard0000" }
foo.bar chunks:
shard0000 1
{ "_id" : { $minKey : 1 } } -->> { "_id" : { $maxKey : 1 } } on : shard0000 { "t" : 1000, "i" : 0 }
#删除片
mongos> use admin
switched to db admin
mongos> db.runCommand({"removeshard":"127.0.0.1:10000"})
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard0000",
"ok" : 1
}
mongos> db.runCommand({"removeshard":"127.0.0.1:10000"})
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(1),
"dbs" : NumberLong(1)
},
"ok" : 1
}