CentOS系统MongoDB 编译安装教程详解

作者:袖梨 2022-06-30

CentOS5编译安装mongodb

mongodb有已经编译好的二进制包,解压到对应目录就可以使用。
下面介绍一下,怎么从源码进行编辑安装。

安装前:

安装scons

wget http://prdownloads.sourceforge.net/scons/scons-2.3.4.tar.gz
python setup.py install

下载mongodb源码&安装:

wget https://github.com/mongodb/mongo/archive/r2.2.7-rc0.tar.gz
tar xvf r2.2.7-rc0.tar.gz -C /usr/src
cd /usr/src/mongo-r2.2.7-rc0
scons --prefix=/usr/local/mongo install

配置:

mkdir -p /usr/local/mongo/etc
cp /usr/src/mongo-r2.2.7-rc0/rpm/mongod.conf /usr/local/mongo/etc

注:如果有需要,编辑mongod.conf配置文件,指定一下数据存放的目录等等
如我的配置:

logpath=/var/log/mongo/mongod.log
logappend=true
fork = true
dbpath=/data0/mongodb
pidfilepath = /var/run/mongodb/mongod.pid

启动:

/usr/local/mongo/bin/mongod --config=/usr/local/mongo/etc/mongod.conf

测试连接:

[root@localhost ~]# /usr/local/mongo/bin/mongo
MongoDB shell version: 2.2.7-rc0
connecting to: test
> show dbs;
local 0.078125GB
mydb 0.203125GB
sina 0.453125GB
wiki_bak 0.203125GB


CentOS6 上的MongoDB 编译安装

1.安装依赖的软件包
# yum install pcre-devel python-devel scons

2.从官方下载最新的源码包
http://www.mongodb.org/downloads

3.使用Py 编译工具scons 进行编译并安装
# tar zxf mongdb-src.tar.gz
//解压
# cd mongodb-src
# scons -j 8 all               //使用8线程并行编译,推荐使用CPU核心数
# scons –prefix=/usr/local/mongo install
// 如果需要lib库和include头文件 需要加上 –full 参数如下:
# scons –prefix=/usr/local/mongo –full install

4.添加mongodb 用户和配置
# useradd mongodb -M -s /sbin/nologin

//创建目录,根据需要进行自定义
# mkdir -pv /etc/mongo                      //配置文件路径
# mkdir -pv /var/lib/mongo                 //数据存储路径
# mkdir -pv /var/log/mongo                //日志文件路径
# mkdir -pv /var/run/mongodb            //Pid 路径

//将源码包中的配置文件复制到 /etc/mongo 下
# cp mongodb-src/rpm/mongod.conf /etc/mongo

//给予日志和数据路径mongodb用户组访问权限
# chown -R mongodb.mongodb /var/log/mongodb
# chown -R mongodb.mongodb /var/lib/mongodb

5. 编辑配置文件
# vi /etc/mongo/mongod.conf
—–配置参考下面的路径进行或查阅配置说明文档——
dbpath = /var/lib/mongo
logpath = /var/log/mongo
pidfilepath = /var/run/mongodb/mongod.pid
directoryperdb = true
logappend = true
noauth = true
port = 27017
maxConns = 1024
fork = true
rest = true
quota = true
quotaFiles = 1024
nssize = 16
—–配置参考结束——

6.配置开机启动文件
//将下列脚本保存为 mongod 并存放在/etc/rc.d/init.d/ 下,并给与执行权限

#!/bin/sh
#
# mongodb      init file for starting up the MongoDB server
#
# chkconfig:   - 20 80
# description: Starts and stops the MongDB daemon that handles all
#              database requests.

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

exec="/usr/local/mongo/bin/mongod"
prog="mongod"
logfile="/var/log/mongodb/mongodb.log"
options=" -f /etc/mongo/mongod.conf"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile="/var/lock/subsys/mongod"

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $prog: "
    daemon --user mongodb "$exec --quiet $options run >> $logfile 2>&1 &"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

# chkconfig –add mongod
//设置为开机启动
# service mongod start
// 进行启动并测试27017端口是否开放,如果配置有误,请查阅 /var/log/mongo  下的日志输出信息

7. PHP的MongoDB 扩展安装

从 http://pecl.php.net/  搜索mongo 并获取最新稳定版的扩展包
# tar zxf mongo-1.3.0.tgz
# cd mongo-1.3.0
# /usr/local/php/bin/phpize
# ./configure –with-php-config=/usr/local/php/bin/php-config   // 注意php-config 安装路径
# make && make install
//解压并配置进行安装

编辑php.ini 配置文件在末行添加

extension=mongo.so

//如果之前从未安装其它扩展,需要先指定extension_dir 扩展目录
完成后重启PHP,查看phpinfo 有mongo 的项目出现则安装成功.

相关文章

精彩推荐