在linux上连接到MSSQL Server 2008

时间:2013-08-27 20:10:55

标签: python sql-server linux sql-server-2008 pyodbc

我想知道如何从linux机器连接到MSSQL Server 2008。我目前安装了FreeTDS - 但是我没有运气让bsqldb工作。我目前能够使用以下python代码(在Windows中)连接到此数据库:

import pyodbc
cnxn = pyodbc.connect("DRIVER={SQL Server};"
                      +"SERVER=something.example.com;"
                      +"DATABASE=exampledatabase;"

我相信我的Windows凭据正在这里传递。有没有人对linux中使用的内容有任何建议?

1 个答案:

答案 0 :(得分:3)

您是否拥有所需的所有软件?这就是Ubuntu 12.04所需要的:

sudo apt-get install php5-odbc php5-sybase tdsodbc

您是否在Linux服务器上配置了这些文件? (这些是从Ubuntu 12.04服务器获取的)

<强> /etc/odbc.ini

# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssql]
Description             = MSSQL Server
Driver                  = freetds
Database                = MyDatabase
ServerName              = mssql
TDS_Version             = 8.0

<强> /etc/odbcinst.ini

# Define where to find the driver for the Free TDS connections.
[freetds]
Description     = MS SQL database access with Free TDS
Driver          = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup           = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount      = 1

<强> /etc/freetds/freetds.conf

# The basics for defining a DSN (Data Source Name)
# [data_source_name]
#       host = <hostname or IP address>
#       port = <port number to connect to - probably 1433>
#       tds version = <TDS version to use - probably 8.0>

# Define a connection to the MSSQL server.
[mssql]
        host = mssql_server_ip_or_domain_name
        port = 1433
        tds version = 8.0

我已经阅读了几个导致问题的tds版本的帐户。看起来最好是8.0字,但我也看到人们说他们的工作用7.5和7.0。

然后测试你的连接:

isql mssql username password

根据您的环境,您的用户名可能必须采用以下格式:domain \ username

发出命令后,您应该看到类似的内容:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

这就是我认为你的connect命令应该是什么样的(注意:我不懂Python):

cnxn = pyodbc.connect('DRIVER=freetds;SERVER=FOOBAR;PORT=1433;DATABASE=T2;UID=FOO;PWD=bar;TDS_Version=8.0;')