QT unable to connect to mssql server from qt application in ubuntu - sql-server

Unable to connect to mssql server from qt application in ubuntu 20.04
Sql server located in windows 7
The connection is definitely present, because it was possible to connect through DDbeaver.
Can anyone help me in this situation?
Code:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3");
db.setConnectOptions();
QString serverName = "566***";
QString ipName = "tcp:192.168.144.101,1433";
QString dbName = "St***";
QString connectionString = QString("DRIVER={ODBC Driver 18 for SQL Server};Server=%1;Database=%2;").arg(ipName).arg(dbName);
db.setDatabaseName(connectionString);
db.setUserName("sa");
db.setPassword("top123TOP");
if (db.open())
{
qDebug() << "Correct connection";
}
else
{
QString error = db.lastError().text();
qDebug() << error;
}
otuput is:
"[Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2746 [Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection QODBC3: Unable to connect"
odbcinst.ini
[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.0.so.1.1

the following solution helped:
from:https://github.com/microsoft/msphpsql/issues/1112
This was helpful.
Ubuntu 20.04 PHP 7.4 using the 19.10 drivers for sqlsrv
We have a real old SQL server out there . The SQL Server show's version 10.50.2550.0, i think it's SQL Server 2008 R2. I had to use TLSv1 to connect to the server. I also had to do a "systemctl restart apache2" to get it to take affect. TLSv1.1 did not work with my MSSQL server version.
Error message: Connection failed: SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol]Database Connection Error
edit: /etc/ssl/openssl.cnf
1st line in the file added
openssl_conf = default_conf
End of file added
[default_conf]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
MinProtocol = TLSv1
CipherString = DEFAULT#SECLEVEL=1
Not 100% sure why i had to restart apache2 for it to take effect, but I had to.
systemctl restart apache2
reloaded the page and it works

Related

Issues connecting to Docker MSSQL using pyodbc

