略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 安装

2024-04-28

安装

Warning

This extension is deprecated. Instead, the MongoDB extension should be used.

MongoDB 的 PHP 驱动程序可以工作在几乎任何系统上:Windows、Mac OS X、Unix 和 Linux;大端或小端字节序(little/big-endian);32位和64位的机器;PHP 5.3-5.6(1.6之前的版本同时支持 PHP5.2)。

» PECL 扩展未与 PHP 捆绑。

手动安装

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

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

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

  • 确保 extension_dir 变量指向了 mongo.so 的位置。 编译时会显示安装 PHP 驱动的位置,比如输出:

    Installing '/usr/lib/php/extensions/no-debug-non-zts-20060613/mongo.so'
    确保和运行的 PHP 是同一个扩展目录:
    $ php -i | grep extension_dir
      extension_dir => /usr/lib/php/extensions/no-debug-non-zts-20060613 =>
                       /usr/lib/php/extensions/no-debug-non-zts-20060613
    如果不一致,则需要修改 php.ini 里的 extension_dir,或者把 mongo.so 移过去。
  • 要在 PHP 启动的时候加载这个扩展,添加一行:

    extension=mongo.so

在 *NIX 上安装

执行:

$ sudo pecl install mongo

将以下内容添加到 php.ini 文件:

extension=mongo.so

如果 pecl 运行时超出了内存限制,请确认在 php.ini 中的 memory_limit 的设置至少有 128MB。

在 Windows 上安装

针对不同线程安全、VC版本的 PHP 发行版,可从 » PECL 获取到预编译的二进制文件。 解压,并把 php_mongo.dll 放到 PHP 扩展目录(默认是 “ext”)。

将以下内容添加到 php.ini 文件:

extension=php_mongo.dll

Note: 为 Windows 用户添加额外的依赖 DLL

为了使此扩展生效, DLL 文件必须能在 Windows 系统的 PATH 指示的路径下找到。如何操作的信息,请参见题为“如何在 Windows 中将 PHP 目录加到 PATH 中”的FAQ。虽然将 DLL 文件从 PHP 文件夹复制到 Windows 系统目录也行,但不建议这样做。 此扩展需要下列文件在 PATH 路径中: libsasl.dll

OS X

大部分情况下,从 pecl 安装最简单:

$ sudo pecl install mongo

如果用的是 » Homebrew,PHP 包含了驱动安装的方案。比如,安装 PHP 5.6 的驱动,可以使用以下命令:

$ brew install php56-mongo

如果使用的是 » XAMPP,请注意它有自己的 pecl 二进制文件和 php.ini 配置。 你可以通过以下命令安装驱动:

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

Note: 在 OS X 上编译时的 Xcode 依赖

在 OS X 上编译驱动需要 Xcode 开发工具,可以通过 xcode-select --install 安装。 如果命令无效,也许应该先安装 » Command Line Tools 包。

Gentoo

Gentoo 有一个 PHP 驱动的包,叫做 dev-php/pecl-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 下也会产生这个问题。)

第三方安装说明

很多人撰写了安装 PHP 驱动的极好教程。

add a note add a note

User Contributed Notes 6 notes

up
10
cap at unagon dot com
9 years ago
For Debian users: apt-get install php5-dev will install phpize for you.
up
3
bradley dot henke at colorado dot edu
6 years ago
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!
up
2
Andrey
8 years ago
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.
up
0
Hanakaze
6 years ago
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
up
-2
poorpuer at gmail dot com
8 years ago
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.
up
-11
brick
4 years ago
Apache 2.4.10 win32 from apachelounge with php 5.6.4 VC11 x86 Thread Safe on windows7 sp1.
The 5.6 Thread Safe (TS) x86 mongo.dll i downloaded from http://pecl.php.net/package/mongo/1.5.8/windows
I kept getting the following error
PHP Startup: Unable to load dynamic library mongo.dll
The specified module could not be found.
I fixed this by adding the php and the php/ext to the windows path and rebooting(log off didn't cut it).

官方地址:https://www.php.net/manual/en/mongo.installation.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3