安装/配置
在线手册:中文  英文

安装

MongoDB的PHP驱动程序可以工作在几乎任何系统上:Windows、Mac OS X、Unix和Linux;大众和罕见的机器;32位和64位的机器;PHP 5.1、5.2和5.3。

» PECL 扩展未与 PHP 捆绑。 这个页面包含了安装在不同系统上的更多具体信息,以及用户遇上的问题和疑难解答。

在*NIX上安装

执行:


$ sudo pecl install mongo

如果你使用 CentOS 或者 Redhat, 可以使用 Csoke Arpad 创建的 » 这些发行版的 RPM

将以下行添加到 php.ini:


extension=mongo.so

如果 pecl 运行时超出了内存限制,请确认在 php.ini 中的 memory_limit 设置不小于 32MB。

普通安装

驱动开发人员和对最新 bug 修复感兴趣的人,可以从 » Github 上获取最新源码来编译驱动。 前往 Github 并点击 "download" 按钮。然后运行:


$ tar zxvf mongodb-mongodb-php-driver-<commit_id>.tar.gz
$ cd mongodb-mongodb-php-driver-<commit_id>
$ phpize
$ ./configure
$ sudo make install

并按以下说明修改 php.ini:

OS X

如果你的系统无法找到 autoconf,你需要安装 Xcode(在你的安装DVD里,或者在苹果网站上免费下载)。

如果你在使用 XAMPP,你也许可以通过以下指令编译驱动:


sudo /Applications/XAMPP/xamppfiles/bin/pecl install mongo

如果你正在使用 MAMP(或者是 XAMPP,并且上面的命令没起作用),已经编译好的二进制文件可以从 » Github 下载(下载名称含有「osx」、匹配你 PHP 版本的那个)。从压缩包中解压 mongo.so 并添加它到 MAMP 或 XAMPP的扩展目录。 添加


      extension=mongo.so

到使用的 php.ini 文件并重启服务。

Gentoo

Gentoo 有一个 PHP 驱动的包,叫做 dev-php5/mongo,可以通过以下命令安装:


$ sudo emerge -va dev-php5/mongo

如果你使用了 PECL,你可能得到 libtool 版本不正确的错误。 从源码编译,你需要运行 aclocal 和 autoconf。


$ phpize && aclocal && autoconf && ./configure && make && make install

Red Hat

这包含了 Fedora 和 CentOS。

这些系统上默认的 Apache 设置禁止请求产生网络连接,意味着当连接到数据库,驱动会得到一个 "Permission denied" 错误。当你遇上这个问题,可以试试运行:

$ /usr/sbin/setsebool -P httpd_can_network_connect 1 
然后重启 Apache。(在 SELinux 下也会产生这个问题。)

在 Windows 上安装

已编译好的每个版本的二进制文件可以从 » Github 上获取到,包括了不同版本、线程安全、 VC 库。 解压并把 php_mongo.dll 放到你的 PHP 扩展目录(默认是 "ext")。

最新的(非发布的)代码会在每次提交的时候编译成 Windows 二进制文件。Zip 包含有 php_mongo.dll 和 一个 version.txt。 请保留你的 version.txt,这样当你遇上问题时,你可以告诉开发人员你所使用的版本。(数字很长并且看上去无意义的,但是对开发人员是有意义的!)

获取最新的漏洞修复(以及可能的漏洞),下载对应你安装的 PHP 版本的二进制文件:

并添加下面这行到你的 php.ini 文件:


extension=php_mongo.dll

第三方安装说明

很多人创建了安装 PHP 驱动的极好教程。


安装/配置
在线手册:中文  英文

用户评论:

bradley dot henke at colorado dot edu (2013-04-23 06:36:52)

