如何构建PHP 5.6的mysql扩展?

我正在努力并排安装PHP 5.6和7.0,以便于从PHP 5迁移到7.

我使用的是OndřejSurý的Ubuntu PPA,https://launchpad.net/~ondrej/+archive/ubuntu/php,它提供了PHP 5.6和7.0的可共同安装版本.

两个PHP版本并行工作,但有一个障碍:我无法为PHP 5.6构建mysql扩展(即使我能够为7.0构建它).

(我很清楚mysql扩展已经过时了,它应该被放弃以支持更现代的替代方案.但是,对于我来说,期望遗留应用程序依赖它的客户重构代码是不合理的,而PHP 5.6则是仍然支持.所以,我在这件事上没有多少选择……)

对于不熟悉这种可共同安装的PPA的人,版本号会附加到二进制文件中,因此各种PHP可执行文件的路径如下:

# ls -lah /usr/bin | grep "php"
-rwxr-xr-x  1 root   root     13K Dec 31 03:24 dh_php
lrwxrwxrwx  1 root   root      21 Feb 23 09:51 php -> /etc/alternatives/php
-rwxr-xr-x  1 root   root    3.6M Feb 23 06:37 php5.6
-rwxr-xr-x  1 root   root    3.5M Feb 23 06:58 php7.0
lrwxrwxrwx  1 root   root      25 Feb 23 10:06 php-cgi -> /etc/alternatives/php-cgi
-rwxr-xr-x  1 root   root    3.6M Feb 23 06:37 php-cgi5.6
-rwxr-xr-x  1 root   root    3.4M Feb 23 06:58 php-cgi7.0
lrwxrwxrwx  1 root   root      28 Feb 23 10:06 php-config -> /etc/alternatives/php-config
-rwxr-xr-x  1 root   root    4.1K Feb 23 06:37 php-config5.6
-rwxr-xr-x  1 root   root    4.2K Feb 23 06:58 php-config7.0
lrwxrwxrwx  1 root   root      24 Feb 23 10:06 phpize -> /etc/alternatives/phpize
-rwxr-xr-x  1 root   root    4.7K Feb 23 06:37 phpize5.6
-rwxr-xr-x  1 root   root    4.6K Feb 23 06:58 phpize7.0

这是我尝试过的:

# cd ~

# git clone https://git.php.net/repository/pecl/database/mysql.git

# cd mysql

# phpize5.6
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226

# ./configure --with-php-config=/usr/bin/php-config5.6
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php/20131226 -I/usr/include/php/20131226/main -I/usr/include/php/20131226/TSRM -I/usr/include/php/20131226/Zend -I/usr/include/php/20131226/ext -I/usr/include/php/20131226/ext/date/lib
checking for PHP extension directory... /usr/lib/php/20131226
checking for PHP installed headers prefix... /usr/include/php/20131226
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking for MySQL support... yes, shared
checking for specified location of the MySQL UNIX socket... no
checking for the location of libz... no
checking for MySQL UNIX socket location... /var/run/mysqld/mysqld.sock
checking how to print strings... printf
checking for a sed that does not truncate output... (cached) /bin/sed
checking for fgrep... /bin/grep -F
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking for gawk... (cached) gawk
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for sysroot... no
checking for mt... mt
checking if mt is a manifest tool... no
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC -DPIC
checking if cc PIC flag -fPIC -DPIC works... yes
checking if cc static flag -static works... yes
checking if cc supports -c -o file.o... yes
checking if cc supports -c -o file.o... (cached) yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
configure: creating ./config.status
config.status: creating config.h
config.status: executing libtool commands

到现在为止还挺好.

make期间出现问题.我只包含了一部分输出,因为我确信相同的问题会重复出现.

# make
/bin/sh /root/mysql/libtool --mode=compile cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/root/mysql -DPHP_ATOM_INC -I/root/mysql/include -I/root/mysql/main -I/root/mysql -I/usr/include/php/20131226 -I/usr/include/php/20131226/main -I/usr/include/php/20131226/TSRM -I/usr/include/php/20131226/Zend -I/usr/include/php/20131226/ext -I/usr/include/php/20131226/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /root/mysql/php_mysql.c -o php_mysql.lo
libtool: compile:  cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/root/mysql -DPHP_ATOM_INC -I/root/mysql/include -I/root/mysql/main -I/root/mysql -I/usr/include/php/20131226 -I/usr/include/php/20131226/main -I/usr/include/php/20131226/TSRM -I/usr/include/php/20131226/Zend -I/usr/include/php/20131226/ext -I/usr/include/php/20131226/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/mysql/php_mysql.c  -fPIC -DPIC -o .libs/php_mysql.o
In file included from /root/mysql/php_mysql.c:63:0:
/root/mysql/php_mysql_structs.h:114:2: error: unknown type name 'zend_resource'
  zend_resource *default_link;
  ^
