PHP7 mongo 操作

首先说一下扩展,mongodb提供了两种,php5和php7,所以选择了php7的扩展。

使用pcel安装mongoDB扩展:

1,首先到PHP7的安装目录,我的安装目录是:

$ /Applications/MAMP/bin/php/php7.1.1/bin/pecl install mongodb

 执行成功后的结果:

yanruicngdeiMac:mongodb-1.4.3 yanruicheng$ /Applications/MAMP/bin/php/php7.1.1/bin/pecl install mongodb
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading mongodb-1.5.3.tgz ...
Starting to download mongodb-1.5.3.tgz (1,059,580 bytes)
.........................done: 1,059,580 bytes
426 source files, building
running: phpize
Configuring for:
PHP Api Version: 20160303
Zend Module Api No: 20160303
Zend Extension Api No: 320160303
building in /private/tmp/pear/temp/pear-build-yanruicheng5hrI3C/mongodb-1.5.3

.......

Build process completed successfully
Installing '/Applications/MAMP/bin/php/php7.1.1/lib/php/extensions/no-debug-non-zts-20160303/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.5.3
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongodb.so" to php.ini

/**********结束**********/

2,然后打开php.ini文件,添加extension=mongodb.so配置 或者执行命令:$ echo "extension=mongodb.so" >> `/usr/local/php7/bin/php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

配置就完成了,phpinfo里面可以看到mongodb。

3,当上面两步骤完成之后,打印一下

print_r(get_extension_funcs('mongodb'));
print_r(get_declared_classes());

看看支持那些方法。

4,连接mongodb,如下图:

 

 

解释一下,27017是mongodb的默认端口,可以直接在浏览器上访问的,以下是访问的显示内容:

5,mac 终端启动mongodb的命令是:

mongod --config /usr/local/etc/mongod.conf

 界面是:

说明:这一篇没有涉及安装mongodb,更多关于mongodb的操作以后我再写,有问题的小伙伴欢迎评论哦!

 

posted @ 2018-11-21 13:41  yahn~  阅读(807)  评论(0编辑  收藏  举报