我的php环境是5.4,使用mysql时突然出现以下错误:
mysql_connect(): headers and client library minor version mismatch. headers:50508 library:50166
索性换成mysqlnd扩展,下面简单介绍mysqlnd:
mysqlnd(mysql native driver for php)是php5.3后php自带的mysql驱动,官方推荐使用,优势如下:
a.libmysql驱动是由mysql ab公司(现在是oracle公司)编写, 并按mysql license许可协议发布,所以在php中默认是被禁用的.
而mysqlnd是由php官方开发的驱动,以php license许可协议发布,故就规避了许可协议和版权的问题
b.因为mysqlnd内置于php源代码,故你在编译安装php时就不需要预先安装mysql server也可以提供mysql client api (mysql_connect, pdo , mysqli), 这将减化一些工作量.
c. mysqlnd是专门为php优化编写的驱动,它使用了php本身的特性,在内存管理,性能上比libmysql更有优势. php官方的测试是:libmysql将每条记录在内存中保存了两份,而mysqlnd只保存了一份
d. 一些新的或增强的功能
增强的持久连接
引入特有的函数mysqli_fetch_all()
引入一些性能统计函数 mysqli_get_cache_stats(), mysqli_get_client_stats(),
mysqli_get_connection_stats(),
使用上述函数,可很容易分析mysql查询的性能瓶颈!
ssl支持(从php 5.3.3开始有效)
压缩协议支持
命名管道支持(php 5.4.0开始有效)
如何使用mysqlnd驱动?
传统的编译php时带如下参数:
--with-mysql=/usr/local/mysql
--with-pdo-mysql=/usr/local/mysql
查看编译参数:
[root@localhost php54]#./configure --help | grep mysql --with-mysql=dir include mysql support. dir is the mysql base mysqlnd the mysql native driver will be used --with-mysql-sock=sockpath mysql/mysqli/pdo_mysql: location of the mysql unix socket pointer. --with-mysqli=file include mysqli support. file is the path to mysql_config. if no value or mysqlnd is passed --enable-embedded-mysqli mysqli: enable embedded support --with-pdo-mysql=dir pdo: mysql support. dir is the mysql base directory if no value or mysqlnd is passed as dir, the --enable-mysqlnd enable mysqlnd explicitly, will be done implicitly --disable-mysqlnd-compression-support disable support for the mysql compressed protocol in mysqlnd --with-zlib-dir=dir mysqlnd: set the path to libz install prefix
可以看到只要不指定,默认就是mysqlnd。于是重新编译php:
./configure --prefix=/usr/local/php54 --with-config-file-path=/usr/local/php54/etc --with-zlib --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --with-curlwrappers --enable-fpm --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap--with-mysql --with-pdo-mysql --with-mysqli
原文地址:php使用mysqlnd驱动, 感谢原作者分享。