PHP 5.6 ODBC扩展安装与SQL Server驱动配置

在PHP 5.6环境下安装ODBC扩展的流程,包括编译安装过程中的常见问题解决、SQL Server驱动的安装配置

PHP 5.6 ODBC扩展安装与配置

ODBC扩展安装

编译安装问题分析

编译安装好了PHP5.6之后,发现缺少odbc扩展,于是到PHP源码目录下的ext/odbc目录下编译

1
2
phpize
./configure

这个时候报错:

1
2
checking for Adabas support... cp: cannot stat '/usr/local/lib/odbclib.a': No such file or directory
configure: error: ODBC header file '/usr/local/incl/sqlext.h' not found!

网上搜到的解决方案一般是安装 unixODBC-devel 、 libiodbc-devel 还有 libtool-ltdl-devel。可是都安装好了之后还是这个样子。

由于是在一台无法访问外网的机器上安装的,折腾了半天,也没弄好,猜测可能有些依赖没装好。试了试本地有外网的机器上用docker下载PHP5.6,安装odbc扩展试试,结果还是一样的问题。

后来发现github上这个issues

tianon commented on May 29, 2015 via email That one’s failing at build, not at configure. 😕 tianon commented on May 29, 2015 So, the generated ./configure script has a number of those test “$PHP_XXX” = “no” && PHP_XXX=yes lines. If I comment them all out and run it again, it works and the whole module builds successfully. WUT

大概意思是./configure脚本中,有一些测试的内容,把他们注释掉就可以了

1
2
3
4
5
6
7
8
cd /PATH/php5.6/ext/odbc
phpize
// 重点在这里,用sed命令替换./configure脚本的内容
// 在执行sed命令之前,最好做好备份
sed -ri 's@^ *test +"\$PHP_.*" *= *"no" *&& *PHP_.*=yes *$@#&@g' configure 
./configure --with-unixODBC=shared,/usr
make
make install

到这里odbc扩展就安装成功了。

ODBC安装SQL Server驱动

PHP使用odbc扩展连接SQL Server还需要为ODBC安装SQL Server驱动,参考SQL Server文档

什么是ODBC驱动呢?

1
2
3
4
// odbc连接SQL Server的代码
$connstr = "Driver={SQL Server};Server=$server;Database=$database";

$link=odbc_connect($connstr,$dbusername,$dbpassword);

这里的 Driver={SQL Server}; 就是ODBC驱动,如果未安装驱动则会报错。 [unixODBC][Driver Manager]Can’t open lib ‘ SQL Server’ : file not found

SQL Server的驱动可以在这里找到,注意根据系统进行选择。

由于是 CentOS 7 的服务器,找到 CentOS 7 下只有17版本的驱动,安装之后,驱动部分改为

1
Driver={ODBC Driver 17 for SQL Server};

结果还是不能使用。

想来应该是版本太新了,不支持?想了想CentOS 7 和 RHEL 7是同一家族的,试了试RHEL 7下的msodbcsql-13.0.1.0-1.x86_64.rpm

1
Driver={ODBC Driver 13 for SQL Server};

这回终于完美解决了。

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计