centos7 php5.5 mongodb安装
1、下载最新php MongoDB扩展源码
https://pecl.php.net/package/mongodb
最新的1.6不支持PHP5.5,得用老版本,1.5.5
wget https://pecl.php.net/get/mongodb-1.5.5.tgz tar -xzvf mongodb-1.5.5.tgz cd mongodb-1.5.5
2、老三套
phpize ./configure --with-php-config=/usr/bin/php-config #####error:Cannot find OpenSSL's <evp.h> yum install openssl openssl-devel make && make install
在php.ini里加扩展
vim /etc/php.ini
extension=mongodb.so
3、安装MongoDB
添加源,mongodb-1.5.5扩展,最高支持到MongoDB 4.0
vim /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=0 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
安装
yum install mongodb-org
启动服务
systemctl start mongod.service
开机自启动
chkconfig mongod on
创建数据库
mongo
use DATABASE_NAME
为数据库创建用户
db.createUser({user:"username123",pwd:"123456",roles:[{role:"dbOwner",db:"DATABASE_NAME"}]})
验证用户
db.auth("username123","123456")
4、php测试连接数据库
$manager = new MongoDB\Driver\Manager("mongodb://username123:123456@127.0.0.1"); $bulk = new MongoDB\Driver\BulkWrite(); $bulk->insert(array( 'id' => 2, 'lng' => '456.456', 'lat' => 9999.000, )); try { $result = $manager->executeBulkWrite('db.GPS', $bulk); var_dump($result->getInsertedCount()); } catch (MongoDB\Driver\Exception\BulkWriteException $e) { var_dump($e->getWriteResult()->getWriteErrors()); } $filter = ['id' => ['$gte' => 1]]; $options = [ 'sort' => ['id' => 1], ]; $query = new MongoDB\Driver\Query($filter, $options); $cursor = $manager->executeQuery('db.GPS', $query); foreach ($cursor as $document) { print_r($document); }
mongo