Issues connecting to Docker MSSQL using pyodbc - sql-server

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

Related

Docker in WSL - Can't connect to SQL server on 'localhost' but with '127.0.0.1' SQL Server 2017-latest

Trying to spin up an mssql server with a docker-compose file:
version: "3.7"
services:
sqlserver:
image: mcr.microsoft.com/mssql/server:2017-latest
container_name: sqlserver
user: root
ports:
- "1433:1433"
environment:
SA_USERNAME: sa
SA_PASSWORD: Dev1234!
ACCEPT_EULA: Y
MSSQL_AGENT_ENABLED: 'true'
volumes:
- ./volumes/data2:/var/opt/mssql/data
- ./volumes/backup:/var/opt/mssql/Backup
The server is up and running successfully, But when tried to connect with powershell script code:
$connectionString = "Data Source=localhost,1433;User ID=sa;Password=Dev1234!;"
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open();
It throws error like:
"A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name
is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - The
remote computer refused the network connection.)"
That is, it fails when trying to connect with 'localhost' as host, it is successfully connecting when "127.0.0.1" is specified as host.
The same happens when I tried to connect with SQL server management studio.
My docker engine is running in wsl, an ubuntu distro specifically. There is MySQL and different other servers already spun up which I can access with localhost.
Is there anything I need to specify in docker-compose to make this work? When checking documentation, there are no params aligned to it as I see.

SQL Server not initating a DB on docker deployment

I know this has been covered multiple times, but I still cannot execute an SQL script on docker deployment.
docker-compose
version: '3.9'
services:
mssql:
build: ./docker/mssql
container_name: db
ports:
- 1433:1433
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=Welcome#toSQL2022
- MSSQL_PID=Developer
volumes:
- ./docker/mssql/db-data:/var/opt/mssql/data
- ./docker/mssql/scripts:/var/opt/mssql/scripts
networks:
- supernet
networks:
supernet: {}
Dockerfile
FROM mcr.microsoft.com/mssql/server:2019-CU15-ubuntu-20.04
COPY ./create-from-mdf.sql .
EXPOSE 1433
#tried this: CMD ["/opt/mssql-tools/bin/sqlcmd", "-i", "create-from-mdf.sql", "-S", "localhost", "-U", "SA", "-P" "Welcome#toSQL2022"]
#tried this: CMD sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql#
CMD /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql
I get this error:
db | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
db | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
db | Sqlcmd: Error: 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..
db exited with code 1
However, if I connect to container via CLI the same command gets executed succefully:
/opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql

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

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

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.

I can't connect to SQL Server inside container

This is my docker-compose.yml file:
version: "3.9"
services:
api:
# configuration of API here
db:
image: mcr.microsoft.com/mssql/server
restart: always
container_name: Database
volumes:
- ./Data:/var/opt/mssql
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=lksU2o412f7tBj58t07B
- MSSQL_PID=Express
ports:
- 1433:1433
command: >
sh -c
"
tail -f /dev/null
"
As you can see, I'm using the official docker image for MS SQL Server, mcr.microsoft.com/mssql/server.
My laptop is Ubuntu 20.04 LTS.
I run docker-compose up -d and then docker exec -it db sh to get interactive shell.
Then I write this command:
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'lksU2o412f7tBj58t07B'
And I get this response:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: 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..
This is the official image and I have not changed anything.
Also in my api container, I can ping db or Database. But I can't telnet port 1433.
Also from my host machine I can telnet localhost 1433, but I get this response:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
This is a terrible experience in working with containers from Microsoft. Aren't containers supposed to work out of the box? Isn't container technology there only to solve these stupid problems?
What should I do?

Resources