简体   繁体   中英

Connecting to Microsoft Azure SQL Database using Python on Ubuntu 14.04

I have been trying to connect to my Azure SQL Database on my Ubuntu 14.04 laptop using Python.

I found this article from Microsoft: Connect to SQL Database by using Python on Ubuntu Linux , which showed me how to connect to my SQL Database using Python 2.7.6


I confirmed I met the Python 2.7.6 requirement before getting started:

The output of ryan@laptop:~$ python -V was Python 2.7.6


I followed the instructions and entered the following lines into my terminal:

 sudo apt-get --assume-yes update sudo apt-get --assume-yes install freetds-dev freetds-bin sudo apt-get --assume-yes install python-dev python-pip sudo pip install pymssql 

Everything installed as expected.


I created a python script and entered my information, here is my code:

#!/usr/bin/python
import pymssql
conn = pymssql.connect(server='ryans_server.database.windows.net', user='ryans_user@ryans_server', password='ryans_password', database='ryans_database')

However, when I execute this code, this is the output:

Traceback (most recent call last):
  File "./tp-database.py", line 3, in <module>
    conn = pymssql.connect(server='ryans_server.database.windows.net', user='ryans_user@ryans_server', password='ryans_password', database='ryans_database')
  File "pymssql.pyx", line 637, in pymssql.connect (pymssql.c:9508)
pymssql.OperationalError: (18456, "Login failed for user 'ryans_user'.DB-Lib error message 18456, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n")

I have tried: export TDSVER=7.0 , which causes a different error:

pymssql.OperationalError: (20017, 'DB-Lib error message 20017, severity 9:\nUnexpected EOF from the server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')

What can I do to connect to my Azure SQL Database?

I was able to connect to ms sql server using pymssql from my Ubuntu box.

This is what I used:

#!/usr/bin/python
"Proof connection at pyqllevel."
# Test pyodbc connection. Result is 42.
# Note parameters in connection string, <PARAMETER>.

import pymssql
conn = pymssql.connect(server='serverName.database.windows.net',user='username@serverName',password='xxxxxxxxx',database='ustxazsql1')
cursor = conn.cursor()

In addition, for other users who use Windows VS + Python pymssql (installed via VS) to connect SQL Azure, you may encountered a similar error that pointed by PyanNHG:

在此输入图像描述

To resolve this issue on Windows, please re-install pymssql lib from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql :

  1. Download the pymssql .whl file that align with your Python version
  2. Install pymssql from that .whl via pip,

    python -m pip D:\\whlFile.whl

Then you could successfully connect to SQL Azure using pymssql (Please note an important step is to add the ip address from which you are running this code to Azure portal - Allow ip addresses for connecting sql azure database.)

I am currently using the following code to connect to azure sql

def connect(server, database_name):
try:
    conn = pymssql.connect(server=server['hostname'], user=server['db_user'], password=server['db_password'], database=database_name)
    return conn
except:
    logging.warning("connection Error will continue")
    logging.exception('Got exception on connect to db {}, {}'.format(server, database_name))
    return None

the server structure is basically a dict with the password and user

I have not installed freetds directly and used pip install pymssql in a virtual env and it works fine

You can see the entire script at https://gist.github.com/srgrn/71f0786ca4779e3178b9f5d9c6d1ee80

I had three separate issues connecting to Azure SQL database, first trying with pymssql then pyodbc .

  1. TDS_Version had to be set in the (pyodbc) connection string. I could connect with TDS_Version=7.1 or TDS_Version=7.2 but 7.3 and 8.0 would fail.

  2. I had to explicitly set PORT=1433 ( previous port issue )

  3. I was trying to connect with a user I created, which is incorrect. Connect to azure with the login : I can't login with a user I created in sql azure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM