我知道这是一个老问题,但维护者有一个pyodbc
GitHub Repo。
我还找到了一个非常好的安装 FreeTDS 和设置配置文件的示例。
在我看来,遵循 GitHub 文档上的说明总是最好的选择。截至 2018 年 2 月,对于 CentOs7(他们在链接上拥有所有口味),他们说:
# Add the RHEL 6 library for Centos-7 of MSSQL driver. Centos7 uses RHEL-6 Libraries.
sudo su
curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo
exit
# Uninstall if already installed Unix ODBC driver
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel #to avoid conflicts
# Install the msodbcsql unixODBC-utf16 unixODBC-utf16-devel driver
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
# the Microsoft driver expects unixODBC to be here /usr/lib64/libodbc.so.1, so add soft links to the '.so.2' files
cd /usr/lib64
sudo ln -s libodbccr.so.2 libodbccr.so.1
sudo ln -s libodbcinst.so.2 libodbcinst.so.1
sudo ln -s libodbc.so.2 libodbc.so.1
# Set the path for unixODBC
export ODBCINI=/usr/local/etc/odbc.ini
export ODBCSYSINI=/usr/local/etc
source ~/.bashrc
# Prepare a temp file for defining the DSN to your database server
vi /home/user/odbcadd.txt
[MyMSSQLServer]
Driver = ODBC Driver 13 for SQL Server
Description = My MS SQL Server
Trace = No
Server = 10.100.1.10
# register the SQL Server database DSN information in /etc/odbc.ini
sudo odbcinst -i -s -f /home/user/odbcadd.txt -l
# check the DSN installation with:
odbcinst -j
cat /etc/odbc.ini
# should contain a section called [MyMSSQLServer]
# install the python driver for database connection
pip install pyodbc