kv永久存储mongodb

系统版本:32位CENTOS5.5

安装MongoDB

安装scons、js

yum install -y boost boost-devel pcre pcre-devel
wget http://sourceforge.net/projects/scons/files/scons/2.1.0.alpha.20101125/scons-2.1.0.alpha.20101125.tar.gz/download
tar xvf scons-2.1.0.alpha.20101125.tar.gz
cd scons-2.1.0.alpha.20101125
python setup.py install
cd ..

wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz
tar zxvf js-1.7.0.tar.gz
cd js/src/
export CFLAGS="-DJS_C_STRINGS_ARE_UTF8"
make -f Makefile.ref
JS_DIST=/usr gmake -f Makefile.ref export
cd ../..

安装MongoDB

wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.4.tgz
tar xvf http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.4.tgz
mv mongodb-linux-i686-2.0.4 /usr/local/mongodb

mkdir -pv /usr/local/mongodb/data  /usr/local/mongodb/etc  /usr/local/mongodb/log

配置文件

cat > /usr/local/mongodb/etc/mongod.conf <<'EOF'
# log file to send write to instead of stdout – has to be a file, not directory
logpath=/usr/local/mongodb/log/mongod.log

# append to logpath instead of over-writing
logappend=true

# fork and run in background
fork = true

# specify port number
port = 27017

# comma separated list of ip addresses to listen on – all local ips by default
#bind_ip = 192.168.51.119

# directory for datafiles
dbpath = /usr/local/mongodb/data

# full path to pidfile (if not set, no pidfile is created)
pidfilepath = /usr/local/mongodb/log/mongod.pid

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security. Off is currently the default
#noauth = true
auth = true

# Verbose logging output.
#verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# Enable db quota management
quota = true

# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#oplog = 0

# Diagnostic/debugging option
#nocursors = true

# Ignore query hints
#nohints = true

# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true

# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
nssize = 16

# Accout token for Mongo monitoring server.
#mms-token = <token>

# Server name for Mongo monitoring server.
#mms-name = <server-name>

# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>

# Replication Options

# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com
EOF

启动脚本

cat > /etc/rc.d/init.d/mongod <<'EOF'
#!/bin/bash
#
# mongodb     Startup script for the mongodb server
#
# chkconfig: - 64 36
# description: MongoDB Database Server
#
# processname: mongodb
#

# Source function library
. /etc/rc.d/init.d/functions

prog="mongod"
mongod="/usr/local/mongodb/bin/mongod"
OPTIONS=" -f /usr/local/mongodb/etc/mongod.conf"
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        $mongod $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /usr/local/mongodb/log/$prog
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /usr/local/mongodb/log/$prog
        return $RETVAL
}

reload() {
        echo -n $"Reloading $prog: "
        killproc $prog -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /usr/local/mongodb/log/$prog ]; then
            stop
            start
        fi
        ;;
    reload)
        reload
        ;;
    status)
        status $mongod
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|condrestart|reload|status}"
        RETVAL=1
esac

exit $RETVAL
EOF


# 执行权限
chmod a+x /etc/rc.d/init.d/mongod

# 添加到开机启动项
chkconfig --add mongod
chkconfig mongod on

安装php mongo 扩展

wget http://pecl.php.net/get/mongo-1.2.2.tgz
tar zxf mongo-1.2.2.tgz
cd mongo-1.2.2
phpize
./configure
make && make install

完成后在php.ini文件增加一行

[MongoDB]
extension=mongo.so
成功的话phpinfo()会看到mongo一项

主从配置

MASTER

修改配置文件

vim /usr/local/mongodb/etc/mongod.conf
# 打开
master = true
source = 10.0.0.2 #slave_ip

SLAVE

修改配置文件

vim /usr/local/mongodb/etc/mongod.conf
# 打开
slave = true
source = 10.0.0.3 #master_ip

主从不同步

数据同步大致流程是 :当一个slave启动时,它会对master进行一次彻底同步。slave将复制master中的每一个数据。当初始化完成后slave将查询master的oplog并执行这些操作来保持数据跟新。

如果slave机器上的操作落后master机器太多,slave会处于out-of-sync状态。这时候表示slave不能通过执行同步操作使本地数据赶上master上的数据,因为master中的每一个操作都太新了。造成这种情况的原因包括slave宕机或者忙于处理请求。如果同步时间戳超出了oplog的时间戳,它将重新开始一次彻底的同步(通过执行resync操作)。

当slave处于out-of-sync状态时,复制将被挂起,slave需要重新从master进行同步。resync流程可以手动执行,在slave的admin数据库上运行同步命令:

use admin
db.runCommand({'resync':1})
或者自动执行:在启动slave时使用 --autoresync选项。因为resync是非常操作量大且耗时,最好通过设置一个足够大的oplogSize来避免resync(默认的 oplog大小是空闲磁盘大小的5%)。

MongoDB命令基础

数据库常用命令

#进入数据库admin
use admin;

#增加或修改用户密码
db.addUser('name','pwd');

#查看用户列表
db.system.users.find();

#用户认证
db.auth('name','pwd');

#删除用户
db.removeUser('name');

#查看所有用户
show users;

#查看所有数据库
show dbs;

#查看所有的collection
show collections;

#查看各collection的状态
db.printCollectionStats();

#查看主从复制状态
db.printReplicationInfo();

#修复数据库
db.repairDatabase();

#设置记录profiling(0=off 1=slow 2=all)
db.setProfilingLevel(1);

#查看profiling
show profile;

#拷贝数据库
db.copyDatabase('mail_addr','mail_addr_tmp');

#删除collection
db.mail_addr.drop();

#删除当前的数据库
db.dropDatabase();

增删改查

#存储嵌套的对象
db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]});

#存储数组对象
db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']});

#根据query条件修改,如果不存在则插入,允许修改多条记录
db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true);

#删除yy=5的记录
db.foo.remove({'yy':5});

#删除所有的记录
db.foo.remove();

索引

#增加索引:1(ascending),-1(descending)
db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true});

#索引子对象
db.user_addr.ensureIndex({'Al.Em': 1});

#查看索引信息
db.foo.getIndexes();
db.foo.getIndexKeys();

#根据索引名删除索引
db.user_addr.dropIndex('Al.Em_1');

查询

#查找所有
db.foo.find();

#查找一条记录
db.foo.findOne();

#根据条件检索10条记录
db.foo.find({'msg':'Hello 1'}).limit(10);

#sort排序
db.deliver_status.find({'From':'ixigua@sina.com'}).sort({'Dt',-1});
db.deliver_status.find().sort({'Ct':-1}).limit(1);

#count操作
db.user_addr.count();

#distinct操作,查询指定列,去重复
db.foo.distinct('msg');

#”>=”(大于等于)操作
db.foo.find({"timestamp": {"$gte" : 2}});

#子对象的查找
db.foo.find({'address.city':'beijing'});

管理命令

#查看collection数据的大小
db.deliver_status.dataSize();

#查看colleciont状态
db.deliver_status.stats();

#查询所有索引的大小
db.deliver_status.totalIndexSize();

高级查询相关信息

#条件操作符
$gt : >
$lt : <
$gte: >=
$lte: <=
$ne : !=、<>
$in : in
$nin: not in
$all: all
$not: 反匹配(1.3.3及以上版本)

相关参考

MongoDB中文文档