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
@zjhl1
opt]# ps -ef | grep mongo
root
16350
10593
1
15
:
40
pts/
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 --logappend
root
19027
10593
0
17
:
21
pts/
1
00
:
00
:
00
grep mongo
[root
@zjhl1
opt]#
[root
@zjhl1
opt]# netstat -ant | grep
27017
tcp
0
0
0.0
.
0.0
:
27017
0.0
.
0.0
:* LISTEN
tcp
0
0
127.0
.
0.1
:
27017
127.0
.
0.1
:
47686
ESTABLISHED
tcp
0
0
127.0
.
0.1
:
47686
127.0
.
0.1
:
27017
ESTABLISHED
tcp
0
0
192.168
.
1.111
:
27017
192.168
.
1.100
:
53462
ESTABLISHED
[root
@zjhl1
opt]#
|
(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
static
page /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 =
new
Mongo(
"192.168.100.52"
,
27017
);
// 创建连接
DB db = mongo.getDB(
"pics"
);
// 选择数据库
byte
[] files = createImage(
800
,
600
,
"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