1.1 Installing Microsoft Sources

$ curl -s -o /tmp/prod.repo https://packages.microsoft.com/config/rhel/7/prod.repo

1.2 Prevent conflicts by uninstalling the original version first (optional)

$ yum remove -y unixODBC 

1.3 Install the driver (all three should be installed, one cannot be missing)

$ yum install -y msodbcsql mssql-tools unixODBC-devel

II. Compile pdo_sqlsrv plugin

2.1 Download pdo_sqlsrv extension package

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

2.2 Decompress and compile

$ tar xf pdo_sqlsrv-5.9.0.tgz
$ cd pdo_sqlsrv-5.9.0

2.3 Pre-compilation

$ /usr/bin/phpize
$ . /configure --with-php-config=/usr/bin/php-config

2.4 Compile and install

$ make && make install

III. Adding the pdo_sqlsrv extension

This article is about the installation of php and php-fpm using yum, the path to the relevant configuration file may not be the same as the environment of the gods (find the relevant configuration file according to your own environment).

  • Add the pdo_sqlsrv.so extension to the /etc/php.ini configuration file.
extension=pdo.so
extension=pdo_sqlsrv.so

pdo_sqlsrv.so is a sqlserver extension and needs to be loaded before the pdo.so extension, otherwise the following error will occur

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pdo_sqlsrv.so' - /usr/lib64/php/modules/pdo_sqlsrv.so: undefined symbol: php_pdo_register_driver in Unknown on line 0
  • Restart the php-fpm service
$ systemctl restart php-fpm

IV. Verify that the extension is installed correctly

  • Verify that the extension is successfully installed
$ php -m | grep pdo_sqlsrv

php-pdo_sqlsrv-1.png

Here you can see that the pdo_sqlsrv extension has been added to php.

But then there is a PHP Warning: Module 'PDO' already loaded in Unknown on line 0 Warning message.

After some troubleshooting, I found that the pdo.so extension is also referenced in the /etc/php.d/pdo.ini configuration file.

php-pdo_sqlsrv-2.png

Comment out the pdo.so extension referenced in /etc/php.d/pdo.ini and leave only the extension in /etc/php.ini.

After commenting, restart php-fpm and recheck the php extensions, and the Warning message will not appear.

php-pdo_sqlsrv-3.png

  • Viewing PHP information on the web side

php-pdo_sqlsrv-4.png