I have a SQL Server running on docker. I'm trying to connect to it from a web app written in python3 with pymssql.
It's working until I run the same app inside a docker container.
I get this error:
20002, b'DB-Lib error message 20002, severity 9:\\nAdaptive Server connection failed (nameofmydb.database.windows.net:1433)\\n'.
Code:
pymssql.connect(server='mydb.database.windows.net', port='1433', database='mydb', user='user@server', password='pwd')
I tried with --net=host
but it doesn't work either.
Can someone help me, please?
EDIT: So i finally succeeded after around 2 days , it was because of the installation of odbc driver in the container i was doing it in a wrong way. Also i was forced to use pyodbc yes. Install the odbc drivers + python the way azure tell you to in the docs and use pyodbc and not pymssql (which works outside of docker).
You can reference this blog: Adaptive server connection failed (DB-Lib error message 20002, severity 9) .
Try to use pyodbc
instead of pymssql
. I tried and it works well.
import pyodbc
server = 'XXX.database.windows.net'
database = 'dbname'
username = 'username'
password = 'psd'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
The Azure document Quickstart: Use Python to query an Azure SQL database also provides the example:
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
Hope this helps.
So i did all of these .
But now i have this :
pyodbc.OperationalError: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
i used the odbcinst.ini that is in connection strngs in the azure pannel which correspond to :
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount=1
I changed driver and setup to suit the docker container. The pyodbc code is :
pyodbc.connect( 'Driver={ODBC Driver 17 for SQL Server};Server=tcp:mydb.database.windows.net,1433;Database=mydb;Uid=myuser@mydb;Pwd=mypwd;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'))
Also i repeat it but it looks that everything works well outside the docker container . i have absolutely no clue what is going on ...
I was facing the problem, seems to me that it was connection string which was causing the issue.
import pyodbc
server = 'xxxx.database.windows.net'
database = 'xxxx'
driver= '{ODBC Driver 17 for SQL Server}'
username='xxxxx@xxx.com'
password = 'xxxxxx'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password+';Authentication=ActiveDirectoryPassword') as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT top 4 * FROM [dbo].[TableName]")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
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.