SQL server connection failed from application - sql-server

While I run my spring boot application, which is trying to store data into MS SQL server database, it is unable to connect to Microsoft SQL database.
I am getting the following exception while running:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host /sqlexpress, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
In the code, in java class having main method, the bean method "dataSource()" is implemented as shown below:
#Bean
public DataSource dataSource(){
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("jdbc:sqlserver://<system_name>/sqlexpress:1433;databaseName=databasename");
dataSource.setUsername("username");
dataSource.setPassword("password");
return dataSource;
}
I am trying to connect using JdbcTemplate.
In gradle dependency, the following dependencies are added:
compile("org.springframework:spring-jdbc")
compile("com.microsoft.sqlserver:sqljdbc4:4.0")
From Microsoft SQL Server 2014 Management Studio, I could connect to the host system_name/sqlexpress, having port 1433.
I have checked the SQL server configuration manager -->
SQL Server Network Configuration --> Protocols for SQLEXPRESS --> TCP/IP .
It is enabled and in TCP/IP properties, TCP port of IPAll is set to 1433 only.
I couldn't identify why it is not getting connected when connection is trying from springboot application.
It is highly appreciable if you have any possible solution for this MSSQL connection issue.
Thanks

Basically you need to configure the TCP/IP in a hard coded way.
How to do that is very well explaned here configuring SpringBoot for MSSQL :
From the Start menu, open SQL Server 2014 Configuration Manager.
Click Protocol for SQLEXPRESS under SQL Server Network Configuration on the left pane.
On the right pane, right- click TCP/IP, and select Properties.
On the TCP/IP Properties dialog box that appears, click the IP Addresses tab. Scroll down to locate the IPALL node.
Remove any value, if present for TCP Dynamic Ports and specify 1433 for TCP
Port.
Now click OK and restart your SQL Server.

Related

Connection to SQL Server on non standard port using SSMS

I have a SQL Server in RDS in private subnet on port 1433.
I open a SSH tunnel and use local port 21433.
I am not able to connect it using SSMS if I give db host as localhost,21433 or .21433
Please advise what the connection string needs to be in this case, even sqlcmd did not work.
I am able to connect same DB using another clinet "Dbever"
Follow this steps :
Open the SQL Server Configuration Manager and expand the node SQL Server Network Configuration. Then Select Protocols for SQLEXPRESS (or whatever the name of your SQL Server instance is). Make sure that TCP/IP protocol is enabled.
Right click on TCP/IP and choose Properties. In the TCP/IP Properties dialog box, click on the IP Addresses tab and scroll down to IPAII section. Make sure TCP Dynamic Ports is blank and that TCP Port is set to 1433 (or whatever you want to set TCP Port).
3.First Click on Apply button and then the OK button to save your changes. After that restart your SQL Server instance.
Now you are able to connect to your SQL Server instance remotely using SQL Server Management Studio.
If there is problem persist in remote connection, you most likely need to configure your firewall settings. Make sure the TCP port 21433 is not blocked by your firewall.

Connecting to SQLServer with GORM

I am using the statement below to try and open a connection to the latest download of SQLServer Express.
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
"log"
)
db, err := gorm.Open("mssql", "sqlserver://tony:Password6!#localhost:1433?database=go_user")
The statement results in an error:
2019/09/30 10:29:16 Unable to open tcp connection with host
'localhost:1433': dial tcp [::1]:1433: connectex: No connection could
be made because the target machine actively refused it. panic: Unable
to open tcp connection with host 'localhost:1433': dial tcp
[::1]:1433: connectex: No connection could be made because the target
machine actively refused it.
Is anyone successfully connecting to SQLServer Express with GORM on Golang?
TCP port 1433 is the default port for SQL Server. This port is also the official Internet Assigned Number Authority (IANA) socket number for SQL Server. Client systems use TCP 1433 to connect to the database engine; SQL Server Management Studio (SSMS) uses the port to manage SQL Server instances across the network. You can reconfigure SQL Server to listen on a different port, but 1433 is by far the most common implementation.
However, if you still want to open it, follow the below steps:
Step 1
Probably TCP/IP channel is disabled under SQL Server Configuration Manager. SO go there and enable all TCP/IP options.
Step 2
Just in case at the same place SQL Server Configuration Manager make sure you have 1433 port.
Step 3
Make sure that SQL server is configured to allow remote connections. Use MS SQL Management Studio and right click on the top node which server itself.
Credit from the solution found here.

