如何在Linux下访问SQL SERVER
最近需要在Linux环境下访问SQL SERVER,参考了下面的文章成功连接,在此记录该方法。
原文地址:https://blog.csdn.net/ydyang1126/article/details/50952474
Connecting to Microsoft SQL Server from UNIX (Linux/Mac OSX) in Python
Assuming you have a username and password with some kind of access to a MSSQL server, this might help you.
Mac OS X instructions lower down but the full post should help it all sink in.
Linux
Install unixODBC, this abstracts database access
If I was on SUSE Linux (SLES) I would hit up yast
and install the following packages
|
|
Install FreeTDS driver
This is the Open Source MSSQL Driver for Unix-based systems: http://www.freetds.org/
|
|
Next, configure FreeTDS with the location of unixODBC, you’re telling it you plan to use it via an ODBC interface, if /usr/lib/unixODBC exists then the value you need is /usr. Then, you can make
and install
.
|
|
Find your freetds.conf, we’re going to stick in the connection details for your SQL Server Box. On this Linux box, it’s /usr/local/etc/freetds.conf
In the stock conf, there’s a helpful example provided:
|
|
I added this line to this server config…
|
|
I can’t tell you how awesome this line is. It uses the iConv libraries to make sure you’re getting UTF-8 data over your ODBC connection, even if the SQL Server config is some godawful WIndows cp1252 ANSI code page.
Create your own server config with your own credentitals. The tds version relates to the version of SQL server you are using http://www.freetds.org/userguide/choosingtdsprotocol.htm. In short, if you’re using SQL Server 2000 and above, use version 8.0
Port 1433 is the default port for connecting to MSSQL Servers, why not check if you can connect from your given server before trying the FreeTDS driver
|
|
FreeTDS installs the tsql
binary for testing your connections. The -S
parameter refers to the [egServer70]
section of freetds.conf
:
|
|
Great! That 1>
prompt smells of success!
unixODBC
Now we need to register the driver with unixODBC, using the odbcinst binary that is installed with unixODBC
Create a file:
|
|
edit the file - the key detail here is the Driver line. It should point to wherever the FreeTDS installation wrote the libtdsodbc.so file. In most cases, following these instructions on a Linux box, it will be in /usr/local/lib but it could vary on different UNIX-based systems.
|
|
Register the FreeTDS driver with ODBC
|
|
So that’s the driver registered, now we need to let ODBC know about the SQL Server we want to connect to, as set out in freetds.conf
Add a DSN for the SQL Server in odbc.ini
|
|
isql
is the client installed by unixODBC. Pass isql
the DSN you defined along with your username and password
|
|
Now ODBC is allowing you to enter plain old SQL, INTO A MSSQL SERVER, FROM A LINUX BOX!
|
|
If you encounter issues, make sure that isql knows where to find your odbc ini file or it will not be able to. I’ve found environment variables to be useful in this process, e.g.:
|
|
Install pyodbc
http://code.google.com/p/pyodbc/
To compile it you will need the GNU C++ compiler. Under SLES YAST, it is called gcc-c++
but elsewhere you might find it as g++
.
You’ll also need the python-devel
and unixODBC-devel
package to compile against. Check for these in your distribution’s package manager.
|
|
Or, in modern parlance:
|
|
MAC OSX
There are slight differences on this platform. You’ll need XCode from the Mac App Store and you’ll need to install the Command Line Tools from Xcode
>Preferences
>Downloads
>Components
.
|
|
Then, edit freetds.conf lie
|
|
Pop in your SQL server details
|
|
iODBC takes responsibility for the ODBC legwork on Mac OS X Python, you can edit the config and create the DSN you need. The Driver
line is essential to tell iODBC where the freetds driver is.
|
|
Here are the contents:
|
|
You’ll need to have pyodbc
for Python installed:
|
|
Or, in modern parlance:
|
|
Then get on the Python interpreter:
|
|
Look at those lovely native Python datetime
instances!
References
These references are invaluable:
- http://www.easysoft.com/developer/languages/python/pyodbc.html
- http://www.unixodbc.org/doc/FreeTDS.html