以下操作可以在用户目录下。
安装MongoDb
如果已安装,继续下一步
1、增加mongodb repository:
[root@VM_197_21_centos ~]# vim /etc/yum.repos.d/mongodb.repo
系统是64bit的,添加
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
系统是32bit的,添加
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/
gpgcheck=0
enabled=1
2、安装mongodb
[root@VM_197_21_centos ~]# yum install mongo-10gen mongo-10gen-server
mongodb命令:
启动
[root@VM_197_21_centos ~]# service mongod start
停止
[root@VM_197_21_centos ~]# service mongod stop
查看状态
[root@VM_197_21_centos ~]# service mongod status
安装nginx-gridfs及nginx
1、安装依赖库,如果已安装,请忽略
[root@VM_197_21_centos ~]# yum -y install pcre-devel openssl-devel zlib-deve
[root@VM_197_21_centos ~]# yum -y install gcc gcc-c++
2、下载nginx-gridfs源码
[root@VM_197_21_centos ~]# git clone https://github.com/mdirolf/nginx-gridfs.git
如果没有安装git,执行
[root@VM_197_21_centos ~]# yum install git
进入nginx-gridfs文件夹,安装
[root@VM_197_21_centos ~]# cd nginx-gridfs
[root@VM_197_21_centos ~]# git checkout v0.8
[root@VM_197_21_centos ~]# git submodule inti
[root@VM_197_21_centos ~]# git submodule update
不报错,nginx-gridfs安装完毕。
3、下载nginx源码,编译安装(高版本支持不好)
[root@VM_197_21_centos ~]# wget http://nginx.org/download/nginx-1.4.7.tar.gz
解压文件
[root@VM_197_21_centos ~]# tar zxvf nginx-1.4.7.tar.gz
进入nginx文件,编译安装
[root@VM_197_21_centos ~]# cd nginx-1.4.7
[root@VM_197_21_centos ~]# ./configure --with-openssl=/usr/include/openssl --add-module=../nginx-gridfs/
[root@VM_197_21_centos ~]# make -j8
[root@VM_197_21_centos ~]# make install -j8
其中 –add-module对应设置nginx-gridfs的安装目录。
编译安装完成。
ps:如果在make 过程中报错:
ngx_http_gridfs_module.c:684:16: 错误:变量‘chunksize’被设定但未被使用
解决办法:
进入nginx主目录,修改objs/Makefile,把第3行的 -Werror 错误删除。
配置nginx
编辑nginx的配置文件
[root@VM_197_21_centos ~]# vi /usr/local/nginx/conf/nginx.conf
在server节点中添加location节点
location /image/ {
gridfs testdb
field=filename
type=string;
mongo 127.0.0.1:27017;
}
mongo的服务器地址:127.0.0.1
配置说明:
[testdb]: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]: 密码, 可省略
链接nginx服务器到bin中
[root@VM_197_21_centos ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
启动nginx服务
nginx
测试
[root@VM_197_21_centos ~]# mongofiles put 1234.jpg --local ~/1234.jpg --host 172.0.0.1 --port 27017 --db testdb --type jpg