CentOS 下 PHP7.2 安装 mssql sqlsrv扩展以链接 SQL SERVER 数据库

piaoling  2023-03-22 15:50:17


 
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
yum install -y msodbcsql mssql-tools unixODBC-devel
修复报错:This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712



1、安装依赖包

yum install gcc-c++
yum install unixODBC-devel


2、下载mssql扩展包

官方地址:http://pecl.php.net/package/pdo_sqlsrv

我选择了5.3.0版本

wget http://pecl.php.net/get/pdo_sqlsrv-5.3.0.tgz

解压

tar -zxvf pdo_sqlsrv-5.3.0.tgz


3、安装扩展

进入文件夹

cd pdo_sqlsrv-5.3.0

执行以下命令

phpize
./configure --with-php-config=/usr/bin/php-config
make && make install


4、加载扩展

cd /etc/php.d
vim pdo.ini

添加一行:extension=pdo_sqlsrv.so

image.png


5、重启php-fpm

systemctl reload php-fpm


6、查看是否加载成功

php -m

看一下是否存在pdo_sqlsrv,如果存在说明安装成功

image.png


或者查看phpinfo(),如果存在pdo_sqlsrv也表示安装成功

image.png


8、安装ODBC驱动

成功安装sqlsrv扩展后,在进行链接SQL Server数据库时报错:

This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712

image.png

提示需要安装ODBC驱动


添加安装源到本机

curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo

卸载部分包,避免冲突

yum remove unixODBC-utf16 unixODBC-utf16-devel

安装msodbcsql17包

ACCEPT_EULA=Y yum install msodbcsql17

至此,终于可以链接SQL SERVER数据库了


扩展:

extension=pdo_sqlsrv
extension=sqlsrv

扩展下载:https://learn.microsoft.com/zh-cn/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver16

https://learn.microsoft.com/zh-cn/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver16


参考:

https://blog.csdn.net/laker_bbs/article/details/121973166

https://cloud.tencent.com/developer/article/2006009

https://www.tytrock.com/topics/357

https://learn.microsoft.com/zh-cn/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver16

类别 :  默认(751)  |  浏览(1787)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: