I am using the following code to connect to the SQL server database from my application.
try:
self.__db_conn = pymssql.connect(self.__db_host, self.__db_user, self.__db_pass, self.__db_name)
self.__db_cursor = self.__db_conn.cursor(as_dict=True)
except pymssql.DatabaseError as e:
print("Error: Could not connect to ODSREAD Database : %S", e)
and when I execute, I am getting the following error:
Error: Could not connect to ODSREAD Database : %S (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 (ods.domain.net:1433)\n')
I have tried with pyodbc also and the code is as follows:
__db_driver = '{FreeTDS}'
__db_ip = ODS_IP
__db_port = ODS_PORT
__db_dbname = ODS_DBNAME
__db_username = ODS_USERNAME
__db_password = ODS_PASSWORD
__db_conn = None
__db_cursor = None
__connect_url = None
connectUrl = 'DRIVER={driver};SERVER={server};PORT={port};DATABASE={db_name};UID={username};PWD={password};TDS_VERSION=7.0'.format(
driver=self.__db_driver,
server=self.__db_ip,
port=self.__db_port,
db_name=self.__db_dbname,
username=self.__db_username,
password=self.__db_password
)
self.__db_conn = pyodbc.connect(self.__connect_url)
Here also, I am getting errors like this:
pyodbc.ProgrammingError: ('42000', '[42000] [unixODBC][FreeTDS][SQL Server]Login failed.
The login is from an untrusted domain and cannot be used with Windows authentication. (18452) (SQLDriverConnect)')
Seems like it is taking the Windows Authentication instead of the driver specified. I am running this on a Linux (CentOS 7.6) server and hence I am not able to use Windows authentication.
How can I fix this error?
Adding freetds.conf
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
use ntlmv2 = yes
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 5.0
# A typical Microsoft server
[egServer70]
host = ntmachine.domain.com
port = 1433
tds version = 7.0
and odbcinst.ini has the following
[PostgreSQL]
Description=ODBC for PostgreSQL
Driver=/usr/lib/psqlodbcw.so
Setup=/usr/lib/libodbcpsqlS.so
Driver64=/usr/lib64/psqlodbcw.so
Setup64=/usr/lib64/libodbcpsqlS.so
FileUsage=1
[MySQL]
Description=ODBC for MySQL
Driver=/usr/lib/libmyodbc5.so
Setup=/usr/lib/libodbcmyS.so
Driver64=/usr/lib64/libmyodbc5.so
Setup64=/usr/lib64/libodbcmyS.so
FileUsage=1
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1
UsageCount=1
[FreeTDS]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/lib64/libtdsodbc.so.0
UsageCount=1