©
本文檔使用 php中文網手册 發布
mysqli扩展在PHP5.0.0中被引入。Mysql Native驱动在PHP5.3.0版本中被引入。
通用的Unix分发包中会包含PHP可安装的二进制版本。虽然这些二进制版本通常会被构建为支持启用mysql扩展的, 蚕食扩展库自身可能需要依赖其他附加包的安装。因此对包管理的检查会比选择可用的发行版本更重要。
除非你的Unix分发包包含的PHP二进制包的mysqli扩展可用,否则你就需要 从源码构建PHP。如果你希望使用mysql扩展,从PHP源代码构建允许你这样做,并且可以指定每个扩展 使用什么客户端库。
使用Mysql Native Driver 是推荐的选项,会提升性能并且会带来一些Mysql Client Library不允许的访问特性。 什么是Mysql自然驱动器?中对Mysql Native Driver 的优势做了一个简要概述。
/path/to/mysql_config代表了要用来编译mysql客户端/连接mysql服务端的mysql_config程序位置。
PHP 版本 | 默认 | 配置选项: mysqlnd | 配置选项: libmysql | 更新日志 |
---|---|---|---|---|
5.0.x, 5.1.x, 5.2.x | libmysql | 不适用 | --with-mysqli=/path/to/mysql_config | |
5.3.x | libmysql | --with-mysqli=mysqlnd | --with-mysqli=/path/to/mysql_config | mysqlnd is now supported |
5.4.x | mysqlnd | --with-mysqli | --with-mysqli=/path/to/mysql_config | mysqlnd is now the default |
请注意Mysql扩展和客户端库是可以自由混合的。比如,可以使用libmysql这个mysql客户端库来启用Mysql扩展,使用 Mysql Native Driver来配置mysqli扩展。所有的扩展和客户端库的顺序都是可能的。
下面的示例使用Mysql Client Library(libmysql)构建了mysql扩展,并且mysqli和 PDO Mysql扩展使用Mysql Native Dirver(作为客户端库):
./configure --with-mysql=/usr/bin/mysql_config \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd [other options]
在windows上,PHP通常使用二进制安装包进行安装。
当PHP安装后,如果期望使用则可以通过一些配置来开启mysqli并指定客户端库。
mysqli扩展默认并不会开启,因此php.ini中php_mysqli.dll这个DLL 必须开启。为此你需要找到php.ini文件(通常在c:\php),并且你需要确认删除了 [PHP_MYSQLI]部分中的extension=php_mysqli.dll一行行首的注释符号(分号)。
另外,如果你希望使用Mysql client library(libmysql)作为mysqli的驱动器,你需要确保PHP可以访问 客户端库的文件。Mysql Client Library作为文件libmysql.dll包含在windows的PHP分发包中。 这个文件需要在windows系统的PATH环境变量中才可以被成功加载。查看FAQ文档 怎样增加我的PHP目录到Windows的PATH中了解怎样配置环境变量PATH。 把libmysql.dll拷贝到Windows的系统目录(通常是c:\Windows\system)也是适用的, 通常系统目录默认就在系统的PATH环境变量下。然而,我们强烈反对这种做法。
就像开启任何PHP扩展(比如php_mysqli.dll),PHP指令 extension_dir可以被设置为PHP扩展位置的目录路径。 查看windows安装介绍手册。关于此的一个例子是PHP 5中 extension_dir指令的值是c:\php\ext。
Note:
如果启动web服务器的时候发生了类似下面这样的错误"Unable to load dynamic library './php_mysqli.dll'", 这就是因为在系统中无法找到php_mysqli.dll或者libmysql.dll。
在windows上,对于PHP 5.3或更新版本,mysqli扩展默认开启并使用Mysql Native Driver。 这就是说你不需要担心关于访问libmysql.dll的配置。
[#1] Davinken [2015-06-17 16:20:55]
On CentOS 6 php-mysqli has been already incorporated into the php-mysql RPM package.
[#2] Anonymous [2014-10-16 05:25:34]
Thank you to whoever included the installation for cent OS :)
[#3] Anonymous [2013-06-27 14:23:57]
yum install php-mysqli
--centos Linux 2.6.18-308.el5
[#4] Mark F [2011-03-14 01:09:06]
Just to note with regards to SSL and compression. MySQLnd and thus extensions using mysqlnd such as mysqli... now supports SSL and compression. Both have been supported inside mysqlnd since PHP 5.3.3 - http://www.php.net/manual/en/mysqlnd.overview.php
[#5] arekm [2010-08-24 05:49:52]
"using the MySQL Native Driver results in improved performance and gives access to features not available when using the MySQL Client Library"
Of course there are also disadvantages:
- no SSL support (a nightmare!)
- no mysql < 4.1 support
So some people cannot use mysqlnd :(
[#6] josh dot ribakoff at gmail dot com [2009-11-10 22:21:58]
I kept getting configure errors:
/tmp/cc4f2PKd.o: In function `main':
/usr/src/php-5.3.0/configure:14287: undefined reference to `yp_get_default_domain'
collect2: ld returned 1 exit status
configure: failed program was:
#line 14270 "configure"
#include "confdefs.h"
The solution was to configure with mysql native driver as outlined on this page.
[#7] David dot Kit dot Friedman at gmail dot com [2009-08-19 16:04:51]
>On Windows, PHP is most commonly installed using the binary installer. Once PHP has been installed, some >configuration is required to enable mysqli and specify the client library you want it to use.
Basically, if you want to add extensions you can double-click again on the installer file. The installer will find the already installed PHP and will ask if you want to change the installation.
When you go through the prompts it will take you back to the list of extensions and you can pick which extensions you want to add.
For the mysqli extension the installer also edited php.ini so that the extension was enabled:
[PHP_MYSQLI]
extension=php_mysqli.dll
the installer added those lines to the end of the file.