I have pulled a docker image that runs an MS SQL from docker hub:
docker pull mcr.microsoft.com/mssql/server:2017-latest
docker run --name some-db -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD=Aamerge1234 -e MSSQL_PID=Developer -d -p 1433:1433 mcr.microsoft.com/mssql/server:2017-latest
The container runs fine, I added a strong password as required.
This is the python code I am using to connect with the database using pyodbc:
import pyodbc
server = 'localhost:1433'
# database = ''
username = 'sa'
password = 'Aamerge1234'
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER='+server+';UID='+username+';PWD='+ password)
I have omitted the variable database because mssql creates several.
I tried using the following ODBC drivers:
ODBC Driver 17 for SQL Server
SQL Server
However, it always returns error:
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 expired (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. (53)')

Login failed for the user '' SQL Server when connecting using python from EMR server

I'm trying to connect to SQL Server Database from AWS EMR server.
$cat /etc/odbcinst.ini
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.8.so.1.1
UsageCount=1
and I modified the .odbc.ini file in my home directory like this.
$cat .odbc.ini
[ODBC Data Sources]
studylog = ODBC17Driver
[studylog]
Driver = /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.8.so.1.1
Description = My Description
Server = hostname.amazonaws.com
output of odbcinst -j command
unixODBC 2.3.7
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /mnt/efs/home/username/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
output of odbcinst -q -d command
[ODBC Driver 17 for SQL Server]
output of odbcinst -q -s command
[studylog]
Python script I'm using to connect to SQL Server
import pyodbc
host = "hostname.amazonaws.com"
user="DBUser"
password="Password"
db="dbname"
details = {
'server' : host,
'database' : db,
'username' : user,
'password' : password
}
# ODBC Driver 17 for SQL Server
connect_string = 'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server};PORT={{1433}}; DATABASE={database};UID={username};PWD={password}'.format(**details)
connection = pyodbc.connect(connect_string)
print(connection)
output of my python script
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'DBUser'. (18456) (SQLDriverConnect)")
What's going wrong in this? Thank you in advance.

How to log in to a SQL Server using pyodbc in a Virtual Machine (VMware)?

I've always accessed my University's database using pyodbc in my own machine. However, when I try to use the same code (below) in a Virtual Machine (VMware) I receive an error (translated from Portuguese to English):
import pyodbc
server = 'beirute-01'
database = 'RAIS'
port= '1433'
username = 'gabrielcp2'
password = '*****'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT='+port+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
-------------------------------------------------- -------------------------
OperationalError Traceback (most recent call last)
C:\Users\GABRIE~1\AppData\Local\Temp/ipykernel_2064/1387256718.py in <module>
11 driver= 'ODBC Driver 17 for SQL Server'
12
---> 13 cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT='+port+';DATABASE='+database+';UID='+username+';PWD=' + password)
14
15 cursor = cnxn.cursor()
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Timeout Error [258]. (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Logon timed out (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Cannot complete login process due to pre-logon response delay (258)')
How could I fix it?

unixODBC works but Apache will not connect

I am trying to setup apache to connect to a Microsoft SQL server for authentication. This is not ideal but this legacy system has the credentials in MSSQL and that can not change. I have unixODBC setup and working
**odbcinst.ini**
[SQL Server Native Client 11.0]
Description = Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver = /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0
Threading = 1
UsageCount = 1
**odbc.ini**
[mssql]
Driver = SQL Server Native Client 11.0
Server = 192.168.250.200
Database = DBName
When I connect using isql I am able to query the database without issue
isql mssql username password
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
In apache I have configured the following
DBDriver odbc
DBDParams "datasource=mssql,user=username;pass=password"
DBDMin 1
DBDKeep 2
DBDMax 10
DBDExptime 300
When I start httpd I get this in the error log
[Thu Dec 10 09:10:35 2015] [dbd_odbc] SQLDriverConnect returned SQL_ERROR (-1) at dbd/apr_dbd_odbc.c:1146 [unixODBC][Microsoft][SQL Server Native Client 11.0]Login timeout expired HYT00 [unixODBC][Microsoft][SQL Server Native Client 11.0]TCP Provider: Error code 0xD 08001 [unixODBC][Microsoft][SQL Server Native Client 11.0]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 confi 08001
[Thu Dec 10 09:10:35.633986 2015] [dbd:error] [pid 15481] (20014)Internal error: AH00629: Can't connect to odbc: [dbd_odbc] SQLDriverConnect returned SQL_ERROR (-1) at dbd/apr_dbd_odbc.c:1146 [unixODBC][Microsoft][SQL Server Native Client 11.0]Login timeout expired HYT00 [unixODBC][Microsoft][SQL Server Native Client 11.0]TCP Provider: Error code 0xD 08001 [unixODBC][Microsoft][SQL Server Native Client 11.0]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 confi 08001
[Thu Dec 10 09:10:35.634054 2015] [dbd:error] [pid 15481] (20014)Internal error: AH00633: failed to initialise
[Thu Dec 10 09:10:35.634200 2015] [dbd:crit] [pid 15481] (20014)Internal error: AH00636: child init failed!
SELinux was blocking the connection from Apache.

Error 20009 (severity 9):Unable to connect: Adaptive Server is unavailable or does not exist OS error 111, "Connection refused"

while trying to connect to remort mssql server by command
tsql -S SQLEXPRESS -U sa
passwod :sa
it throws error as
Error 20009 (severity 9):Unable to connect: Adaptive Server is unavailable or does not exist OS error 111, "Connection refused"
Steps i followed
/etc/freetds/freetdsconf.conf
[SQLEXPRESS]
host = 192.168.1.9
port = 1433
tds version = 8.0
client charset = UTF-8
/etc/odbcinst.ini
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver =/usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup =/usr/lib/i386-linux-gnu/odbc/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 1
etc/odbc.ini
[SQLEXPRESS]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = 192.168.1.9
Database = cakephp_results
Port = 1433
UID = sa
PWD = sa
ReadOnly = No
In your odbc.ini file change 'Servername = ' to 'Server = '. Then remove the UID, PWD and ReadOnly lines from that file temporarily. You may want to remove them altogether and pass in the credentials from your application.
Then, be sure that your MSSQL DB is allowing connections through port 1433, that SQL Server is not blocking the incoming server's IP address, and that you have the correct password.
Also, if that doesn't work, review these troubleshooting instructions:

Resources