Pentaho Kettle: cannot connect to MS SQL Server Express

I have Microsoft SQL Server 2014 Express running on my local machine, a Windows 7 PC.
I use Python and can connect to the server (from the very same PC) without any problems, using SQL alchemy. I can also connect using Excel and the PowerPivot add-in.
However, I cannot connect with Pentaho - Kettle.
I have downloaded the JDBC driver from Microsoft:
https://www.microsoft.com/en-gb/download/details.aspx?id=11774
I took the file jre8\sqljdbc42.jar and moved it to the lib subfolder of
the Pentaho Data Integration installation. I took the file in jre8, not jre7, because my Java is JRE1.8.0_144
I then took the file in auth\x86\sqljdbc_auth.dll and copied it to the same lib folder. I took the x86 file, not the x64, because Java is in c:\Program Files (x86)\Java\jre1.8.0_144\
I add 'table input' and set connection type= MS SQL Server (native) and Access = Native (JDBC)
The error message I get is:
Error connecting to database [mydatabase] :org.pentaho.di.core.exception.KettleDatabaseException: Error occurred while trying to connect to the database
Error connecting to database: (using class com.microsoft.sqlserver.jdbc.SQLServerDriver) The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
org.pentaho.di.core.exception.KettleDatabaseException: Error occurred while trying to connect to the database
Error connecting to database: (using class com.microsoft.sqlserver.jdbc.SQLServerDriver) The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
I have tried various combinations of specifying port number 1433 and of leaving it blank, of using integrated security and of entering my password manually, of setting host name to localhost and to DESKTOP-MYNAME\SQLEXPRESS, but to no avail.
I have read lots of discussions about it but none seems relevant; the SQL Server does accept incoming connections, e.g. from Python and SQL Alchemy.
I ran the SQL Server 2014 configuration manager, and checked:
SQL Server network configuration --> Protocols for SQLEXPRESS --> TCP/IP
In the protocol tab, I had: enabled: yes and Listen All: Yes
In the "IP Addresses" tab, under IPAll at the bottom, "TCP Dynamic ports" was set to 49178. (No idea why).
So I set the port in the 'table input' setup of Pentaho to 49178. It managed to find the server, but there was an error about integrated security. So I copied sqljdbc_auth.dll to both the bin and the lib subfolders of C:\Program Files (x86)\Java\jre1.8.0_144 .... and now it works!
If I don't specify the port, Pentaho tries port 1433, and it doesn't work.
For reference, the connection string I use with Python and SQL Alchemy does not specify the port explicitly.
params = '?driver=SQL+Server+Native+Client+11.0'
engine = create_engine('mssql+pyodbc://' + ServerName + '/'+ Database + params, encoding ='latin1' )
conn=engine.connect()
SQL Server network configuration --> Protocols for SQLEXPRESS --> TCP/IP
In the protocol tab, I had: enabled: yes and Listen All: Yes
In the "IP Addresses" tab, under IPAll at the bottom, "TCP Dynamic ports" was set to 49178.
So I set the port in the 'table input' setup of Pentaho to 49717.
this worked for me also..

Having trouble with SQL Server connection on WebSphere 8.5

I'm having all kinds of trouble getting IBM WebSphere 8.5 to connect to my SQL Server 2012 data source. I have the URL of jdbc:sqlserver://localhost;userName=user;password=password. See the following error (I know it's kind of long):
The test connection operation failed for data source reachingrecovery on server server1
at node MWEISS7X16Node01 with the following exception: java.sql.SQLException: The TCP/IP
connection to the host localhost, port 1433 has failed. Error: "Connection refused:
connect. Verify the connection properties. Make sure that an instance of SQL Server is
running on the host and accepting TCP/IP connections at the port. Make sure that TCP
connections to the port are not blocked by a firewall."
Now, the SQL server and WebSphere are running on the same machine, so I don't think it's a firewall issue. I've checked the SQL server properties under "Connections", and it indicates that everything is fine ("Allow remote connections to the server" is checked.)
I'm at a loss to understand why this is happening. Any suggestions? Thx Mimi
I found out what the problem was. I was using SQL Server Management Studio to check on whether it could make TCP/IP connections or not. What I should have been using was SQL Server Configuration Manager. It had a thing on the left called "SQL Server Network Configuration". I clicked on that, and it brought up "Protocols for server instance name". It had a thing on the right (TCP/IP), which indicated it was disabled. I enabled that, restarted the SQL server, and it worked.

