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

以下示例以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
解压文件:
linux环境下php安装sqlsrv扩展连接mssql_第1张图片

第三步,安装 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
linux环境下php安装sqlsrv扩展连接mssql_第2张图片
linux环境下php安装sqlsrv扩展连接mssql_第3张图片

你可能感兴趣的:(php数据库linux)