关于php:linux环境下php安装sqlsrv扩展连接mssql

89次阅读

共计 1922 个字符,预计需要花费 5 分钟才能阅读完成。

以下示例以 CentOs7.8 + php7.3 为例,可依据理论状况切换其余版本,留神版本之间的对应关系即可

第一步,装置 Microsoft ODBC driver for SQL Server (Linux)

通过 https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16 查问到反对 php7.3 版本的 Microsoft Drivers 版本为 5.6 和 5.8,这里抉择 5.6, 对应的 DOBC driver 版本 抉择 ODBC 17

sudo su
#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version

#Red Hat Enterprise Server 7 and Oracle Linux 7
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo

#Red Hat Enterprise Server 8 and Oracle Linux 8
#curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo

exit
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel #to avoid conflicts
sudo ACCEPT_EULA=Y yum install -y msodbcsql18
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y yum install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo yum install -y unixODBC-devel

第二步,下载 Microsoft Drivers for PHP for SQL Server

下载地址(5.6):
https://github.com/microsoft/msphpsql/releases/download/v5.6.1/CentOS7-7.3.tar
解压文件:

第三步,装置 sqlsrv 扩大

  1. 查看以后装置的 PHP 版本的 Thread Safety

    php -i | grep "Thread Safety"
    # Thread Safety => enabled

    enabled 代表线程平安,应用 php_sqlsrv_73_ts.sophp_pdo_sqlsrv_73_ts.so
    disabled 代表非线程平安,应用 php_sqlsrv_73_nts.sophp_pdo_sqlsrv_73_nts.so

  2. 将对应的扩大文件复制到 php 扩大目录外面
    查看扩大目录

    php -i | grep "extension_dir"

    将文件上传到扩大目录
    能够应用 xshell 拖动,或者 ftp 工具,此处略。

  3. 编辑 php.ini 文件,减少以下配置

    extension=php_sqlsrv_73_ts
    extension=php_pdo_sqlsrv_73_ts
  4. 重启 php-fpm

    sudo /etc/init.d/php-fpm restart

参考文档:

https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16
https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver16
https://github.com/microsoft/msphpsql/releases/tag/v5.6.1

正文完
 0