CentOS6.3安装MongoDB2.2 及 安装PHP的MongoDB客户端

时间:2022-03-30 01:10:46

下载源码:(放到 /usr/local/src 目录下)

到官网 http://www.mongodb.org/downloads 下载源码

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.2.7.tgz (64位)

https://fastdl.mongodb.org/linux/mongodb-linux-i686-2.2.7.tgz (32位)

注意:32位系统安装32位的,64位系统安装64位的!

安装MongoDB

#创建mongodb安装相关目录

mkdir -pv /usr/local/mongodb
mkdir -pv /usr/local/mongodb/etc
mkdir -pv /usr/local/mongodb/data
mkdir -pv /usr/local/mongodb/logs
mkdir -pv /usr/local/mongodb/repair

#将解压包内的bin文件夹移动到安装目录

cd /usr/local/src/
tar -xzvf ./mongodb-linux-i686-2.2.7.tgz
cd ./mongodb-linux-i686-2.2.7
mv ./bin /usr/local/mongodb

#创建mongodb用户和mongodb用户组

groupadd mongodb
useradd -g mongodb -s /usr/sbin/nologin mongodb
chown -R mongodb:mongodb /usr/local/mongodb

#创建配置文件 mongodb.conf

vim /usr/local/mongodb/etc/mongodb.conf

添加如下内容:

dbpath = /usr/local/mongodb/data
logpath = /usr/local/mongodb/logs/mongodb.log

fork = true

#启动mongodb

ln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod
mongod -f /usr/local/mongodb/etc/mongodb.conf

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

mkdir -p /usr/local/mongodb/srv
vim /usr/local/mongodb/srv/mongodb-start

添加下面的内容

#!/bin/sh
mongod -f /usr/local/mongodb/etc/mongodb.conf

vim /usr/local/mongodb/srv/mongodb-stop

添加下面的内容

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

添加执行权限

chmod a+x /usr/local/mongodb/srv/mongodb-start
chmod a+x /usr/local/mongodb/srv/mongodb-stop vim /etc/rc.d/init.d/mongodb

添加下面的内容

#! /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: /usr/local/mongodb/etc/mongodb.conf
# pidfile: /usr/local/mongodb/mongodb.pid PATH=/usr/local/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... "
/usr/local/mongodb/srv/mongodb-start
;;
stop)
echo -n "Stopping MongoDB... "
/usr/local/mongodb/srv/mongodb-stop
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop}" >&2
exit 1
;;
esac
exit 0

添加服务

chmod a+x /etc/rc.d/init.d/mongodb
chkconfig --add mongodb
chkconfig --level 345 mongodb on
service mongodb start // 或 /etc/rc.d/init.d/mongodb start

备份:以前的做法

#设置开机启动

echo "/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/mongodb.log --fork" >> /etc/rc.local

#启动mongodb

/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath /usr/local/mongodb/logs/mongodb.log --fork

#将端口加入防火墙(参考:CentOS6.3编译安装Memcached

注意:数据库端口和Web端口,分别是和,在浏览器中打开 http://localhost:28017,可以看到一些相关信息。

service iptables restart     // 重启防火墙

可能出现的问题:

问题一:

-bash: /usr/local/bin/mongo: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录

解决办法:

yum install ld-linux.so.2

问题二:

mongo: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

解决办法:

yum whatprovides libstdc++.so.6
yum install llibstdc++.so.6

问题三:查看日志的内容如下

Tue Jan  4 09:51:37 MongoDB starting : pid=2794 port=27017 dbpath=/var/lib/mongodb 32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
**       see http://blog.mongodb .org/post/137788967/32-bit-limitations

Tue Jan  4 09:51:37 db version v1.6.5, pdfile version 4.5
Tue Jan  4 09:51:37 git version: 0eb017e9b2828155a67c5612183337b89e12e291
Tue Jan  4 09:51:37 sys info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
**************
old lock file: /var/lib/mongodb /mongod.lock.  probably means unclean shutdown
recommend removing file and running --repair
see: http://dochub.mongodb .org/core/repair for more information
*************
Tue Jan  4 09:51:37 exception in initAndListen std::exception: old lock file, terminating
Tue Jan  4 09:51:37 dbexit:

参考:http://blog.csdn.net/a9529lty/article/details/6456969


安装PHP的MongoDB客户端

官方网站(http://pecl.php.net/package/mongo

cd /usr/local/src
wget -c http://pecl.php.net/get/mongo-1.4.4.tgz
tar -xzvf ./mongo-1.4.4.tgz
cd ./mongo-1.4.4
/usr/local/php/bin/phpize # 利用PHP的 phpize 命令来安装扩展
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

完成后,编辑你 php.ini 文件增加一行。

extension=mongo.so