Unable to connect to SQL Server inside docker container - sql

I have 2 docker containers (inside one of them I have an application, inside another SQL Server).
I tried to connect from the docker-container with application to the SQL Server database inside another container.
For this I did:
root#application:/# sqlcmd -S 172.17.0.1 -U sa -P test
After I got an issue:
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..
When I used this option inside docker-container with the SQL Server database without -S 172.17.0.1, it works fine.
Any ideas how to connect from container with application to the container with database via sqlcmd?
Update
I was mistaken, I used pass this command from docker-container with database.
Still have a problem with connection from app to the database

One way. The most "trust worthy" way IMHO.
You have to expose your sql-server as a SERVICE.
apiVersion: v1
kind: Service
metadata:
name: mssql-service-deployment
#namespace: mycustomnamespace
spec:
selector:
app: mssql
ports:
- protocol: TCP
port: 1433
targetPort: 1433
type: LoadBalancer
..
Then your connection string becomes:
jdbc:sqlserver://mssql-service-deployment.default.svc.cluster.local;DatabaseName=MyDB;"
where ".default." is the NAMESPACE of the exposed service.
Note the repeat (same string value) of "mssql-service-deployment"
This is documented here:
https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#namespaces-and-dns
When you create a Service, it creates a corresponding DNS entry. This
entry is of the form
..svc.cluster.local, which means that if
a container just uses , it will resolve to the service
which is local to a namespace. This is useful for using the same
configuration across multiple namespaces such as Development, Staging
and Production. If you want to reach across namespaces, you need to
use the fully qualified domain name (FQDN).

Related

Connecting to local MS SQL Server from a docker container on Ubuntu 18.04

I have a VPS running Ubuntu 18.04. The server is running an ASP.NET Core application in a docker container and SQL Server Express 2019 is NOT in a container. I am trying to access a database from an application.
My connection string:
Server=host.docker.internal,1433;User ID=SA;Password=mypass;Database=mydb
In application
services.AddDbContext<UniversityContext>(options =>
options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("ConnectionString")));
I get an error
SqlException: 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: 35 - An internal exception was caught)
Running a container:
docker run -d -p 228:80/tcp myrepos/myproject
ASP.NET Core application successfully connecting to SQL Server from my PC
Also, I tried
docker run -d -p 228:80/tcp myrepos/myproject --network="host"
and 127.0.0.1 or localhost instead of host.docker.internal and get similar error but with another code
SqlException: 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: 40 - Could not open a connection to SQL Server)
UPDATED
If I'm not mistaken SQL Server is already listening 1433

Issue connecting to SQL Server with new ASP.NET Core application

I've deployed a new .NET Core app to a production web server, and I'm having trouble connecting to a SQL Server database on another server. The web server currently has applications running on ASP.NET web forms and connecting to the database successfully.
The application is developed in ASP.NET Core 2.1. The database server is running SQL Server 2012 Express. The database is in a named instance called SQLEXPRESS which is using dynamic ports. I prefer not to change that since it's a production environment.
Here is the connection string that I'm using in my new app (potentially sensitive info omitted)
"ConnectionStrings": {
"NLogDb": "Server=<server>\\SQLEXPRESS;Database=<database>;User Id=<user>;Password=<password>;",
"DefaultConn": "Server=<server>\\SQLEXPRESS;Database=<database>;User Id=<user>;Password=<password>;"
},
I know that the servers are able to talk to one another because I currently have running applications talking back and forth. I also used portQry to test from the web server and got the following:
c:\PortQryV2>portqry.exe -n intinsap01 -p UDP -e 1434
Querying target system called:
<servername>
Attempting to resolve name to IP address...
Name resolved to 10.190.190.10
querying...
UDP port 1434 (ms-sql-m service): LISTENING or FILTERED
Sending SQL Server query to UDP port 1434...
Server's response:
ServerName <serverName>
InstanceName SQLEXPRESS
IsClustered No
Version 11.0.6020.0
tcp 65069
np \\<serverName>\pipe\MSSQL$SQLEXPRESS\sql\query
ServerName <serverName>
InstanceName RAPIDLOG
IsClustered No
Version 11.0.6020.0
tcp 1433
np \\<serverName>\pipe\MSSQL$RAPIDLOG\sql\query
Z ΒΆ
==== End of SQL Server query response ====
UDP port 1434 is LISTENING
The other application is using the following for a connection string:
<add name="<name>"
connectionString="Data Source=<server>\SQLEXPRESS;Initial Catalog=<database;Persist Security Info=True;User ID=<username>;Password=<password>"
providerName="System.Data.SqlClient" />
When the application tries to query the database I get the following error:
System.Data.SqlClient.SqlException (0x80131904): 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I suspect an issue with my connection string, but I'm not sure where to go with it.
Any help is appreciated.
I figured out what my problem was. I kept trying different variations in my connection string in my appsettings.json file. Eventually I noticed that there was an additional file named appsettings.production.json. The connection string in that was improperly formatted (not sure what happened there). The connection string that I originally posted was the right one.

