<pre name="code" class="python">/确保你已经安装了mongodb和git工具
// 下载nginx-gridfs
注意: 需要特定版本的nginx-gridfs
//安装 nginx
配置 /usr/local/pcre-8.38
pcre:
tar -xzvf pcre-8.21.tar.gz
cd pcre-8.21
./configure && make && make install
tar -zxvf nginx-xxx.tar.gz
cd nginx-xxx
./configure --add-module=/usr/local/nginx-gridfs --with-http_stub_status_module --with-pcre=/usr/local/pcre-8.38
make
make install
nginx -V
nginx: nginx version: nginx/1.0.1
nginx: built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
nginx: configure arguments: --add-module=/usr/local/nginx-gridfs --with-http_stub_status_module --with-pcre=/usr/local/pcre-8.38
注【/usr/local/nginx-gridfs/ 为nginx-gridfs下载目录】
//配置nginx
修改/usr/local/nginx/conf/nginx.conf配置文件。
注:这里说明下,nginx有两个nginx.conf的配置文件,勿配错;
在nginx.conf中server加入:
location /pics/ {
gridfs pics field=filename type=string;
mongo 114.35.2.17:27017;
}
注【my_app 为mongodb存储文件的数据库名字】
//启用nginx
//打开浏览器输入要访问的图片:http://192.168.1.109:28118/gridfs/6b6e567cjw1dmea3323e1j.jpg。 你看到了之前上传的文件
mongofiles put weixin_qrcode_EsYe.jpg -h127.0.0.1 -db pics -t jpg
connected to: 127.0.0.1
added file: { _id: ObjectId('56a86cc854e10ebac7c9c5e2'), filename: "weixin_qrcode_EsYe.jpg", chunkSize: 262144, uploadDate: new Date(1453878472772), md5: "138b8a395ee91dddaea9b913334ca2a7", length: 37555, contentType: "jpg" }
done
filename')
-t [ --type ] arg MIME type for put (default is to omit)
use tt
这样就创建了一个数据库,如果什么都不操作离开的话,这个库就会被系统删除.所以我们还要执行下面的命令:
db.usr.insert({'name':'tompig'});
db.usr.insert({'name':'tompig1','id':1});
> use tt
switched to db tt
> db.usr.insert({'name':'tompig'});
> db.usr.insert({'name':'tompig1','id':1});
> db.getCollectionNames();
[ "system.indexes", "usr" ]
> db.userInfo.find();
> select * from user;
Wed Jan 27 14:44:18.923 SyntaxError: Unexpected identifier
> db.user.find();
> db.usr.find()
{ "_id" : ObjectId("56a867082830668c6cd6a8e5"), "name" : "tompig" }
{ "_id" : ObjectId("56a867092830668c6cd6a8e6"), "name" : "tompig1", "id" : 1 }
http://192.168.32.34/pics/weixin_qrcode_EsYe.jpg
[root@yytpxt ~]# mongofiles list -h114.55.5.57 -db pics
connected to: 114.55.5.57
weixin_qrcode_b_YH.jpg 38916
Mongo connection dropped, could not reconnect,
11.上传文件验证:
sudo mongofiles put --host localhost --port 27017 --db img --local ~/photo.jpg --type jpg
如果提示Error: need a filename错误,就在--local ~/photo.jpg xxxname.jpg 添加一个名字即可 (http://hi.baidu.com/manbuzhiwu/item/9adbce5d45405811e6c4a59c)
mongofiles put --host localhost --port 27017 --db img --local ~/photo.jpg --type jpg
redis01:/root# mongofiles put --host 192.168.32.34 --port 27017 --db pics 1.gif --type gif
connected to: 192.168.32.34:27017
added file: { _id: ObjectId('56a972f55f55d2eb2ae1de18'), filename: "1.gif", chunkSize: 262144, uploadDate: new Date(1453945589610), md5: "5752b38d6c10dd9b9a15792b86b46947", length: 22718, contentType: "gif" }
done!
redis01:/root# mongofiles -h 192.168.32.34 list -db pics
connected to: 192.168.32.34
q.jpg 79398
test6.jpg 6759
1.gif 22718
mongdb外部访问:
redis01:/root# /usr/local/mongodb/bin/mongod --bind_ip 192.168.32.34 -f /usr/local/mongodb/bin/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1403
all output going to: /usr/local/mongodb/logs/mongodb.log
mongdb列出内容:
redis01:/root# mongofiles put q.jpg -h192.168.32.34 -db pics -t jpg
connected to: 192.168.32.34
added file: { _id: ObjectId('56a87ec80004fad5d9bdae4a'), filename: "q.jpg", chunkSize: 262144, uploadDate: new Date(1453883081024), md5: "196fea5be1558e28067d7e1e2c1ebc0d", length: 79398, contentType: "jpg" }
done!
redis01:/root#
redis01:/root# cat /usr/local/mongodb/bin/mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/logs/mongodb.log
port=27017
fork=true
nohttpinterface=true
redis01:/root#
}
location /pics/ {
gridfs pics field=filename type=string;
mongo 192.168.32.34:27017;
}