Skip to content

Instantly share code, notes, and snippets.

@RatserX
Last active December 20, 2021 20:04
Show Gist options
  • Save RatserX/8a5fa160e4445a41093ae905fae6a9e2 to your computer and use it in GitHub Desktop.
Save RatserX/8a5fa160e4445a41093ae905fae6a9e2 to your computer and use it in GitHub Desktop.
Setup the Microsoft ODBC Driver for SQL Server on CentOS/RHEL with different PHP versions

Setup the Microsoft ODBC Driver for SQL Server on CentOS/RHEL with different PHP versions

Prerequisites

Update the installed packages.

yum update

Install the REMI repository.

# CentOS/RHEL 6
rpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-6.rpm
# CentOS/RHEL 7
rpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-7.rpm

Check that the repositories are correctly installed.

yum repolist

Step 1 - Install the PHP versions

In this case, PHP 7.0 and 7.1 will be installed.

# PHP 7.0
yum install php70 php70-cli
# PHP 7.1
yum install php71 php71-cli

Step 2 - Install the Microsoft ODBC Driver for SQL Server

sudo su
curl https://packages.microsoft.com/config/rhel/7/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 msodbcsql
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y yum install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo yum install unixODBC-devel

Step 3 - Install the SQL Server PHP extension

# PHP 7.0
yum install php70-php-sqlsrv
# PHP 7.1
yum install php71-php-sqlsrv

Troubleshooting

  • The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
    • Description: Usually this is caused by querying the database with a login that uses a language setting with a different date format from the one that is being sent from the driver. Use the DBCC USEROPTIONS to validate this and change the user language accordingly.
    • References: 1

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment