mongodb installation and php extension for centos

 

Step 1: Download the MongoDB installation package

MongoDB's official website: http://www.mongodb.org to find the corresponding version to download.

wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.4.tgz 

tar -zxvf mongodb-linux-x86_64-2.4.4

 

Step 2: Preparation for installation

After we unzip the downloaded compressed package, we will rename it for convenience.

mv mongodb-linux-x86_64-2.4.4 mongodb

 

Then, copy to /usr/local/mongodb

cp -R mongodb/  /usr/local/mongodb

 

(This step can be omitted) Step 3: Create a database folder (the default database file location is /data/db, which will be created automatically at startup)

mkdir -p /datas/mongodb/data

 

Step 4: Self-start at boot (be sure to let mongodb run in the background when it starts, otherwise it may be blocked when centos is started)

Add the mongodb startup item to rc.local to ensure that mongodb starts when the server is powered on. The command is as follows:

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

 

 

Step 5: Start mongodb

run the mongod command

--dbpath execute database storage path (default is /data/db)

--fork runs in Daemon (daemon) mode, in background mode

--logappend Specifies the log generation method (append/overwrite)

Note: If the --fork parameter is specified, the --logpath log file path must be specified

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

 

If the startup is successful, the following information will be displayed:

about to fork child process, waiting until server is ready for connections.

forked process: 10695

all output going to: /datas/mongodb/mongodb.log

child process started successfully, parent exiting

 

使用ps -ef | grep mongodb查看是否存在进程

 

[Linux]进入MongoDB客户端操作,命令如下:

/usr/local/mongodb/bin/mongo ip:端口号 -u用户名 -p密码  (如果没有用户和密码,可以不使用)

默认端口是:27017

命令:/usr/local/mongodb/bin/mongo 127.0.0.1:27017

 

至此,MongoDB 服务器端安装完成,但是如果是要用php去操作MongoDB还要安装他的PHP扩展.

 

 

 

1)下载最新的php mongodb扩展源码,源码可以在http://pecl.php.net/package/mongo下载

wget http://pecl.php.net/get/mongo-1.4.0.tgz 

 

2)解压,进入安装目录

tar -zxvf mongo-1.4.0.tgz 

cd mongo-1.4.0

 

3)进入文件夹后,首先运行phpize来编译扩展的环境

/usr/local/php/bin/phpize 

 

4)运行后,我们运行./configure脚本来进行配置

./configure --with-php-config=/usr/local/php/bin/php-config  # --with-php-config 这个参数是告诉配置脚本 php-config 这个程序的路径

make

make install

 

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

extension=mongo.so

 

6)重启php,看到mongo模块,证明MongoDB的php扩展安装成功。

 

OK ,至此你可以使用php来操作 MongoDB 了

 

一些帮助信息:

在php的mongo扩展中,提供了4类接口(对象):

1,针对mongoDB连接的操作: Mongo

2,针对mongoDB中数据库的操作: MongoDB

3,针对mongoDB中collection的操作: MongoCollection

4,针对查询结果集的操作: MongoCursor

 

以上就是linux中安装 mongodb以及php安装mongodb扩展的步骤

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326946462&siteId=291194637