使用mongofiles操作GridFS

2014-11-24 08:11:57 · 作者: · 浏览: 3
使用mongofiles操作GridFS
GridFS描述:
GridFS,看起来像一种文件系统,其实是一种数据库用法。主要用来在数据库中存储二进制大文件。可以统一用数据库处理数据,而无需借助外部的文件系统。另外,还可以利用MongoDB的复制或者是分片机制,其故障恢复和可扩展性较好。使用这种方式存储,可以避免使用文件系统的某些限制,例如平台的差异性导致存储需要做特殊处理(Linux在同一目录下的文件数限制),还可以避免文件碎片(MongoDB分配空间以2GB作为单位),这样数据存放相对较集中,即使有文件碎片,相对来说,其程度也要比操作系统的碎片程度低得多。
操作环境:Win8 64位操作系统, 虚拟机 CentOS5.5.
操作步骤:
    (1) 启动mongoDB服务器:

[plain] 
[root@h3 ~]# mongod -f /etc/mongod.conf  
Mon Aug 12 13:07:19.737  
Mon Aug 12 13:07:19.739 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.  
Mon Aug 12 13:07:19.740  
about to fork child process, waiting until server is ready for connections.  
forked process: 22881  
all output going to: /var/log/mongo/mongod.log  
child process started successfully, parent exiting  
[root@h3 ~]#  

     (2)准备文件:
[plain] 
[root@h3 dbs]# echo 'hello mongoDB' >
> test.txt [root@h3 dbs]# cat test.txt hello mongoDB (3)将文件写入mongoDB: [plain] [root@h3 dbs]# mongofiles put test.txt connected to: 127.0.0.1 added file: { _id: ObjectId('520871172918b24487573832'), filename: "test.txt", chunkSize: 262144, uploadDate: new Date(1376284952450), md5: "f595caa8025995fab85484de67dc8549", length: 14 } done! (4) 查看文件: [plain] [root@h3 dbs]# mongofiles list connected to: 127.0.0.1 test.txt 14 (5)删除建立的test.txt文件: [plain] [root@h3 dbs]# rm -rf test.txt (6)查看数据: [plain] [root@h3 dbs]# mongofiles get test.txt connected to: 127.0.0.1 done write to: test.txt [root@h3 dbs]# cat test.txt hello mongoDB

操作完成!