Pymssql Write to the server failed - 20006 - sql-server

I am using pymssql=2.1.1 to connect Azure database from python. Due to idle connection for few minutes, i am getting error (Write to the server failed) and not able to fetch the data.
I am using connect method of pymssql to create connection to Azure DB.
conn = pymssql.connect(server=v_host, user=v_user, password=v_passwd, database=v_db)
cursor = self.conn.cursor(as_dict=True)
cursor.execute(query)
The error looks like
(20006, b'DB-Lib error message 20006, severity 9:\nWrite to the server failed\nNet-Lib error during Connection reset by peer (104)\n')

If you want to us pymssql connect to Azure SQL database, make sure the following requirements:
Examples:
import pymssql
conn=pymssql.connect("xxx.database.windows.net", "username#xxx", "password", "db_name")
cursor = conn.cursor()
cursor.execute(query)
For more details, please see: Connecting to Azure SQL Database. Starting with version 2.1.1 pymssql can be used to connect to Microsoft Azure SQL Database. And you can troubleshoot the error by pymssql Frequently asked questions.
Another way, you also can try the pyodbc 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(query)
Here is the Azure document: Quickstart: Use Python to query an Azure SQL database.
Hope this helps.

Related

Which exact driver is sqlalchemy using?

I am having trouble with a MS SQL connection when using pyinstaller. When run in interactive mode, everything works as expected. After compiling to an exe, the MS SQL database connection times out on the first query with the following error:
(pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expire (0); ...
My connection string is similar to the following:
create_engine(
"mssql+pyodbc://USER:PASSWORD#SERVERIP/DB_NAME?driver=ODBC+Driver+17+for+SQL+Server"
)
In attempting to diagnose the issue, I am printing out the drivers available to pyodbc with pyodbc.drivers() (which shows a large disparity between available drivers in compiled vs interactive) as well as the driver in use using
print(session.bind.dialect.name)
> pyodbc
print(session.bind.dialect.driver)
> mssql
It returns the upper level python modules which are being used but not the .dll that is handling it at a lower level. Is there any way to find which exact driver is being used? Any tips on what could be causing the error in the compiled version in the firstplace would be appreciated as well.
The issue may be in your connection string.
To create a proper connection string to connect to MSSQL Server ODBC driver with sqlAlchemy use the following:
import urllib
from sqlalchemy import create_engine
server = 'serverName\instanceName,port' # to specify an alternate port
database = 'mydb'
username = 'myusername'
password = 'mypassword'
params = urllib.parse.quote_plus('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
Also, you can check the following article Connecting to Microsoft SQL Server using SQLAlchemy and PyODBC
Is there any way to find which exact driver [.dll] is being used?
import pyodbc
cnxn = engine.raw_connection()
print(cnxn.getinfo(pyodbc.SQL_DRIVER_NAME)) # msodbcsql17.dll
print(cnxn.getinfo(pyodbc.SQL_DRIVER_VER)) # 17.08.0001

SQL Server Connection with Python PYMSSQL active directory authentication

I am trying to connect SQL Server using pymssql package with active directory authentication. Below is the code.
db = pymssql.connect(server=url, user=r'Domain\user', password=password, database=database)
cursor = db.cursor()
But it fails with below error:
Error (18452, b'Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed ()\n')
Python version: Python 3.8.6
PYMSSQL version: pymssql 2.1.5
For most client libraries you can't specify the username/password with Windows Integrated Auth. It just connects as the identity of the current process.
To do otherwise would require the driver to implement (or us a library that implements) the NTLM or Kerberos authentication protocols, and AFAIK only the Microsoft JDBC Driver for SQL Server does that.
Not sure that will work. Try it like this.
# connect to SQL Server
import pyodbc
from sqlalchemy import create_engine
driver= '{SQL Server Native Client 11.0}'
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=your_server_name;'
r'DATABASE=your_db_name;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Employee')
# print contents of table
for row in cursor:
print('row = %r' % (row,))

Could not connect newly created SQL Server database using pyodbc

I have configured a new SQL Server 2017.XXX instance in my Windows 10. Then, created a new database called CUSTOMER_DEVICES. I tried to connect to database using pyodbc. I could not and got below error:
Error - pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC SQL Server Driver]
When I use the same code in existing production server it works. Code below
import pyodbc
conn = pyodbc.connect(r'Driver={SQL Server};Server=localhost;Database=CUSTOMER_DEVICES;Trusted_Connection=yes;')
cursor = conn.cursor()
print(cursor)
Check if SQL server Configuration Management is configured. Enable TCP, IP. Check below link Check here

Azure server access issue despite fixing firewall settings

I'm receiving an error when I try to connect to an Azure SQL server using pyodbc.
I found the connection parameters under 'Connection strings' for ODBC in the Azure portal. I have added my IP address in the firewall settings (and waited >1 hour) but this did not resolve the problem.
import pyodbc
DRIVER = '{SQL Server}'
SERVER = 'tcp:[server name].database.windows.net'
PORT = '1433'
DATABASE = [database name]
USERNAME = [username]
PASSWORD = [password]
CONNECTION_STRING = f'DRIVER={DRIVER};PORT={PORT};SERVER={SERVER};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD}'
cursor = pyodbc.connect(CONNECTION_STRING).cursor()
I get the following error:
ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]
Cannot open server [server name] requested by the login. Client with IP address [my IP]
is not allowed to access the server. To enable access, use the Windows Azure
Management Portal or run sp_set_firewall_rule on the master database to create a
firewall rule for this IP address or address range. It may take up to five minutes
for this change to take effect. (40615) (SQLDriverConnect); [42000] [Microsoft]
[ODBC SQL Server Driver]Invalid connection string attribute (0); [42000] [Microsoft]
[ODBC SQL Server Driver][SQL Server]Cannot open server [server name] requested by the
login. Client with IP address [my IP] is not allowed to access the server. To enable
access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the
master database to create a firewall rule for this IP address or address range. It may
take up to five minutes for this change to take effect. (40615); [42000] [Microsoft]
[ODBC SQL Server Driver]Invalid connection string attribute (0)")
Update:
I tried connecting using Visual Studio and it prompts me to create a new firewall rule. I choose 'Add my client IP' and click 'OK'. The prompt then immediately reappears. I tried clicking it a few times and the new rules do appear in the Azure portal, but I am still not able to connect through either Visual Studio or python.
Solution:
I was using SQL authentication instead of Active Directory authentication. Solved the problem by adding AUTHENTICATION=ActiveDirectoryPassword to the connection string.
Please ensure you have added client IP to firewall.
On the Azure SQL database overview, Set server firewall.
Add client IP:
Please modify you code like this and try again:
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()
This code example is provided by this documen: Create code to query your SQL database.
Updates:
The error has be solved..
Finally figured it out - it turns out the problem was that Jon123 was using SQL authentication instead of Active Directory authentication.
Hope this helps.

How do I access my Azure SQL database (not in a container) from within a docker container

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()

Resources