Instructions for Manually Installing MongoDB for PHP for use with "XAMPP for Linux":
There were two gotchas that I ran into. "XAMPP for Linux" needs 32 bit libraries, and the php API version must match the php API version of "XAMPP for Linux".
Hopefully this will save someone else time figuring this all out!
1) Install "XAMPP for Linux" AND the "XAMPP for Linux Development Files".
2) Grab the source code from github (in the manual instructions above)
$ tar zxvf mongodb-mongodb-php-driver-<commit_id>.tar.gz
$ cd mongodb-mongodb-php-driver-<commit_id>
There are now some smalls deviation from the manual instructions...
3) PHPize
$ sudo /opt/lampp/bin/phpize
4) Configure
$ ./configure
5) Make changes to the Makefile (XAMPP requires a 32-bit compilation)
- Choose your favorite text editor and open "Makefile"
- Make the following changes:
Line 20: prefix = /opt/lampp
Line 23: prefix = /opt/lampp
Line 25: phpincludedir = /opt/lampp/include/php
Line 27: CFLAGS = -g -O2 -m32
Line 34: EXTENSION_DIR = /opt/lampp/lib/php/extensions/no-debug-non-zts-20100525/
Line 35: PHP_EXECUTABLE = /opt/lampp/bin/php
Make sure you replace </path/to/mongo-php-driver-master>
Line 38: INCLUDES = -I/opt/lampp/include/php -I/opt/lampp/include/php/main -I/opt/lampp/include/php/TSRM -I/opt/lampp/include/php/Zend -I/opt/lampp/include/php/ext -I/opt/lampp/include/php/ext/date/lib -I</path/to/mongo-php-driver-master>/util -I</path/to/mongo-php-driver-master>/exceptions -I</path/to/mongo-php-driver-master>/gridfs -I</path/to/mongo-php-driver-master>/types -I</path/to/mongo-php-driver-master>/mcon
6) Compile
$ sudo make all
7) Copy the static object into the extension directory
$ sudo cp modules/mongo.so /opt/lampp/lib/php/extensions/no-debug-non-zts-20100525/
9) Update your php.ini file
- Grab you favorite text editor and open /opt/lampp/etc/php.ini (you'll probably need to use sudo).
- Add the line: extension=mongo.so
10) Restart "XAMPP for Linux". You should now be able to see Mongo listed in your phpinfo() page! Installation Complete!!!
Good Luck!

Hanakaze (2013-01-12 18:18:05)

To install the Mongo driver on Windows 7 x64 with EasyPHP, you have to use the x86 driver (instead of x64) and non nts :
php_mongo-1.3.2RC1-5.4-vc9.dll actually

tiago dot braga at gmail dot com (2011-08-08 09:04:28)

In Windows running IIS the directory to instal is "C:\Program Files\PHP\ext". Than you have to install the apropriate MongoDriver version (in my case was mongo-1.1.4-php5.3vc9) and than Restart IIS.
The MongoDB Driver will be available.

rishi at plavaga dot in (2011-06-27 01:25:31)

I am using XAMPP 1.7.4 (32 bit) on Windows 7. Though it was mentioned that Apache uses a non-threadsafe version of DLL, when used, it was giving me the below error:
PHP Warning: PHP Startup: Unable to load dynamic library
Warning: PHP Startup: Unable to load dynamic library 'D:\xampp\php\ext\php_mongo.dll' - The specified module could not be found.
I had to use the thread-safe version (mongo-1.1.4.zip\mongo-1.1.4-php5.3vc6ts\php_mongo.dll) to get it working.

Andrey (2011-06-01 16:54:44)

On windows installation check for VC compilator version inside phpinfo(), if you install incorrect one it wouldn't work.
I tried to run on Apache2 with VC6, and always get folowing error:
Fatal error: Class 'Mongo' not found
Until I look into phpinfo and found MSVC9, after that
I install VC9 dll files and it solve the problem.

poorpuer at gmail dot com (2010-10-26 08:26:37)

If you're running IIS under Windows 7 and have PHP manager installed you'll also need to manually enable the extension. Start IIS Manager, select PHP Manager, under the section labeled PHP Extensions click on Enable or disable an extension, then enable the php_mongo.dll extension, and restart IIS.

cap at unagon dot com (2010-02-26 13:52:55)

For Debian users: apt-get install php5-dev will install phpize for you.

易百教程