因业务需要,需要连接其它系统所使用的Microsoft SQL Server数据库,然后要使用PDO ODBC才能连接,但现在所使用的PHP环境没有装这个扩展,经过一系列踩坑,整理出了下面的教程。
安装unixODBC
unixODBC是一个数据库连接组件,安装pdo_odbc
前需要安装这个,否则安装pdo_odbc
时会出现Cannot find header file(s) for pdo_odbc
错误
yum install unixODBC unixODBC-devel libtool-ltdl libtool-ltdl-devel
转到pdo_odbc源代码目录
从PHP5.1开始,PDO_ODBC包含在PHP源代码中。可以使用configure命令将PDO_ODBC扩展编译为静态或共享模块。
所以我们只需要转到pdo_odbc源代码目录进行编译,而不用再去另外下载了。
cd /www/server/php/56/src/ext/pdo_odbc
make clean
如果你在尝试这篇教程直接,尝试过其它网上的方法,先执行下以下命令清除缓存,否则加载扩展时可能会出现PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) '/www/server/php/56/lib/php/extensions/no-debug-non-zts-20131226/pdo_odbc.so' in Unknown on line 0
make clean
phpize
phpize 命令是用来准备 PHP 扩展库的编译环境的,主要是根据系统信息生成对应的configure文件。
/www/server/php/56/bin/phpize
configure
./configure --with-php-config=/www/server/php/56/bin/php-config --with-pdo-odbc=unixODBC,/usr/
执行结果
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/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... /www/server/php/56
checking for PHP includes... -I/www/server/php/56/include/php -I/www/server/php/56/include/php/main -I/www/server/php/56/include/php/TSRM -I/www/server/php/56/include/php/Zend -I/www/server/php/56/include/php/ext -I/www/server/php/56/include/php/ext/date/lib
checking for PHP extension directory... /www/server/php/56/lib/php/extensions/no-debug-non-zts-20131226
checking for PHP installed headers prefix... /www/server/php/56/include/php
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 ODBC v3 support for PDO... yes, shared
checking for PDO includes... checking for PDO includes... /www/server/php/56/include/php/ext
checking for selected PDO ODBC flavour... unixODBC
libs /usr//lib,
headers /usr//include
checking for odbc.h in /usr//include... no
checking for odbcsdk.h in /usr//include... no
checking for iodbc.h in /usr//include... no
checking for sqlunix.h in /usr//include... no
checking for sqltypes.h in /usr//include... yes
checking for sqlucode.h in /usr//include... yes
checking for sql.h in /usr//include... yes
checking for isql.h in /usr//include... no
checking for sqlext.h in /usr//include... yes
checking for isqlext.h in /usr//include... no
checking for udbcext.h in /usr//include... no
checking for sqlcli1.h in /usr//include... no
checking for LibraryManager.h in /usr//include... no
checking for cli0core.h in /usr//include... no
checking for cli0ext.h in /usr//include... no
checking for cli0cli.h in /usr//include... no
checking for cli0defs.h in /usr//include... no
checking for cli0env.h in /usr//include... no
checking for SQLBindCol in -lodbc... yes
checking for SQLAllocHandle in -lodbc... yes
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
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 dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... 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
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
make
make && make install
执行结果
/bin/sh /www/server/php/56/src/ext/pdo_odbc/libtool --mode=link cc -DPHP_ATOM_INC -I/www/server/php/56/src/ext/pdo_odbc/include -I/www/server/php/56/src/ext/pdo_odbc/main -I/www/server/php/56/src/ext/pdo_odbc -I/www/server/php/56/include/php -I/www/server/php/56/include/php/main -I/www/server/php/56/include/php/TSRM -I/www/server/php/56/include/php/Zend -I/www/server/php/56/include/php/ext -I/www/server/php/56/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -o pdo_odbc.la -export-dynamic -avoid-version -prefer-pic -module -rpath /www/server/php/56/src/ext/pdo_odbc/modules pdo_odbc.lo odbc_driver.lo odbc_stmt.lo -Wl,-rpath,/usr//lib -L/usr//lib -lodbc
cc -shared .libs/pdo_odbc.o .libs/odbc_driver.o .libs/odbc_stmt.o -L/usr//lib -lodbc -Wl,-rpath -Wl,/usr//lib -Wl,-soname -Wl,pdo_odbc.so -o .libs/pdo_odbc.so
creating pdo_odbc.la
(cd .libs && rm -f pdo_odbc.la && ln -s ../pdo_odbc.la pdo_odbc.la)
/bin/sh /www/server/php/56/src/ext/pdo_odbc/libtool --mode=install cp ./pdo_odbc.la /www/server/php/56/src/ext/pdo_odbc/modules
cp ./.libs/pdo_odbc.so /www/server/php/56/src/ext/pdo_odbc/modules/pdo_odbc.so
cp ./.libs/pdo_odbc.lai /www/server/php/56/src/ext/pdo_odbc/modules/pdo_odbc.la
PATH="$PATH:/sbin" ldconfig -n /www/server/php/56/src/ext/pdo_odbc/modules
----------------------------------------------------------------------
Libraries have been installed in:
/www/server/php/56/src/ext/pdo_odbc/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /www/server/php/56/lib/php/extensions/no-debug-non-zts-20131226/
查看so文件是否生成
ls -lh /www/server/php/56/lib/php/extensions/no-debug-non-zts-20131226/
total 2.1M
-rwxr-xr-x 1 root root 1.3M Sep 3 15:13 opcache.a
-rwxr-xr-x 1 root root 610K Sep 3 15:13 opcache.so
-rwxr-xr-x 1 root root 126K Sep 3 15:13 pdo_odbc.so
配置php.ini
大概在配置文件九百多行的位置新建一行,加入以下代码
extension=/www/server/php/56/lib/php/extensions/no-debug-non-zts-20131226/pdo_odbc.so
重启php和httpd环境
重启php-fpm和nginx等
查看phpinfo()
查看phpinfo(),搜索PDO_ODBC,如果找到基本就是安装成功。
安装了扩展,还要在服务器安装数据库连接驱动才能连接SQL Server数据库,可以看我的下一篇文章。