GridFS是一種在MongoDB中儲存大二進位檔案的機制。使用GridFS存檔案有如下幾個原因:
● GridFS可以簡化需求。如果已經用了MongoDB,GridFS就可以不需要獨立的檔案儲存體架構。
● GridFS利用已經建立的複製和分區機制,所以對於檔案儲存體來說故障恢複和擴充都很容易。
● GridFS可以避免用於儲存使用者上傳內容的檔案系統出現的某些問題。例如:GridFS在同一目錄下放置大量檔案是沒有任何問題的。
● GridFS不產生磁片,因為MongoDB分配的資料檔案空間以2G為一塊。
使用GridFS:mongofiles
mongofiles是GridFS的工具 + 生產力,用於管理GridFS檔案
--協助命令
[root@racdb ~]# mongofiles--help
Browse and modify a GridFSfilesystem.
usage: mongofiles [options]command [gridfs filename]
command:
one of (list|search|put|get)
list - list all files. 'gridfs filename' is an optional prefix
which listed filenames must beginwith.
search - search all files. 'gridfs filename'is a substring
which listed filenames must contain.
put - add a file with filename 'gridfsfilename'
get - get a file with filename 'gridfsfilename'
delete - delete all files with filename'gridfs filename'
options:
--help produce helpmessage
-v [ --verbose ] be more verbose (includemultiple times
formore verbosity e.g. -vvvvv)
--version print theprogram's version and exit
-h [ --host ] arg mongo host to connect to (<set
name>/s1,s2 for sets)
--port arg server port. Can also use --host
hostname:port
--ipv6 enable IPv6support (disabled by
default)
-u [ --username ] arg username
-p [ --password ] arg password
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg (=MONGODB-CR)
authentication mechanism
--dbpath arg directly accessmongod database files
in thegiven path, instead of
connecting to a mongod server -needs
to lockthe data directory, so cannot
be usedif a mongod is currently
accessing the same path
--directoryperdb each db is in a separate directly
(relevant only if dbpath specified)
--journal enable journaling(relevant only if
dbpathspecified)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (somecommands)
-l [ --local ] arg local filename for put|get(default is
to usethe same name as 'gridfs
filename')
-t [ --type ] arg MIME type for put (defaultis to omit)
-r [ --replace ] Remove other files withsame name after
PUT
--上傳檔案
[root@racdb ~]# mongofiles put foo.log
connected to: 127.0.0.1
added file: { _id:ObjectId('56caba480ad7ef0aa8a76f0c'), filename: "foo.log", chunkSize:261120, uploadDate: new Date(1456126536618), md5:"d1bfff5ab0cc6b652aaf08345b19b7e6", length: 21 }
done!
--列出檔案
[root@racdb ~]# mongofiles list
connected to: 127.0.0.1
install.log 54876
foo.log 21
--下載檔案
[root@racdb ~]# rm -f foo.log
[root@racdb ~]# mongofiles get foo.log
connected to: 127.0.0.1
done write to: foo.log
[root@racdb ~]# ll foo.log
-rw-r--r--. 1 root root 21 2月 22 15:36 foo.log