MongoDB gridfs nginx File Server Installation configuration

Source: Internet
Author: User
Tags findone fpm install php install mongodb mongodb git clone


Gridfs is a file specification that stores large files in MongoDB. All officially supported drivers implement the GRIDFS specification. Take a quick look at the official instructions:
When to use Gridfs
In MongoDB, use Gridfs for storing files larger than MB.
In some situations, storing large the files may is more efficient in a MongoDB the database than on a system-level filesystem.
Play Gridfs just found that this thing, can not be counted as the traditional sense of the step-by-step file server. Its data is stored in MongoDB, large files are divided into small chunks of storage, distributed dependent MongoDB.

Fastdfs is more appropriate for storing small files, such as pictures.
For large files, such as: small video, a larger document, such as Gridfs or more appropriate.
One, install mongodb,gridfs,php extension

[Root@localhost ~]# yum install Php-pecl-mongo MongoDB mongodb-devel mongodb-server
Second, start MongoDB, restart PHP-FPM

[Root@localhost ~]#/etc/init.d/mongod start
[Root@localhost ~]#/etc/init.d/php-fpm Restart

Three, common command line operation


[root@localhost ~]# mongofiles list//upload
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//Find files containing 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//Find files that start with 111
Connected to:127.0.0.1
111.jpg 74843
111.jpg 74843
111.jpg 74843

[Root@localhost ~]# Mongofiles put 111.jpg//upload
Connected to:127.0.0.1
Added file: {_id:objectid (' 57a05aac1936d393ecd3e20e '), FileName: "111.jpg", chunksize:261120, Uploaddate:new Date (147 0126764728), MD5: "67131c5063a3397efe2c2d552f7f4528", length:74843}
done!

[Root@localhost ~]# Mongofiles get 111.jpg//download
Connected to:127.0.0.1
Done Write To:111.jpg

Four, PHP instance

1, Upload


<?php

$mongo = new MONGO ();
$db = $mongo->selectdb (' Test ');
$grid = $db->getgridfs ();
$id = $grid->storefile ("111.jpg"); Note File path
echo $id;

2, download


<?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, single Lookup


<?php

$mongo = new MONGO ();
$db = $mongo->selectdb (' Test ');
$grid = $db->getgridfs ();
$id = ' 579f183a33a447cd1d8b4568 ';
$file = $grid->findone (Array (' _id ' =>new mongoid ($id)); Three results.
$file = $grid->findone (Array (' filename ' => ' 1.txt ')); Three results.
$file = $grid->findone (' 1.txt '); Three results.
Print_r ($file);

4, multiple Lookup


<?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;
}

Five, Nginx-gridfs plug-in installation

Above I said, Gridfs is not the traditional sense of the Distributed File server, upload files, you can not find where. The root path is specified whenever Nginx and Apache set up the virtual machine. Now, there is no path, to enable Nginx to access the picture, to install Nginx-gridfs plug-ins.
1,nginx 1.4.7 Download


# wget http://nginx.org/download/nginx-1.4.7.tar.gz
2,nginx1.4.7 Installation Method


# 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//delete-werror, or make && makes install will complain
# Make && make install

3, configure nginx1.4.7

location/images/{
Gridfs test Field=filename type=string;
MONGO 127.0.0.1:27017;
}
Keywords for gridfs:nginx recognition plug-ins
Test:db Name
[Root_collection]: Choose collection, such as Root_collection=blog, Mongod will go to find blog.files and blog.chunks two blocks, the default is FS
[Field]: Query field, ensure that the field name in Mongdb, support _id, filename, can be omitted, the default is _id
[Type]: explains the data type of field, supports Objectid, int, string, can omit, the default is int
[User]: username, can be omitted
[Pass]: password, can be omitted
Mongo:ip:port

Once the nginx is restarted, it can be accessed through the URL. Yun_qi_img/111.jpg

4, installation of nginx1.4.7, and Gridfs Plug-ins encountered problems

4.1, install the error

Cc1:warnings being treated as errors
/ROOT/NGINX-GRIDFS/MONGO-C-DRIVER/SRC/BSON.C: In the function ' Bson_ensure_space ':
/root/nginx-gridfs/mongo-c-driver/src/bson.c:632: Error: Comparing between signed and unsigned integer expressions
MAKE[1]: * * * [OBJS/ADDON/SRC/BSON.O] Error 1
MAKE[1]: Leaving directory '/root/nginx-1.4.7 '
Make: * * * [build] Error 2

Solution:

Vim Objs/makefile, delete-werror
4.2,nginx1.4.7 cannot display file, nginx error log error
Mongo connection dropped, could not reconnect

Solution:

Mongo-c-driver direct git clone download, git clone https://github.com/eagleas/mongo-c-driver.git
Instead of doing the following two steps

Git submodule init
git submodule update

5,nginx1.2.9, and Gridfs Plug-ins encountered problems
nginx1.2.9 installation Root nginx1.4.7 installation is two different.


# git clone https://github.com/eagleas/mongo-c-driver.git
Replace into
# git submodule init
# git submodule update
There is no need to edit the Objs/makefile file, the installation process and the Nginx log will not error. Recommend that you use nginx1.2.9

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.