/root/mysql/php_mysql_structs.h:115:2: error: unknown type name 'zend_long'
  zend_long num_links,num_persistent;
  ^
/root/mysql/php_mysql_structs.h:116:2: error: unknown type name 'zend_long'
  zend_long max_links,max_persistent;
  ^
/root/mysql/php_mysql_structs.h:117:2: error: unknown type name 'zend_long'
  zend_long allow_persistent;
  ^
/root/mysql/php_mysql_structs.h:118:2: error: unknown type name 'zend_long'
  zend_long default_port;
  ^
/root/mysql/php_mysql_structs.h:122:2: error: unknown type name 'zend_long'
  zend_long connect_errno;
  ^
/root/mysql/php_mysql_structs.h:123:2: error: unknown type name 'zend_long'
  zend_long connect_timeout;
  ^
/root/mysql/php_mysql_structs.h:124:2: error: unknown type name 'zend_long'
  zend_long result_allocated;
  ^
/root/mysql/php_mysql_structs.h:125:2: error: unknown type name 'zend_long'
  zend_long trace_mode;
  ^
/root/mysql/php_mysql_structs.h:126:2: error: unknown type name 'zend_long'
  zend_long allow_local_infile;
  ^
/root/mysql/php_mysql.c:119:2: error: unknown type name 'zend_resource'
  zend_resource *active_result_res;
  ^
/root/mysql/php_mysql.c:414:32: error: unknown type name 'zend_resource'
 static void _free_mysql_result(zend_resource *rsrc)
                                ^
/root/mysql/php_mysql.c:425:40: error: unknown type name 'zend_resource'
 static void php_mysql_set_default_link(zend_resource *link)
                                        ^
/root/mysql/php_mysql.c: In function 'php_mysql_select_db':
/root/mysql/php_mysql.c:372:57: error: request for member 'ptr' in something not a structure or union
    _mysql_result = (MYSQL_RES *)mysql->active_result_res->ptr; \

似乎mysql扩展的源代码不再与PHP 5.6 API兼容.

鉴于mysql扩展在同一服务器上的PHP 7.0下完全编译,人们必须想知道扩展源是否已更新为使用7.0.

我试图找到mysql扩展代码的早期版本,但https://pecl.php.net/package/mysql仅将用户引用到http://git.php.net/?p=pecl/database/mysql.git;a=summary,它似乎没有任何有意义的版本历史记录,例如设计用于PHP的旧分支< 7.0. 我还能尝试什么?也许我忽视了一些愚蠢的事情…… 谢谢你的任何建议! 更新:解决方案 根据接受的答案,当我需要PHP 5.6源代码时,我正在获取PHP 7源代码. 这是解决方案(请务必根据需要调整自己的版本,路径和服务命令):

# git clone --depth=1 -b PHP-5.6 https://git.php.net/repository/php-src.git
# cd ./php-src/ext/mysql
# phpize5.6
#./configure --with-php-config=/usr/bin/php-config5.6
# make
# make install
# printf "; configuration for php mysql module\n; priority=20\nextension=mysql.so" > /etc/php/mods-available/mysql.ini
# ln -s /etc/php/mods-available/mysql.ini /etc/php/5.6/fpm/conf.d/20-mysql.ini
# service php5.6-fpm reload

解决方法:

您正在使用PHP 5.6构建PHP 7扩展源.你必须获取5.6个来源.一种方法可能是从php-src.git或源分发tarball中获取5.6分支,然后在ext / mysql目录中运行phpize.

$git clone -b PHP_5_6 git@git.php.net:/php-src.git php-5.6
$cd php-5.6/ext/mysql
$phpize5.6
上一篇:redhat 5 中文乱码


下一篇:Linux运维发展方向及系统运维技能