如何为 PHP 5.6 构建 mysql 扩展?
How does one build the mysql extension for PHP 5.6?
我正在并排安装 PHP 5.6 和 7.0,以便于从 PHP 5 迁移到 7。
我正在使用 Ondřej Surý 的 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 refers users only to 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 或源分发压缩包中获取 5.6 分支,然后 运行 phpize 在 ext/mysql 目录中。
$ git clone -b PHP_5_6 git@git.php.net:/php-src.git php-5.6
$ cd php-5.6/ext/mysql
$ phpize5.6
我正在并排安装 PHP 5.6 和 7.0,以便于从 PHP 5 迁移到 7。
我正在使用 Ondřej Surý 的 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 refers users only to 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 或源分发压缩包中获取 5.6 分支,然后 运行 phpize 在 ext/mysql 目录中。
$ git clone -b PHP_5_6 git@git.php.net:/php-src.git php-5.6
$ cd php-5.6/ext/mysql
$ phpize5.6