SQL Server - Close external port and work as localhost when sql server and application are on the same server

My database and my web application are at the same server at the moment and I would like to access SQL Server as localhost and close the SQL Server port for remote connections.
This is my current connection string, how can I modify it so it will access the database from as localhost?
"ConnectionStrings": {
"DefaultConnection": "Server=tcp:152.242.211.112;Initial Catalog=testdb;User ID=usera;Password=********"
}
I tried:
server=(local)
tcp=127.0.0.1
Data Source=(local)
using Data Source=(local)\\SQLEXPRESS
I got this message:
A connection was successfully established with the server, but then an
error occurred during the login process. (provider: Shared Memory
Provider, error: 0 - No process is on the other end of the pipe.)
SQL Server uses the magic string (local) instead of localhost - this is because SQL Sever can use transports other than TCP/IP for connecting to the server instance (like Named Pipes and Shared Memory) whereas the hostname localhost is specific to TCP/IP.
So use Data Source=(local);Initial Catalog=... in your connecting string.

Status : Failure -Test failed: IO Error: An existing connection was forcibly closed by the remote host

I have recently downloaded the oracle database 12c enterprise edition. So I got Oracle SQL developer installed with the package. I started to create new database connection as given in the documentation.
My problem is....
When I try to test or connect with the following field values:
Hostname: localhost
Port: 1521
SID: orcl (I have tried 'xe' as well but the error persists.)
I get the following error:
Status: Failure -Test failed: IO Error: An existing connection was forcibly closed by the remote host, connect lapse 15080 ms., Authentication lapse 0 ms.
I have also tried to use different port numbers but the issue remains the same. Don't know what went wrong.
If local server sqlplus connection is fine, and error occurs only for remote client such as sql developer, check your sqlnet.ora file to see if it has tcp.validnode_checking = yes. Valid Node Checking allows or denies access from specified IP addresses to Oracle services. Set tcp.validnode_checking = no and restart database listener should solve issue.
Change the connection setting to Connection Type=Basic and use SID (not Service Name).
If you use Connection Type=TNSNAMES or Service Name instead of SID - the connections will be dropped.

SQL Server Remote Access - Can't Connect

I've tried enabling TCP/IP - setting tcp port - restarting sql service - creating firewall inbound rule for the port - allowing remote connections on sql server - I'm able to ping - server name: tcp:, with correct credentials - server name: , and still not working.
What am I missing?
Error I'm getting:
Cannot connect to <server name>
ADDITIONAL INFORMATION:
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 wait operation timed out.) (Microsoft SQL
Server, Error: 258)
If you are not setting the domain users then make sure you are using SQL Server authentication and either set up local user in the database or simply use the sa user (not recommended)
Because you are not specifying the SQL Server edition, please take a look this link for enabling remote access if you are using SQL Server 2005