|
- Mongodb官方文档:http://docs.mongodb.org/manual/
- 一、介绍Mongodb的web管理界面
1、Mongodb的安装
2、启动Mongodb以及web管理端口
注释:截取一部分参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| [iyunv@mongodb ~]# mongod --help
Options:
General options:
-f [ --config ] arg configuration file specifying additional options
--quiet quieter output
--port arg specify port number - 27017 by default
--dbpath arg (=/data/db) directory for datafiles - defaults to /data/db/
--logpath arg log file to send write to instead of stdout - has
--httpinterface enable http interface
--fork fork server process
--rest turn on simple rest api
注意:默认情况下mongodb 2.6版本的管理关口是是关闭的
--httpinterface:启用mongodb的http的端口
[iyunv@mongodb ~]# mongod --httpinterface
2014-06-10T16:19:00.067-0400 [initandlisten] MongoDB starting : pid=6157 port=27017 dbpath=/data/db 64-bit host=mongodb
2014-06-10T16:19:00.068-0400 [initandlisten] db version v2.6.1
2014-06-10T16:19:00.068-0400 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2014-06-10T16:19:00.068-0400 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-06-10T16:19:00.068-0400 [initandlisten] allocator: tcmalloc
2014-06-10T16:19:00.068-0400 [initandlisten] options: { net: { http: { enabled: true } } }
2014-06-10T16:19:00.198-0400 [initandlisten] journal dir=/data/db/journal
2014-06-10T16:19:00.198-0400 [initandlisten] recover : no journal files present, no recovery needed
2014-06-10T16:19:00.209-0400 [initandlisten] waiting for connections on port 27017
2014-06-10T16:19:00.252-0400 [websvr] admin web console waiting for connections on port 28017
|
3.访问Mongodb的管理界面 http://172.16.3.126:28017/
二、在Mongodb中存放文件有两种方式:
1、命令行方式mongofiles
#上传图片到mongodb数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| [iyunv@mongodb opt]# mongofiles put zhengyansheng.jpg -h 127.0.0.1 -d pics -t jpg
connected to: 127.0.0.1
added file: { _id: ObjectId('53976bfee549115e4df63464'), filename: "zhengyansheng.jpg", chunkSize: 261120, uploadDate: new Date(1402432510426), md5: "414c671373903f8093acbace1ab35978", length: 12335, contentType: "jpg" }
done!
#查看mongodb是否有图片
[iyunv@mongodb opt]# mongo
MongoDB shell version: 2.6.1
connecting to: test
> use pics
switched to db pics
> show collections
fs.chunks
fs.files
system.indexes
> db.fs.files.find()
{ "_id" : ObjectId("53976bfee549115e4df63464"), "filename" : "zhengyansheng.jpg", "chunkSize" : 261120, "uploadDate" : ISODate("2014-06-10T20:35:10.426Z"), "md5" : "414c671373903f8093acbace1ab35978", "length" : 12335, "contentType" : "jpg" }
>
|
2、客户端编程
#程序执行上传
#PHP 上传图片至mongodb
#注意在lamp或者lnmp环境才能执行上传
1
2
3
4
5
6
7
8
9
10
11
12
| <?php
$conn = new Mongo("127.0.0.1:27017");
$db = $conn->pics;
$grid = $db->getGridFS();
//直接上传图片
//$id = $grid->put('/opt/server.jpg', array('filename' => 'server.jpg'));
//二进制流形式写入
$data = file_get_contents('/tmp/server.jpg');
$id = $grid->storeBytes($data, array('filename' => 'server.jpg'));
$result = $grid->find();print_r($result);
$conn->close();
?>
|
三、安装nginx的插件nginx-gridfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| gridfs为nginx的扩展模块
mdirolf-nginx-gridfs-v0.8-0-gb5f8113.tar.gz的下载地址
https://download.github.com/mdir ... 8-0-gb5f8113.tar.gz
mongodb-mongo-c-driver为gridfs连接mongodb数据库的驱动
mongodb-mongo-c-driver-v0.3-0-g74cc0b8.tar.gz的下载地址
https://download.github.com/mong ... 3-0-g74cc0b8.tar.gz
[iyunv@mongodb src]# tar xf nginx-gridfs-0.8.tar.gz
[iyunv@mongodb src]# tar xf mongo-c-driver-0.3.1.tar.gz
[iyunv@mongodb src]# ll
total 80
drwxrwxr-x. 6 root root 4096 May 12 2011 mongo-c-driver-0.3.1
-rw-r--r--. 1 root root 53024 Jun 11 2014 mongo-c-driver-0.3.1.tar.gz
drwxrwxr-x. 3 root root 4096 Apr 14 2011 nginx-gridfs-0.8
-rw-r--r--. 1 root root 18883 Jun 11 2014 nginx-gridfs-0.8.tar.gz
[iyunv@mongodb src]# ll nginx-gridfs-0.8/mongo-c-driver/
total 0
[iyunv@mongodb src]# mv mongo-c-driver-0.3.1/* nginx-gridfs-0.8/mongo-c-driver/
|
四、编译nginx并添加模块nginx-gridfs
1、安装nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
| yum -y install gcc gcc-c++ make unzip wget vim openssh-clients
unzip pcre-8.33.zip
cd pcre-8.33
./configure --prefix=/usr/local/pcre
make && make install
cd ..
tar xf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib
make && make install
cd ..
tar xf openssl-1.0.0l.tar.gz
cd openssl-1.0.0l
./config --prefix=/usr/local/openssl
make && make install
cd ..
groupadd -r nginx
useradd -g nginx -r -s /sbin/nologin nginx
tar xf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--with-pcre=../pcre-8.33 \
--with-zlib=../zlib-1.2.8 \
--with-openssl=../openssl-1.0.0l \
--with-http_stub_status_module \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_gzip_static_module \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--add-module=/usr/local/src/nginx-gridfs-0.8
make -j8 && make install -j8
===vim /etc/init.d/nginx===
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /usr/local/nginx/sbin/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
====赋予权限
chmod +x /etc/init.d/nginx
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
====启动nginx服务
service nginx start
|
2、配置nginx规则
1
2
3
4
5
6
7
8
9
10
11
12
13
| location /pics/ {
gridfs pics field=filename type=string;
mongo 127.0.0.1:27017;
}
#参数解释
gridfs:nginx识别插件的关键字
pics: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:mongodb url
|
重新启动nginx
3、访问nginx中mongodb的图片资源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| 这里需要证明一个结论:就是Mongodb不缓存在客户端的浏览器中
做个试验来证明我的结论
首先我们来访问nginx的默认首页
访问:http://172.16.3.126/
[iyunv@mongodb ~]# tail -f /usr/local/nginx/logs/access.log
172.16.3.136 - - [10/Jun/2014:17:11:35 -0400] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
172.16.3.136 - - [10/Jun/2014:17:11:35 -0400] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
172.16.3.136 - - [10/Jun/2014:17:11:48 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
172.16.3.136 - - [10/Jun/2014:17:11:51 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
172.16.3.136 - - [10/Jun/2014:17:11:56 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
再次访问mongodb
访问:http://172.16.3.126/pics/zhengyansheng.jpg
[iyunv@mongodb ~]# tail -f /usr/local/nginx/logs/access.log
172.16.3.136 - - [10/Jun/2014:17:12:40 -0400] "GET /pics/zhengyansheng.jpg HTTP/1.1" 200 12335 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
172.16.3.136 - - [10/Jun/2014:17:12:40 -0400] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
172.16.3.136 - - [10/Jun/2014:17:12:55 -0400] "GET /pics/zhengyansheng.jpg HTTP/1.1" 200 12335 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
172.16.3.136 - - [10/Jun/2014:17:12:58 -0400] "GET /pics/zhengyansheng.jpg HTTP/1.1" 200 12335 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" "-"
大家注意看状态码会发现,当我们访问nginx的默认首页的时候状态码是200-->304-->304-->304 说明从浏览器取的缓存
当我们访问mongodb的时候状态吗是200-->200-->200-->200 说明每次都是从服务器取的资源
|
|
|