Debian7.0 安装mongodb
更新需要的包:
apt-get install -y scons libpcrecpp0 libpcre3-dev libpcap-dev libmozjs-dev xulrunner-dev
1.解压缩tar文件
进入指定安装mongodb的目录后,执行如下命令
tar -xvf mongodb-linux-x86_64-2.4.8.tgz
2. 创建数据文件和日志文件
mv mongodb-linux-x86_64-2.4.8 /opt/mongodb
mkdir /usr/local/mongodb/data
mkdir /usr/local/mongodb/logs
3、在安装mongodb的用户下添加如下环境变量,以便直接使用mongodb bin目录下的命令
root@d1:~# cat /etc/profile|grep mon
PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin
测试命令:
root@d1:~# mongo
MongoDB shell version: 2.4.8
connecting to: test
Tue Jan 14 16:31:07.240 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed
4、启动:
root@d1:~# mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/mongodb.log --logappend --port=27017 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 4128
all output going to: /usr/local/logs
child process started successfully, parent exiting
root@d1:~# mongo
MongoDB shell version: 2.4.8
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
Questions? Try the support group
> use test;
switched to db test
>
5、设置mongodb自动启动
将如下命令添加到 /etc/rc.local
mongod --dbpath=/opt/mongodb/data --logpath=/opt/mongodb/logs --logappend --port=27017 --fork
6、NGINX增加如下配置,并重新启动NGINX:
location /pics/ {
gridfs pics
field=filename
type=string;
mongo 10.10.18.242:27017;
}
7、制造测试数据
上传一个文件
root@d1:~# mongofiles put 1.jpg -h10.10.18.244 -db pics -t jpg
connected to: 10.10.18.244
added file: { _id: ObjectId('52d52fdce63f0d8e41b0cbb9'), filename: "1.jpg", chunkSize: 262144, uploadDate: new Date(1389703132557), md5: "3dd9566037a412619b5ca018c6aac019", length: 2807, contentType: "jpg" }
done!
列出库中文件:
root@d1:~# mongofiles list -db pics
connected to: 127.0.0.1
1.jpg 2807
1.txt 1069
mongod -f /usr/local/mongodb/mongo.conf
root@d1:/usr/local/mongodb# mongo 10.10.18.242:27017/pics
MongoDB shell version: 2.4.8
connecting to: 10.10.18.242:27017/pics
>
########################################################################################################
保存文件
# echo "test" >1.txt
# ./mongofiles put 1.txt
connected to: 127.0.0.1
added file: { _id: ObjectId('504d5baa867ba4bf176a297b'), filename: "1.txt", chunkSize: 262144, uploadDate: new Date(1347247018446), md5: "d8e8fca2dc0f896fd7cb4cb0031ba249", length: 5 }
done!
获取文件
# ./mongofiles get 1.txt
connected to: 127.0.0.1
done write to: 1.txt
显示文件列表
# ./mongofiles list
connected to: 127.0.0.1
1.txt 5
查找文件
# ./mongofiles search 1.*
connected to: 127.0.0.1
1.txt 5
# ./mongofiles list 1.*
connected to: 127.0.0.1
1.txt 5
########################################################################################################
gridfs配置说明
gridfs DB_NAME [root_collection=ROOT] [field=QUERY_FIELD] [type=QUERY_TYPE] [user=USERNAME] [pass=PASSWORD]
gridfs 表示告诉nginx服务器要调用gridfs模块
root_collection= 指定Gridfs collection的前缀. 默认: fs
field= 指定用于查询的字段 可以是 _id 和 filename. 默认: _id
type= 指定查询的类型,这里支持 objectid, string 和int. 默认: objectid
user= 指定数据库的用户名. 默认: NULL
pass= 指定数据库的密码. 默认: NULL
这里配置了2个地址,分别对应到上面的创建的test和test2库。
location /gridfs/ {
gridfs test; #指定db 为test,其它均为默认本地27017服务
}
location /gridfs2/ {
gridfs test2 field=filename type=string;
mongo 127.0.0.1:27017;
}
阅读(3212) | 评论(0) | 转发(0) |