下载对应版本的PHP源码并解压,我的版本是php-5.4.16

进入源码目录下的ext/mysqlnd,并编译出mysqlnd.so

cp config9.m4 config.m4
sed -ie "s{ext/mysqlnd/php_mysqlnd_config.h{config.h{" mysqlnd_portability.h #修改源文件
phpize #执行phpize,增加./configure 配置
./configure --with-php-config=/path/to/php-config #配置文件为原php的bin下的php-config
sudo make &&sudo make install #编译安装

如果不晓得php-config在哪,可以用这个命令查找

sudo find / -name php-config

进入ext/mysql,编译出mysql.so

phpize
./configure --with-php-config=/path/to/php-config --with-mysql=mysqlnd
make && make install

进入ext/mysqli,编译出mysqli.so

phpize
./configure --with-php-config=/path/to/php-config --with-mysqli=mysqlnd
make && make install

在php.ini中加入

extension=mysqlnd
extension=mysql
extension=mysqli