How to connect to a server cluster using python?

Hassan Askari 0 Reputation points
2024-09-11T16:57:20.3+00:00

I am trying to setup a connection string to a sever cluster instance in python after a few searches here and there I seem to be stumped, regardless of what solution I try I get one of two errors. Either the cluster dose not exist or back slash is not allowed in connection string. I tried using raw strings to by bass the backslash error but the error returned to server cannot be found. I have double checked/triple checked the credentials. I am able to connect from other applications and services in java but I am having issues with my python scripts. Any suggestions would help thank you for your time in advance.

DRIVER_NAME = 'ODBC Driver 17 for SQL Server'
SERVER_NAME = 'SERVER_NAME'
INSTANCE = 'INSTANCE'
USERNAME = 'USERNAME'
PASSWORD = 'PASSWORD'


CONNECTION_STRING = f"""DRIVER={{{DRIVER_NAME}}};SERVER={SERVER_NAME};DATABASE={DATABASE_NAME};INSTANCE{INSTANCE};UID={USERNAME};PWD={PASSWORD}"""



DRIVER_NAME = 'SQL Server'
SERVER_NAME = r'SERVER_NAME/INSTANCE'
USERNAME = 'USERNAME'
PASSWORD = 'PASSWORD'

CONNECTION_STRING = f"""DRIVER={{{DRIVER_NAME}}};SERVER={SERVER_NAME};DATABASE={DATABASE_NAME};UID={USERNAME};PWD={PASSWORD}"""
try:
    cnxn = pyodbc.connect(CONNECTION_STRING)
    cursor = cnxn.cursor()

the code fails at pyodbc.connect(CONNECTION_STRING)

Error in insertIntoDB: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (2)')

Windows Server Clustering
Windows Server Clustering
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Clustering: The grouping of multiple servers in a way that allows them to appear to be a single unit to client computers on a network. Clustering is a means of increasing network capacity, providing live backup in case one of the servers fails, and improving data security.
995 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.