以下是安装脚本
mkdir mongodb
cd mongodb
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-static-legacy-1.2.4.tgz
tar xzf mongodb-linux-x86_64-static-legacy-1.2.4.tgz
cd mongodb-linux-x86_64-static-1.2.4/
mkdir mongodb
cd mongodb
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-static-legacy-1.2.4.tgz
tar xzf mongodb-linux-x86_64-static-legacy-1.2.4.tgz
cd mongodb-linux-x86_64-static-1.2.4/
#建立数据保存路径
mkdir data
cd data
mkdir db
启动服务
cd /home/bmc/mongodb/mongodb-linux-x86_64-static-1.2.4/bin/
./mongod --dbpath=/home/bmc/mongodb/data/db/
cd /home/bmc/mongodb/mongodb-linux-x86_64-static-1.2.4/bin/
./mongod --dbpath=/home/bmc/mongodb/data/db/
config
根据如下链接编写自己的config文件
http://www.mongodb.org/display/DOCS/File+Based+Configuration
你可以使用nginx+gridfs插件来进行访问mongodb,但是gridfs 插件需要boost,由于boost版本问题,这个东东基本安装不上或是安装很费劲,您可以使用django启动服务来代替这个复杂的插件,代码如下 :)
Views
return HttpResponse(im, mimetype="image/JPEG")
return HttpResponse(im, mimetype="image/JPEG")
后台连接代码:
#encoding=utf-8
from pymongo import Connection
from gridfs import *
from PIL import Image
import StringIO
import threading, time
#文件处理系统
class GFS:
#定义connection and fs
c = None
db = None
fs = None
instance = None
locker = threading.Lock()
#初始化
def __init__(self):
print "__init__"
GFS._connect()
print "server info " + " * " * 40
print GFS.c.server_info
#获得单列对象
@staticmethod
def getInstance():
GFS.locker.acquire()
try:
GFS.instance
if not GFS.instance:
GFS.instance = GFS()
return GFS.instance
finally:
GFS.locker.release()
#写入
def put(self,name,image,format="png",mime="image"):
gf = None
data = None
try:
data = StringIO.StringIO()
image.save(data,format)
data.getvalue()
name = "%s.%s" % (name,format)
print "name is %s" % name
gf = GFS.fs.open(name,"w")
gf.content_type = "%s/%s" % (mime,format)
gf.write(data.getvalue())
finally:
try:
gf.close()
data.close()
finally:
GFS.c = None
GFS._connect()
#获得图片
def get(self,name):
gf = None
try:
gf = GFS.fs.open(name,"r")
print gf
im = gf.read()
dic = {}
dic["chunk_size"] = gf.chunk_size
dic["metadata"] = gf.metadata
dic["mode"] = gf.mode
dic["length"] = gf.length
dic["upload_date"] = gf.upload_date
dic["name"] = gf.name
dic["content_type"] = gf.content_type
return (im , dic)
except Exception,e:
print e
return (None,None)
finally:
if gf:
if not gf.closed:
gf.close()