MongoDB Gridfs Basic Usage

Source: Internet
Author: User
Tags install mongodb openssl

Mongodb Gridfs picture File storage Solution

Before the solution is to receive image data, the image is stored directly to the disk array, and then through Apache server, the picture information stored in the database, and storage of an Apache access path.

Currently, background services are required to store images, store images in a MongoDB cluster, and then access them via the Nginx-gridfs module in the browser, with the same effect as Apache access to local files.

The content of this program is relatively basic, will have in-depth understanding and optimization, please look forward to!

First, install MongoDB

(1) Installing MongoDB

Go to the Scheduled installation directory: cd/usr/local/

Download Mongodb-linux-x86_64-2.6.1.tgz https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.1.tgz

Decompression: TAR-ZXF mongodb-linux-x86_64-2.6.1.tgz

Renamed: MV mongodb-linux-x86_64-2.6.1 mongodb2.6.1

Create data directory and log directory: Mkdir-p mongodb2.6.1/mongodb_data/mongodb_db

Mkdir-p Mongodb2.6.1/mongodb_data/mongodb_logs

Start MongoDB: Enter the MongoDB bin directory to execute the following command:

Cd/usr/local/mongodb2.6.1/bin

./mongod--dbpath=/usr/local/mongodb2.6.1/mongodb_data/mongodb_db/--logpath=/usr/local/mongodb2.6.1/mongodb_ Data/mongodb_log/mongodb.log--logappend &

Check the startup status:

Ps-ef | grep MONGO see just the start of the command that is running the

by Netstat-ant | grep 27017 can also view the port static state

?
12345678910 [root@zjhl1opt]# ps -ef | grep mongoroot     1635010593115:40pts/1 00:01:11./mongod --dbpath=/opt/mongodb2.6.1/mongodb_data/mongodb_db/ --logpath=/opt/mongodb2.6.1/mongodb_data/mongodb_logs/mongodb.log --logappendroot     1902710593017:21pts/100:00:00grep mongo[root@zjhl1opt]# [root@zjhl1opt]# netstat -ant | grep 27017tcp        000.0.0.0:270170.0.0.0:*                   LISTEN      tcp        00127.0.0.1:27017127.0.0.1:47686ESTABLISHED tcp        00127.0.0.1:47686127.0.0.1:27017ESTABLISHED tcp        00192.168.1.111:27017192.168.1.100:53462ESTABLISHED [root@zjhl1opt]#

(2) Installing the Mongovue client

Mongovue is a graphical client similar to PL/SQL Developer in Oracle or SQLyog in MySQL.

Currently is charged, after download Installer.msi directly installed on the line. As to the issue of authorization Baidu to solve.

This tool has a lot of functions, here do not expand to speak, there is a need for their own Baidu.

Second, install Nginx

(1) Download Nginx-gridfs plugin

On the internet there is an outgoing use of git to install this plugin, this is more troublesome, I left a download package in the attachment of the article. I am testing available.

Download and download the MONGO C driver package, which can also be downloaded in the attachment.

After the download is complete, unzip: TAR-ZXF nginx-gridfs.tar.gz

TAR-ZXF mongo-c-driver-0.94.2.tar.gz

After the decompression is complete, copy the driver package contents to the Mongo-c-driver directory in the Nginx-gridfs directory:

MV mongo-c-driver-0.94.2/* nginx-gridfs/mongo-c-driver/

If there is a hint already exists in the SRC directory, no relationship is ignored. Then is the download Nginx, compile the installation process.

(6) Download Nginx1.0.1

Nginx version is too high to support the Nginx-gridfs module is not very successful, I am using the Nginx1.7 to get a day out of the picture. It would be nice to change into 1.0.1. Not very familiar with C not to delve into. There's the danale who knows to tell me.

wget Http://nginx.org/download/nginx-1.0.1.zip

Decompression: Unzip Nginx-1.0.1.zip

Configure compile-time settings:./configure--prefix=/usr/local/nginx--with-openssl=/usr/include/openssl--with-http_stub_status_module- -add-module=/opt/nginx-gridfs

Compiling: Make

Install: Make install

Configure MOGON-GRIDFS Address:

In the vim/usr/local/nginx/conf/nginx.conf configuration file, add the following:

location/pics/{
Gridfs Pics
Field=filename
type=string;
MONGO 127.0.0.1:27017;
}

Gridfs:nginx identify the name of the plugin

PICS: Database name

[Root_collection]: Select collection, such as Root_collection=blog, Mongod will find Blog.files and blog.chunks two blocks, the default is FS

[Field]: Query field, guaranteed Mongdb has this field name, support _id, filename, can be omitted, default is _id

[Type]: interpreted field data type, support Objectid, int, string, can be omitted, default is int

[User]: username, can be omitted

[Pass]: password, can be omitted

Mongo:mongodb URL MONGO name address: port

?
12345678910111213         #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }        location /pics/ {                gridfs pics                field=filename                type=string;                mongo 127.0.0.1:27017;        }        #error_page  404/404.html;        # redirect server error pages to the staticpage /50x.html

Start:/usr/local/nginx/sbin/nginx &

View log: Tail-f/usr/local/nginx/logs/error.log

Check: In Browser input: http://192.168.1.111/

When you see the following letter, the Nginx boot succeeds.

Welcome to nginx!

Third, test development

Test examples using Java development, the code in the attachment

(1) Picture writing

All of the examples in the attachment, Gridfstest.rar, and the example also require MongoDB's driver jar package Mongo-java-driver-2.9.3.jar.

?
12345678                 try{   Mongo mongo = newMongo("192.168.100.52"27017);// 创建连接   DB db = mongo.getDB("pics"); // 选择数据库   byte[] files = createImage(800600"800 X 600"); // 创建图片   save(files, "test3.jpg", db); // 存储图片  catch(Exception e) {   e.printStackTrace();  }

(2) Document acquisition

Get the picture through the code is not written. Write only how to pass the file path in the program, and then show it on the front.

When storing picture information, you can add the IP address: http://192.168.100.52/pics/test3.jpg, and then the front end app can get the picture content with the given URL address.

The same effect can be obtained by entering an address in the browser.

MongoDB Gridfs Basic Usage

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.