This article is about fixing pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)') in mkleehammer pyodbc
  • 08-Feb-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)') in mkleehammer pyodbc

pyodbc.OperationalError: (‘08001’, ‘[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)’) in mkleehammer pyodbc

Lightrun Team
Lightrun Team
08-Feb-2023

Explanation of the problem

The error message pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)') indicates that there was a connection failure when attempting to connect to a SQL Server database using the pyodbc library. This error occurs when the connection between the client and the server was abruptly terminated.

To troubleshoot this issue, the first step is to verify that the SQL Server instance is running and reachable from the client. This can be done by using the ping command to verify the connectivity to the server’s host, and by checking if the SQL Server instance is listening on the correct port using the telnet command.

ping <server hostname or IP address>
telnet <server hostname or IP address> <port number>

If the server is reachable, the next step is to check the connection string used in the pyodbc code. Ensure that the correct username and password are specified, and that the correct server name, database name, and port number are specified.

cnxn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                      "Server=<server_name>;"
                      "Database=<database_name>;"
                      "UID=<username>;"
                      "PWD=<password>;")

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for pyodbc.OperationalError: (‘08001’, ‘[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)’)

The pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)') error is a result of compatibility issues between the OpenSSL library used by the system and the Microsoft ODBC Driver 17 for SQL Server. The error message is indicating that there is a problem with the TCP Provider and that the connection to the SQL Server database cannot be established.

To resolve this issue, there are two potential solutions. The first is to edit the /etc/ssl/openssl.cnf file and change the values of MinProtocol and CipherString. The MinProtocol value determines the minimum version of the SSL/TLS protocol that will be used for communication. By setting it to TLSv1.0, the system will only use the TLS 1.0 protocol, which is known to be compatible with the Microsoft ODBC Driver 17 for SQL Server. The CipherString value specifies the ciphers that will be used for encryption and authentication of the SSL/TLS connection. By setting it to DEFAULT@SECLEVEL=1, the system will use the default ciphers at the lowest security level.

The second solution is to implement the changes in a Dockerfile. This involves adding three commands to the Dockerfile: chmod +rwx /etc/ssl/openssl.cnf, sed -i 's/TLSv1.2/TLSv1/g' /etc/ssl/openssl.cnf, and sed -i 's/SECLEVEL=2/SECLEVEL=1/g' /etc/ssl/openssl.cnf. The chmod command is used to give the current user access rights to the file, while the sed commands replace the text in the /etc/ssl/openssl.cnf file in-place. This solution is useful if you are using a Docker container to run your application and need to make these changes within the container.

In conclusion, the pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)') error can be resolved by either editing the /etc/ssl/openssl.cnf file and changing the values of MinProtocol and CipherString, or by implementing these changes in a Dockerfile. These changes ensure that the system is only using the TLS 1.0 protocol and the default ciphers at the lowest security level, which are compatible with the Microsoft ODBC Driver 17 for SQL Server and will allow you to establish a successful connection to the SQL Server database.

Other popular problems with mkleehammer pyodbc

Problem: Connection Failure

One of the most common issues with mkleehammer-pyodbc is the connection failure error. This error occurs when the application cannot establish a connection to the database, resulting in an error message such as: pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)").

Solution:

To resolve this issue, it is important to make sure that the correct driver is installed and configured on the system. One common solution is to install the Microsoft ODBC Driver 17 for SQL Server, which is known to be compatible with mkleehammer-pyodbc. To do this, you can use the following code block

!apt-get update && apt-get install -y unixodbc-dev
!apt-get install -y msodbcsql17
!apt-get install -y unixodbc-bin
!apt-get install -y mssql-tools

Problem: Incorrect Credentials

Another issue that may arise with mkleehammer-pyodbc is incorrect credentials. This can happen if the username or password specified in the connection string is incorrect. The error message in this case may look like: pyodbc.OperationalError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user '<username>'.").

Solution:

To resolve this issue, make sure to provide the correct credentials when connecting to the database. The following code block provides an example of how to connect to a SQL Server database using mkleehammer-pyodbc and specifying the username and password:

import pyodbc

cnxn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                      "Server=<server_name>;"
                      "Database=<database_name>;"
                      "UID=<username>;"
                      "PWD=<password>;")
cursor = cnxn.cursor()

Problem: Timeout Error

A third issue that may arise with mkleehammer-pyodbc is a timeout error. This error occurs when the connection to the database takes too long to complete, resulting in a message such as: pyodbc.Error: ('HYT00', '[HYT00] [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0)').

Solution:

To resolve this issue, you can increase the timeout value in the connection string. The default timeout value is 15 seconds, but it can be increased to a higher value if necessary. The following code block provides an example of how to increase the timeout value to 30 seconds:

import pyodbc

cnxn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                      "Server=<server_name>;"
                      "Database=<database_name>;"
                      "UID=<username>;"
                      "PWD=<password>;"
                      "Connection Timeout=30;")
cursor = cnxn.cursor()

A brief introduction tomkleehammer pyodbc

mkleehammer-pyodbc is a Python library that provides access to Microsoft SQL Server databases using the Open Database Connectivity (ODBC) API. It is a popular choice for interacting with Microsoft SQL Server databases from a Python application, as it supports various versions of the SQL Server database and offers a robust and flexible interface for querying and manipulating data.

The library is built on top of the PyODBC library, which is a Python interface to ODBC databases. mkleehammer-pyodbc extends PyODBC with additional functionality for working with SQL Server databases, including support for multi-statement transactions, bulk insert operations, and connection pooling. It also includes a set of utility functions for working with SQL Server data types, such as converting dates and times from the SQL Server representation to Python data structures. Additionally, the library includes a number of optimizations and improvements over the underlying PyODBC library, making it a fast and reliable choice for connecting to and working with SQL Server databases in Python.

Most popular use cases for mkleehammer-pyodbc

  1. Data Retrieval: mkleehammer-pyodbc can be used to retrieve data from SQL Server databases and load it into a Python application for further processing or analysis. The library supports standard SQL SELECT statements and provides a convenient interface for fetching and processing query results. The following code snippet demonstrates a simple example of using mkleehammer-pyodbc to retrieve data from a SQL Server database:
import pyodbc

conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=myServerAddress;Database=myDataBase;UID=myUsername;PWD=myPassword")
cursor = conn.cursor()
cursor.execute("SELECT * FROM myTable")
rows = cursor.fetchall()
for row in rows:
    print(row)
conn.close()
  1. Data Manipulation: mkleehammer-pyodbc can be used to manipulate data stored in a SQL Server database, including inserting, updating, and deleting records. The library supports standard SQL statements for data manipulation and provides a convenient interface for executing these statements and tracking the results. The following code snippet demonstrates a simple example of using mkleehammer-pyodbc to insert data into a SQL Server database:
import pyodbc

conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=myServerAddress;Database=myDataBase;UID=myUsername;PWD=myPassword")
cursor = conn.cursor()
cursor.execute("INSERT INTO myTable (column1, column2) VALUES ('value1', 'value2')")
conn.commit()
conn.close()
  1. Database Administration: mkleehammer-pyodbc can be used to perform various database administration tasks, such as creating tables, modifying the structure of existing tables, and managing database security. The library supports standard SQL statements for database administration and provides a convenient interface for executing these statements and tracking the results. The following code snippet demonstrates a simple example of using mkleehammer-pyodbc to create a table in a SQL Server database:
import pyodbc

conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=myServerAddress;Database=myDataBase;UID=myUsername;PWD=myPassword")
cursor = conn.cursor()
cursor.execute("CREATE TABLE myTable (column1 VARCHAR(50), column2 VARCHAR(50))")
conn.commit()
conn.close()
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By clicking Submit I agree to Lightrun’s Terms of Use.
Processing will be done in accordance to Lightrun’s Privacy Policy.