Firewall Port 1433 not opening

I am using MS Server 2008 with MSSQL 2008-R2 as database server.
Each time for some work i have to login to server via Remote connection.I tried to configure
the SQL Server remote connection on, followed the following steps....
1.Created Inbound and outbound rules for tcp port 1433.
2.In sqlserver configuration manager ,all is set for IP address and POrt no properties.
3.Set the SqlServer instance for remote connections.
But still its not working.
Also when i checked port 1433 for tcp on local computer it is shown closed.
thanks in advance.......
Probably port 1433 is disabled, so enable it using MS WIndows firewall.
or just use
netsh firewall set portopening protocol = TCP port = 1433 name = SQLPort mode = ENABLE scope = SUBNET profile = CURRENT
Probably MS SQL Server Browser Service is not running. So go to Services and start it.
or just execute under Run menu
%windir%\system32\services.msc
Probably TCP/IP channel is disabled under SQL Server 2008/2014 Configuration Manager. SO go there and enable all TCP/IP options.
Just in case at the same place (SQL Server 2008/2014 Configuration Manager) make sure you have 1433 port.
Make sure that SQL server is configured to allow remote connections. Use MS SQL Management Studio and right click on the top node which server itself.
In the SQL Server Configuration Manager->SQL Server Network Configuration->Protocols, check that named pipes and tcp/ip are enabled.
If that doesn't work then it may be your firewall blocking it, try this link: http://msdn.microsoft.com/en-us/library/cc646023.aspx
This may be useful too:
http://support.microsoft.com/kb/287932
If named pipes and TCP/IP access are already enabled in SQL Server Configuration Manager, then you need to either add a rule for UDP port 1434, or a rule for sqlbrowser.exe so that the SSMS client can talk to the SQL Server Browser service.
I actually see people having this problem surprisingly often so I wrote up an explanation here: http://blog.dereenigne.com/microsoft-sql-server-dynamic-ports-windows-firewall-and-you/
If you're trying to connect to an SQL Server Express instance as is my case right now... it doesn't use port 1433 by default. One must check the IpAll => TCP Dynamic Ports in Sql Server Configuration Manager. Mine is 52848.
After entering the correct port 52848 in DbSchema app (the one I'm trying to use), it connected successfully.
Check the following screenshot:
This is the way to go:
The default SQL Server express installation does not enable TCP
network protocols, so here are some steps to take to be able to
communicate with SQL Server Express. These steps should be similar for
MS SQL Server Express 2017, 2016, 2014, 2012, 2008, and 2005.
Launch the SQL Server Configuration Manager. It should be in the Start -> Programs -> Microsoft SQL Server 2005 -> Configuration Tools start menu option.
Select the SQL Server 2005 Network Configuration tab. There should be a Protocols for SQLExpress option, and one of the protocols should be TCP IP.
Enable the TCP IP protocol if it is not enabled.
The default port for SQL Express may not be 1433. To find the port it is listening on, right-click on the TCP IP protocol and scroll all the way down to the IP All heading. There should be a section called TCP Dynamic Ports. This should list the port SQL Express is listening on. You can then put this value into the port field when adding the connection profile via RazorSQL.
Make sure to restart SQL Express before trying to connect.
Text kindly reproduced from:
https://razorsql.com/docs/support_sqlserver_express.html
I had multiple NICs in my server. When I went to SQL Server Network Configuration -> TCP/IP and right clicked for properties there was a list of the NICs and at the bottom was the open port for my SQLEXPRESS. It was not 1433..... I do not know if there is a default but mine was in the 49,000 s.

Resources