Connect host machine's SQL Server 2017 from homestead vagrant - sql-server

I'm trying to connect my local/host machine SQL Server from Laravel vagrant homestead. I've been googling all day trying to connect the host machine's database installing different PHP packages but so far have no lucks. I thought I would remove PHP at this point and try to establish the connection inside homestead using sqlcmd instead.
So, I have installed sqlcmd in homestead and tried to execute the following command to connect to the host machine's database.
sqlcmd -S 10.0.2.2,1433 -U sa -P 'password' -Q "sp_databases"
However, I received the following error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection because an error was encountered during handshakes before login. Common causes include client attempting to connect to an unsupported version of SQL Server, server too busy to accept new connections or a resource limitation (memory or maximum allowed connections) on the server..
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection due to prelogin failure.
However, If I use sqlcmd on my host machine and execute the following command, I am able to access the database.
sqlcmd -S LAPTOP-ACQES\MSSQL2017 -U sa -P Asdf1234 -Q "sp_databases"
In my Homestead.yaml, I have set up port forwarding from:
ports:
- send: 1433
to: 1433

You say this doesn't work:
sqlcmd -S 10.0.2.2,1433 -U sa -P 'password' -Q "sp_databases"
Yet this does:
sqlcmd -S LAPTOP-ACQES\MSSQL2017 -U sa -P Asdf1234 -Q "sp_databases"
In the former, you are using the IP, but no instance name.
Try:
sqlcmd -S 10.0.2.2\MSSQL2017 -U sa -P 'password' -Q "sp_databases"
Also:
Ensure TCP/IP is enabled in SQL Network Configuration.
Verify SQL Server Browser is running.
Allow remote connections in SQL Server properties.
Lock SQL Network configuration to port 1433 on all adapters, and disable dynamic ports (then restart services).

Related

Cannot connect to custom SQL Server docker image

we're currently using Windows containers and, as there are no official SQL Server images for Windows, we are building our own.
We've successfully created the image and by running via
docker run --name SQLServer2022preview -e ACCEPT_EULA=Y -e sa_password=PaSSw0rd -p 1433:1433 -d mycompany:winsrv1809-sql2022preview
I can connect and work as expected to localhost,1433. However, this image will act as a base and we need to build another image based on this on runtime. When doing so, is where it can't connect. We're getting this message
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server [2]. .
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
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..
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; sqlcmd -i "C:\restore.sql"' returned a non-zero code: 1
This is the docker file
FROM mycompany:winsrv1809-sql2022preview
EXPOSE 1433
EXPOSE 4338
COPY ./databases/DB1.bak C:/
COPY ./HAS_Database/restore.sql C:/
RUN sqlcmd -i "C:\\restore.sql"
and restore.sql
RESTORE DATABASE DB1
FROM DISK = N'C:\DB1.bak'
WITH FILE = 1,
MOVE N'DB1_Imported'
TO N'C:\DB1.mdf',
MOVE N'DB1_log'
TO N'C:\DB1_log.ldf',
NOUNLOAD,
STATS = 5;
GO
There's no login errors in sql log looking into the container (which automatically closes after aprox 30 sec) so I guess that it may be relative to port forwarding. Can anyone help?
I tried port forwarding in the start step, with no luck. I'm a docker newbie

DotNetCore Docker container could not connect to mssql

I have containerized our DotNetCore application.
Our application code connects to different SQL Server instances
We are observing a weird error that our Application is not able to connect to a particular SQL Server instance.
Any connection trying to hit the SQL will go forever in Sending form.
I tried the below to identify the error.
Debug my Docker container locally and install SQL CMD to check if I am able to connect to SQL Server
Below is the error that is occurring
root#aabcd:/app# sqlcmd -S abcinstnacename,3431 -U username -p -Q "SELECT ##VERSION"
Password:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
SQL Server Version that is causing the error
Microsoft SQL Server 2016 (SP2-CU2-GDR) (KB4458621) - 13.0.5201.2 (X64)
In Docker file below are the base images I am using, will that cause any issue ?
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
Also tried adding below line in my Dockerfile with no luck!
RUN sed -i '/^ssl_conf = ssl_sect$/s/^/#/' /etc/ssl/openssl.cnf
Please share your thoughts on this

How to fix Microsoft ODBC Driver 17 for SQL Server ssl_choose_client_version:unsupported protocol on debian?

I have installed sql server developer edition on my debian 10 linux, and I can connect to my localhost with no problem. I have a database which I could connect to it by a specific connection, for this purpose I have to connect to a gateway through pptp vpn, but when I want to connect with this command at my terminal:
# sqlcmd -S <server name> -U <username> -P <password>
I face with these messages:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol].
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
Do you know how can I fix it?
Thanks.

How to connect to SQL Server Developer (windows edition) in a container on Win10 from outside the container

I got the docker image for SQL Server Developer edition and started it up:
PS C:\WINDOWS\system32> docker ps -aa
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81347866fc2b microsoft/mssql-server-windows-developer "powershell -Command…" About an hour ago Up About an hour (healthy) 0.0.0.0:1433->3341/tcp trusting_mccarthy
PS C:\WINDOWS\system32>
It's listening on port 3341 as you can see. the IP is:
PS C:\WINDOWS\system32> docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 8
172.17.234.173
HOwever I cannot connect to it from the host:
PS C:\WINDOWS\system32> sqlcmd -S 172.17.234.173,3341
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: An attempt was made to access a socket in a way forbidden by its access permissions.
.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 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..
Must be doing something wrong. any hints?
PS: I can't connect to 1433 either:
PS P:\> sqlcmd -S 172.17.234.173,1433
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: An attempt was made to access a socket in a way forbidden by its access permissions.
.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 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..
PS P:\>
Docker Run command used:
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong!Passw0rd>" `
-p 1433:1433 --name sql1 `
-d mcr.microsoft.com/mssql-server-windows-developer
note I've started with 1433:3341 as well. No joy either way

sqlcmd works in CMD and not in bash

I am having quite a few problems making MSSQL drivers work in Ubuntu. I have followed the following tutorial to make sqlcmd work in Ubuntu 16.04.
# In CMD:
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
1> select name from sys.databases
2> go
After installing the same tool in Ubuntu, it seems to work, but it timeouts when attempting to connect to the same database:
# In Ubuntu bash
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
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 0x2AFA.
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..
I have tried to change configurations of the database, such that the port is static at 1433, but still no luck.
Do you have any suggestions?
I have fixed the problem!
1. Install MSSQL drivers on Ubuntu
Follow this tutorial.
2. Ensure your port on the database is static
Follow this tutorial to set up a static port.
3. Identify the IP adress of your database
I had to call the following code on the database to get the ip-adress: local_net_address.
SELECT
+ CONNECTIONPROPERTY('net_transport') AS net_transport,
+ CONNECTIONPROPERTY('protocol_type') AS protocol_type,
+ CONNECTIONPROPERTY('auth_scheme') AS auth_scheme,
+ CONNECTIONPROPERTY('local_net_address') AS local_net_address,
+ CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port,
+ CONNECTIONPROPERTY('client_net_address') AS client_net_address
4. Connect to database (in Ubuntu bash)
Here are two examples with netcat and sqlcmd.
# Using MSSQL tool
sqlcmd -S my_server_ip_adress//my_server_name,my_port -U my_username -P my_password -d my_database
# Using netcat
nc -z -v -w5 my_server_ip_adress my_port

Resources