gridfs是一种将大型文件存储在MongoDB的文件规范。所有官方支持的驱动均实现了GridFS规范。简单看一下官方说明: When to Use GridFS In MongoDB, use GridFS for storing files larger than 16 MB. In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem. 玩了玩gridfs才发现,这玩意,不能算做是传统意义上的分步式文件服务器。它的数据存储在mongodb中,大文件会被分成小块存储,分布式依赖mongodb。
对于存储小文件,例如:图片之类的,fastdfs比较合适的。 对于大一些的文件,例如:小视频,大一点文档等gridfs还是比较合适的。 一,安装mongodb,gridfs,php扩展 [root@localhost ~]# yum install php-pecl-mongo mongodb mongodb-devel mongodb-server 二,启动mongodb,重启php-fpm [root@localhost ~]# /etc/init.d/mongod start [root@localhost ~]# /etc/init.d/php-fpm restart 三,常用命令行下操作 [root@localhost ~]# mongofiles list //上传列表 connected to: 127.0.0.1 1.txt 342 1.txt 342 /root/1.txt 342 /root/1.txt 342 /root/1.txt 342 111.jpg 74843 111.jpg 74843 111.jpg 74843 [root@localhost ~]# mongofiles search txt //查找含有txt的文件 connected to: 127.0.0.1 /root/1.txt 342 /root/1.txt 342 /root/1.txt 342 1.txt 342 1.txt 342 [root@localhost ~]# mongofiles list 111 //查找以111开头的文件 connected to: 127.0.0.1 111.jpg 74843 111.jpg 74843 111.jpg 74843 [root@localhost ~]# mongofiles put 111.jpg //上传 connected to: 127.0.0.1 added file: { _id: ObjectId('57a05aac1936d393ecd3e20e'), filename: "111.jpg", chunkSize: 261120, uploadDate: new Date(1470126764728), md5: "67131c5063a3397efe2c2d552f7f4528", length: 74843 } done! [root@localhost ~]# mongofiles get 111.jpg //下载 connected to: 127.0.0.1 done write to: 111.jpg
四,php实例 1,上传 <?php $mongo = new Mongo(); $db = $mongo->selectDB('test'); $grid = $db->getGridFS(); $id = $grid->storeFile("111.jpg"); //注意文件路径 echo $id;
2,下载 <?php $mongo = new Mongo(); $db = $mongo->selectDB('test'); $grid = $db->getGridFS(); $file = $grid->findOne(array('filename'=>'111.jpg')); header('Content-type: image/png'); echo $file->getBytes(); exit;
3,单个查找 <?php $mongo = new Mongo(); $db = $mongo->selectDB('test'); $grid = $db->getGridFS(); $id = '579f183a33a447cd1d8b4568'; //$file = $grid->findOne(array('_id'=>new MongoId($id))); //三个结果一样 $file = $grid->findOne(array('filename'=>'1.txt')); //三个结果一样 //$file = $grid->findOne('1.txt'); //三个结果一样 print_r($file);
4,多个查找 <?php $mongo = new Mongo(); $db = $mongo->selectDB('test'); $grid = $db->getGridFS(); $cursor = $grid->find(array('filename'=>'/root/1.txt')); foreach ($cursor as $obj) { $filename = $obj->getFilename().'<br/>'; echo $filename; }
五,nginx-gridfs插件安装 上面我说了,gridfs并不是传统意义上的分布式文件服务器,上传的文件,具体你也找不到在什么地方。不管nginx和apache设置虚拟机的时候,都是要指定root路径的。现在呢,没路径,要使nginx能访问图片,要装nginx-gridfs插件了。 1,nginx 1.4.7下载 # wget http://nginx.org/download/nginx-1.4.7.tar.gz 2,nginx1.4.7 安装方法
# git clone git://github.com/mdirolf/nginx-gridfs.git # cd nginx-gridfs # git clone https://github.com/eagleas/mongo-c-driver.git # mkdir /usr/local/nginx # tar zxvf nginx-1.4.7.tar.gz # cd nginx-1.4.7 # ./configure --prefix=/usr/local/nginx --add-module=/root/nginx-gridfs # vim objs/Makefile //删除-Werror,不然make && make install的时候会报错 # make && make install
3,配置nginx1.4.7 location /images/{ gridfs test field=filename type=string; mongo 127.0.0.1:27017; } gridfs:nginx识别插件的关键字 test:db名 [root_collection]: 选择collection,如root_collection=blog, mongod就会去找blog.files与blog.chunks两个块,默认是fs [field]: 查询字段,保证mongdb里有这个字段名,支持_id, filename, 可省略, 默认是_id [type]: 解释field的数据类型,支持objectid, int, string, 可省略, 默认是int [user]: 用户名, 可省略 [pass]: 密码, 可省略 mongo: ip:port 重启nginx后,就可以能过url进行访问了。http://192.168.10.208/images/111.jpg 4,安装nginx1.4.7,及gridfs插件遇到的问题 4.1,安装报错 cc1: warnings being treated as errors /root/nginx-gridfs/mongo-c-driver/src/bson.c: 在函数‘bson_ensure_space’中: /root/nginx-gridfs/mongo-c-driver/src/bson.c:632: 错误:在有符号和无符号整数表达式间比较 make[1]: *** [objs/addon/src/bson.o] 错误 1 make[1]: Leaving directory `/root/nginx-1.4.7' make: *** [build] 错误 2 解决办法: vim objs/Makefile ,删除-Werror 4.2,nginx1.4.7无法显示文件,nginx error log报错 Mongo connection dropped, could not reconnect 解决办法: mongo-c-driver直接git clone下载,git clone https://github.com/eagleas/mongo-c-driver.git 而不要进行以下二步操作 git submodule init git submodule update 5,nginx1.2.9,及gridfs插件遇到的问题 nginx1.2.9安装根nginx1.4.7安装有二点不同就是 # git clone https://github.com/eagleas/mongo-c-driver.git 替换成 # git submodule init # git submodule update 还有就是不用编辑objs/Makefile文件,安装过程以及nginx的log不会报错。推荐大家使用nginx1.2.9
|