php连接MongoDB
一、安装php的mongo扩展
# wget https://github.com/mongodb/mongo-php-driver-legacy/archive/master.zip -O /usr/local/src/mongo_php.zip
# unzip mongo_php.zip
# cd mongo-php-driver-legacy-master
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make
# make install
二、配置php添加扩展模块
# vim /usr/local/php/etc/php.ini添加
extension = mongo.so
检查添加是否成功
# /usr/local/php/bin/php -m | grep mongo
重启php-fpm
# /etc/init.d/php-fpm restart
在nginx发布的网站目录中添加一个php脚本,内容如下:
# vim mongo.php
<?php
$m = new MongoClient();
echo "Connection to database successfully";
$db =$m->ceshi;
echo "Database ceshi selected";
?>
然后访问一下:http://www.test.com/mongo.php
出现下面信息说明配置成功
Connection to database successfully Database ceshi selected