Connecting to SQL Server from RHEL or Centos

Keith Erskine edited this page Dec 6, 2016 · 3 revisions

Microsoft provide database drivers specifically for Red Hat Enterprise to connect to SQL Server ( CentOS is derived from Red Hat so the same driver will usually work with CentOS just as well. There are actually two Microsoft drivers, versioned as numbers 11 and 13.0. Version 11 works with Red Hat 5 and 6. Version 13.0 works with Red Hat 6 and 7. However the following instructions are for version 11 only.

Note, Microsoft driver 11 uses unixODBC as its driver manager, not FreeTDS or iODBC. Furthermore, the driver and driver manager must be installed globally on your server. Don't try installing them in a Python virtual environment, it'll end in tears.

Install unixODBC

See for reference.

Microsoft specifies that unixODBC version 2.3.0 should be installed with their driver, and goes on to say that version 2.3.1 is not supported. However, you should be aware that version 2.3.0 is quite old now and has some significant bugs in it, particularly when making multiple connections to the same database. That specific scenario can cause segmentation faults in your Python code - see "Driver version was not being held when a second connection was made to the driver" on - an issue that was fixed in version 2.3.1. Hence, if you are using multiple concurrent database connections, you should definitely consider installing at least version 2.3.1 of unixODBC. The rest of these instructions assume version 2.3.2 is being installed.

# download and unzip the unixODBC driver
curl -O ''
tar -xz -f unixODBC-2.3.2.tar.gz
cd unixODBC-2.3.2

# remove any existing unixODBC drivers - be very careful with 'sudo rm'!
sudo rm /usr/lib64/libodbc*

# install the unixODBC driver
# note, adding "--enable-stats=no" here is not specified by Microsoft
./configure --prefix=/usr --libdir=/usr/lib64 --sysconfdir=/etc --enable-gui=no --enable-drivers=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --enable-stats=no 1> configure_std.log 2> configure_err.log
make 1> make_std.log 2> make_err.log
sudo make install 1> makeinstall_std.log 2> makeinstall_err.log

# the Microsoft driver expects unixODBC to be here /usr/lib64/,
# so add soft links to the '.so.2' files
cd /usr/lib64
sudo ln -s
sudo ln -s
sudo ln -s

Check the unixODBC installation with the following commands. They should all return information that can be verified as correct:

ls -l /usr/lib64/libodbc*
odbc_config --version --longodbcversion --cflags --ulen --libs --odbcinstini --odbcini
odbcinst -j
isql --version

Install the Microsoft ODBC Driver for Linux

See for reference.

First of all, go to and download the appropriate zip file to your Linux server. Then install it as follows:

cd /path/to/your/driver/file/directory
tar -xz -f msodbcsql-11.0.2270.0.tar.gz
cd msodbcsql-11.0.2270.0
sudo ./ install --accept-license --force 1> install_std.log 2> install_err.log

Check the msodbc installation with the following commands. They should all return information that can be verified as correct:

ls -l /opt/microsoft/msodbcsql/lib64/
dltest /opt/microsoft/msodbcsql/lib64/ SQLGetInstalledDrivers
cat /etc/odbcinst.ini   # should contain a section called [ODBC Driver 11 for SQL Server]

Prepare a file for defining the DSN to your database with a temporary file something like this:

Driver      = ODBC Driver 11 for SQL Server
Description = My MS SQL Server
Trace       = No
Server      =

In that file, leave the 'Driver' line exactly as specified above, but modify the rest of the file as necessary. Then run the following commands:

# register the SQL Server database DSN information in /etc/odbc.ini
sudo odbcinst -i -s -f /path/to/your/temporary/dsn/file -l

# check the DSN installation with:
cat /etc/odbc.ini   # should contain a section called [MySQLServerDatabase]

Test the Connection

Try connecting to your database with something like this:

python -c "import pyodbc; print(pyodbc.connect('DSN=MySQLServerDatabase;UID=myuid;PWD=mypwd'))"
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.