2
votes

Using python 2.7 and the MS odbc driver through pyodbc. My connection string looks like this:

mssql+pyodbc://myuser:mypass@serverip/instancename?driver=ODBC+Driver+11+for+SQL+Server

I am getting login failed. However if I use the same credentials and "serverip\instancename" in the microsoft sql server management studio, I can connect.

The thing that is driving me crazy is a couple of days ago, this same connection string worked for me but to a different sql server instance on the same machine. So what I am trying to figure out is how to go about troubleshooting it.

Thanks for any pointers.

2
Which OS is this? Is this also the same client (could it be related to the fact that the driver is not installed)? - van
Driver is installed. The client is debian linux. One of the things that is confusing me a little bit is the "instance name" versus "database" nomenclature. This physical server has more than one SQL server instance on it. And the particular SQL server instance I am interested in has more than one database. And by database here, I mean database in the sense that you would say "use db1" in your sql statements. - Jay
i think you need a backslash (\ ) to specify an instance name, and put a database name after a forward slash (/). So maybe mssql+pyodbc://myuser:mypass@serverip\instancename?driver=ODBC+Driver+11+for+SQL+Server would do it? - van
I had tried backslash too, thanks though van. My latest theory is that MS SQL Server uses different ports for different named instances on the same physical server (OS). So perhaps connecting to a named instance is just a matter of specifying that named instance's port number. I just have to get some sysadmin help to try this out and it isthe weekend right now so no dice. - Jay
re: "I am getting login failed. However if I use the same credentials and "serverip\instancename" in the microsoft sql server management studio, I can connect." - See this answer for the explanation. - Gord Thompson

2 Answers

2
votes

When learning sqlalchemy I had the same issue. This method worked for me:

import urllib
params = urllib.quote_plus("DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATABASE=test;UID=user;PWD=password")

engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)

Which comes from the sqlalchemy documentation. It does not seem related to the login credentials, but the error message was the same for me and this worked.

ref http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#pass-through-exact-pyodbc-string

2
votes

I just went through this, the reason it fails is the port, each instance listens in a different port, so you need to point which port is for that instance.

my code is:

DBserver='host.abc.nt'
DBengine='devenv'
DBport='####'
DBname='mydb'
DBDriver='ODBC Driver 13 for SQL Server'
DBuser='user'
DBpwd='pass'
DBuserpass = DBuser if len(DBuser) > 0 else ''
DBuserpass = DBuserpass + ':' + DBpwd if len(DBuser) > 0 else ''
if len(DBengine) > 0:
    DBserver = DBserver + '\\' + DBengine 

engine = sa.create_engine(f'''mssql+pyodbc://{DBuserpass}@{DBserver}:{DBport}/{DBname}?driver={DBDriver}''')

query=engine.execute('SELECT DB_NAME(), @@SERVERNAME')
query.fetchall()
[('mydb', 'host\\devenv')]

With that you create the engine, there is no need for other libraries besides sqlalchemy

but if you want to know the port of the instance, you can use: Github repository from gordthompson/sqlserverport

or in Windows you connect with SQL Studio (or anything else) do some selects the the DB and in the meanwhile in CMD run a netstat to get the port:

C:\Users\pla>netstat -qfa | findstr host
 TCP    10.0.0.1:##87     host.abc.nt:4096  ESTABLISHED