CentOS 编译安装 MongoDB 数据库

October 16, 2010 – 12:15 am

测试环境

CentOS 5.3 x86_64 安装开发工具和开发库

安装Scons

yum install -y python-devel
tar zxf scons-1.2.0.tar.gz
cd scons-1.2.0
python setup.py install
cd ..

安装spidermonkey库

yum install -y boost boost-devel
tar zxf 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 ../..

安装pcre

tar zxf pcre-6.6.tar.gz
cd pcre-6.6
./configure --enable-utf8 --enable-unicode-properties
make && make install
cd ..

安装MongoDB

tar zxf mongodb-src-r1.6.3.tar.gz
cd mongodb-src-r1.6.3
scons all
mkdir /db
scons --prefix=/db/mongo163 install

如果需要安装lib和head,使用如下方式安装:

scons --prefix=/db/mongo163 --full install

创建配置文件

mkdir -p /db/mongo163/etc /db/mongo163/data /db/mongo163/log/ /db/mongo163/repair
touch /db/mongo163/etc/mongo.conf
echo "
dbpath = /db/mongo163/data
logpath = /db/mongo163/log/mongodb.log
repairpath = /db/mongo163/repair
pidfilepath = /db/mongo163/mongodb.pid
directoryperdb = true
logappend = true
noauth = true
port = 33306
maxConns = 1024
fork = true
rest = true
quota = true
quotaFiles = 1024
nssize = 16
" > /db/mongo163/etc/mongo.conf

启动mongodb

ln -s /db/mongo163/bin/mongod /usr/bin/mongod
mongod -f /db/mongo163/etc/mongo.conf

看看是不是启动起来了,但是使用这种方式管理mongodb服务器很不明智,我们完善一下:

mkdir -p /db/mongo163/srv
touch /db/mongo163/srv/mongodb-start
echo "
#!/bin/sh
mongod -f /db/mongo163/etc/mongo.conf
" > /db/mongo163/srv/mongodb-start
touch /db/mongo163/srv/mongodb-stop
echo "
#!/bin/bash
pid=`ps -o pid,command ax | grep mongod | awk '!/awk/ && !/grep/ {print $1}'`;
if [
"${pid}" != "" ]; then
    kill -2 ${pid};
fi
" > /db/mongo163/srv/mongodb-stop
chmod a+x /db/mongo163/srv/mongodb-start
chmod a+x /db/mongo163/srv/mongodb-stop
touch /etc/rc.d/init.d/mongodb163
echo '
#! /
bin/sh
#
# mongodbthis script starts and stops the mongodb daemon
#
# chkconfig: - 85 15
# description: MongoDB is a non-relational database storage system.
# processname: mongodb
# config: /db/mongo163/etc/mongo.conf
# pidfile: /db/mongo163/mongodb.pid
PATH=/db/mongo163/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=mongodb
test -x $DAEMON || exit 0
set -e
case "$1" in
 
start)
        
echo -n "Starting MongoDB... "
        /
db/mongo163/srv/mongodb-start
        ;;
 
stop)
        
echo -n "Stopping MongoDB... "
        /
db/mongo163/srv/mongodb-stop
        ;;
      *)
            
N=/etc/init.d/$NAME
            
echo "Usage: $N {start|stop}" >&2
            
exit 1
            ;;
    
esac
    
exit 0
' > /
etc/rc.d/init.d/mongodb163
chmod a+x /etc/rc.d/init.d/mongodb163
chkconfig --add mongodb163
chkconfig --level 345 mongodb163 on
/
etc/rc.d/init.d/mongodb163 start

通过php操作一下mongodb
安装mongodb扩展省略,和装其他php扩展一样,安装完毕我们创建一个脚本跑一下看看。

<?php
 
//连接
$m = new Mongo('192.168.90.187:33306');
 
//选择数据库
$db = $m->myTest;
$collection = $db->test;
 
//添加记录
$obj = array( "title" => "第一个", "detail" => "测试" );
$collection->insert($obj);
 
$obj = array( "title" => "第二个", "online" => true );
$collection->insert($obj);
 
//查询
$cursor = $collection->find();
foreach ($cursor as $obj)
{
 
var_dump($obj);
 
echo $obj["title"] . "\n";
}
 
//关闭连接
$m->close();
?>

打印结果为:

array(3) {
  ["_id"]=>
  object(MongoId)#8 (0) {
  }
  ["title"]=>
  string(9) "第一个"
  ["detail"]=>
  string(6) "测试"
}
第一个
array(3) {
  ["_id"]=>
  object(MongoId)#4 (0) {
  }
  ["title"]=>
  string(9) "第二个"
  ["online"]=>
  bool(true)
}
第二个
  1. 1 Trackback(s)

  2. Mar 27, 2011: Stam He的博客 » CentOS 编译安装 MongoDB 1.8 数据库

Post a Comment