一、安装PCRE库
PCRE(Perl Compatible Regular Expressions),提供高速的正则表达式匹配api。

yum install pcre-devel

或者到ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/下载合适的pcre源码包,然后configure/make/make install

二、安装Spider Monkey库
SpiderMonkey是JavaScript的C实现,可以提供JavaScript的代码解释功能。
到ftp://ftp.mozilla.org/pub/mozilla.org/js/下载js压缩包,解压后进入src目录,并按照如下步骤进行编译安装:

export CFLAGS=”-DJS_C_STRINGS_ARE_UTF8″  make -f Makefile.ref JS_DIST=/usr make -f Makefile.ref export

三、安装SCons
Scons是一个Python写的自动化构建工具,比传统的GNU make更加简便、可靠以及高效。
到http://sourceforge.net/projects/scons/files/scons/下载源码压缩包,解压之后按照如下步骤进行安装:

python setup.py install

四、安装MongoDB
先到http://www.mongodb.org/downloads/下载想要的MongoDB版本。建议下载1.6以上,有很多架构和功能方面的改进。
解压缩之后按照如下步骤进行安装:

scons all scons –prefix=/usr/local/mongodb install

如果想要头文件和lib包,执行

scons –prefix=/usr/local/mongodb –full install

五,让MongoDB跑起来
虽然MongoDB支持在命令行里面添加所有参数,但是为了管理方便,还是应该添加一个配置文件的。然后在命令行里面指定配置文件的位置。
下面是一个简单的配置文件:

vim /usr/local/mongodb/config/mongodb
#Directory and relavent set dbpath = /usr/local/mongodb/data logpath = /usr/local/mongodb/log/mongodb.log repairpath = /usr/local/mongodb/repair pidfilepath = /usr/local/mongodb/mongodb.pid directoryperdb = true #sysinfo = true #verbose = true logappend = true #cpu = true #Network and security set noauth = true port = 8888 maxConns = 1024 fork = true #Management #nohttpinterface = true rest = true quota = true quotaFiles = 1024 nssize = 16 #syncdelay = 5

值得注意的是,类似 nohttpinterface = false 的配置是无效的。因为这些选项默认为false,不需要配置。

运行MongDB:

mongod –config /usr/local/mongodb/config/mongodb

启动脚本 mongodb-start.sh

#!/bin/sh /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/config/mongodb ## --upgrade \ ##runs a database upgrade option if needed \

关闭脚本 mongodb-stop.sh

#!/bin/bash pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`; if [ "${pid}" != "" ]; then kill -2 ${pid}; fi

六,做服务

启动脚本init-rpm.sh

#! /bin/sh # # mongodb – this script starts and stops the mongodb daemon # # chkconfig: - 85 15 # description: MongoDB is a non-relational database storage system. # processname: mongodb # config: /opt/config/mongodb # pidfile: /opt/mongodb/mongo.pid PATH=/opt/mongodb/bin:/sbin:/bin:/usr/sbin:/usr/bin NAME=mongodb test -x $DAEMON || exit 0 set -e case "$1" in start) echo -n "Starting MongoDB... " su - mongodb -c "/opt/bin/mongodb-start" ;; stop) echo -n "Stopping MongoDB" /opt/bin/mongodb-stop ;; *) N=/etc/init.d/$NAME echo "Usage: $N {start|stop}" >&2 exit 1 ;; esac exit 0

把以上启动脚本做成服务

#mv init-rpm.sh /etc/init.d/mongodb #chmod +x /etc/init.d/mongodb #chkconfig --add mongodb #chkconfig -–level 345 mongodb on

 

MongoDB与sql语句对照表

SQL Statement  Mongo Query Language Statement 
CREATE TABLE USERS (a Number, b Number)
implicit; can be done explicitly
INSERT INTO USERS VALUES(1,1)
db.users.insert({a:1,b:1})
SELECT a,b FROM users
db.users.find({}, {a:1,b:1})
SELECT * FROM users
db.users.find()
SELECT * FROM users WHERE age=33
db.users.find({age:33})
SELECT a,b FROM users WHERE age=33
db.users.find({age:33}, {a:1,b:1})
SELECT * FROM users WHERE age=33 ORDER BY name
db.users.find({age:33}).sort({name:1})
SELECT * FROM users WHERE age>33
db.users.find({'age':{$gt:33}})})
SELECT * FROM users WHERE age<33
db.users.find({'age':{$lt:33}})})
SELECT * FROM users WHERE name LIKE "%Joe%"
db.users.find({name:/Joe/})
SELECT * FROM users WHERE name LIKE "Joe%"
db.users.find({name:/^Joe/})
SELECT * FROM users WHERE age>33 AND age<=40
db.users.find({'age':{$gt:33,$lte:40}})})
SELECT * FROM users ORDER BY name DESC
db.users.find().sort({name:-1})
CREATE INDEX idx_name ON users(name)
db.users.ensureIndex({name:1})
CREATE INDEX idx_name ON users(name,ts DESC)
db.users.ensureIndex({name:1,ts:-1})
SELECT * FROM users WHERE a=1 and b='q'
db.users.find({a:1,b:'q'})
SELECT * FROM users LIMIT 10 SKIP 20
db.users.find().limit(10).skip(20)
SELECT * FROM users WHERE a=1 or b=2
db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )
SELECT * FROM users LIMIT 1
db.users.findOne()
EXPLAIN SELECT * FROM users WHERE z=3
db.users.find({z:3}).explain()
SELECT DISTINCT last_name FROM users
db.users.distinct('last_name')
SELECT COUNT(*y) FROM users
db.users.count()
SELECT COUNT(*y) FROM users where AGE > 30
db.users.find({age: {'$gt': 30}}).count()
SELECT COUNT(AGE) from users
db.users.find({age: {'$exists': true}}).count()
UPDATE users SET a=1 WHERE b='q'
db.users.update({b:'q'}, {$set:{a:1}}, false, true)
UPDATE users SET a=a+2 WHERE b='q'
db.users.update({b:'q'}, {$inc:{a:2}}, false, true)
DELETE FROM users WHERE z="abc"
db.users.remove({z:'abc'});