This question already has answers here:
java.net.ConnectException: Connection refused
(19 answers)
Closed 3 years ago.
So today I decided my website would run faster if I moved our databases to a different server, so I did, you can see how I am connecting below:
final String driver = "com.mysql.jdbc.Driver";
final String url = "jdbc:mysql://external_ip_address_here:3306/dbname";
final String username = "user";
final String password = "pass";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, username, password);
This works fine when I am connecting to 127.0.0.1 / localhost, now I am getting this error:
java.net.ConnectException
MESSAGE: Connection refused (Connection refused)
My MySQL server is running Ubuntu 18.04.
I added multiple firewall rules to try and allow all incoming connections to port 3306 - sudo ufw allow from any to any port 3306 proto tcp & I tried iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT, still not working, when i port scan the network it shows port 3306 as closed
FIX 1: This fixed the connection refused error -
Step 1: edit file /etc/mysql/mysql.conf.d/mysqld.cnf
Step 2: remove line bind-address = 127.0.0.1
Check the servers firewall configurations. It may be the reason for your problem. Add a new rule that allows connection from your URL in firewall settings.
This fixed the connection refused error -
Step 1: edit file /etc/mysql/mysql.conf.d/mysqld.cnf
Step 2: remove line bind-address = 127.0.0.1
Related
This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 5 years ago.
I try to connect with my DB (MySQL) from a servlet but I get Connection refused error. I read similar questions like this. However:
You have not started your server.
In my local server (Ubuntu server 16.04), with IP 192.168.0.4 is installed MySQL server. MySQL Server is running as the command service mysql status give this output:
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-03-21 11:50:56 EET; 1 weeks 1 days ago
Main PID: 1209 (mysqld)
You are trying to connect to the wrong IP/port.
MySQL is listening to port 3306, as the command netstat -an | grep 3306.
gives the following output
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
A firewall is blocking your connection before it reaches your server.
The server's firewall is disabled, as ufw status
Status: inactive
I disabled my PC's firewall too but it didn't solve the connection error.
Your server has too many pending connections waiting to be accepted.
My server is fresh installed and there is no way to have any pending connections.
Your server is not listening for connections.
I run the commnand nmap -sT -O localhost and I got:
Not shown: 993 closed ports
PORT STATE SERVICE
21/tcp open ftp
22/tcp open ssh
25/tcp open smtp
80/tcp open http
110/tcp open pop3
143/tcp open imap
3306/tcp open mysql
NETBEANS
Me and Server we are in the same network. My Java class which handles the communication with the DB contains:
private final String WEBAPP_DB = "jdbc:mysql://192.168.0.4:3306/myDataBase";
private final String JDBC_DRIVER="com.mysql.jdbc.Driver";
private final String USERNAME = "myUserName";
private final String PASSWORD = "myPassWord";
Connection c = null;
PreparedStatement stm = null;
sq = "SELECT * FROM Machines";
try {
Class.forName(JDBC_DRIVER);
c = DriverManager.getConnection(WEBAPP_DB, USERNAME, PASSWORD);
stm = c.prepareStatement(sq);
ResultSet rs = stm.executeQuery();
while(rs.next()){
System.out.println("Machine = " + rs.getInt("id"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stm != null) {
stm.close();
}
if (c != null) {
c.close();
}
}
and some of the errors I get is
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
...
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
...
Caused by: java.net.ConnectException: Connection refused: connect
Any hint what may be the problem?
If you're trying to connect yo your MySql server remotely, the problem is in the MySql server network configuration, it's listening on localhost (127.0.0.1):
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
It should be:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
Or at least, instead of 0.0.0.0, your server ip address (192.168.0.4).
I suggest to read this How to Bind to Static IP address in MySQL
On the other hand, if you're connecting locally (your code is executed on MySql server, which is the same machine where NetBeans runs) you should change your connection string into jdbc:mysql://localhost:3306/myDataBase
P.S. So at resuming your questionnaire :)
You are trying to connect to the wrong IP.
I'm trying to access a Azure-hosted SQL database on my java application. I checked the port 1433 using nmap and it shows that it's closed:
Starting Nmap 7.12 ( https://nmap.org ) at 2016-09-02 09:44 PHT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00014s latency).
Other addresses for localhost (not scanned): ::1
PORT STATE SERVICE
1433/tcp closed ms-sql-s
I have edited the /etc/pf.conf and restarted my mac but the port is still closed. Here is my pf.conf:
scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"
pass in proto tcp from any to any port 1433
Also, my firewall is set to off.
My java app is throwing this error:
Error starting database: The TCP/IP connection to the host flowengine7.database.windows.net, port 1433 has failed. Error: "Connection timed out: no further information. 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.".
Java code:
String connectionString =
"jdbc:sqlserver://dbName.database.windows.net:1433;database=dbName;user=user##user;password=passwordhere;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
Connection connection = null;
try {
connection = DriverManager.getConnection(connectionString);
println 'connected';
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) try {
connection.close();
} catch (Exception e) {
}
}
You're overlooking the Azure SQL server-level Firewall:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-configure-firewall-settings/
You need to whilelist your Mac's Public IP address in there to be able to connect from local. By default only Azure services can reach 1433/TCP on your Azure SQL instance (permitted by Allow access to Azure Services setting in the Azure SQL Firewall).
If you're getting a new Public IP address every time you reboot your DSL/Cable/Fiber modem you'll need to define a whole range of addresses not just the one (i.e. 174.73.0.0 - 174.73.255.255 vs. listing 174.73.16.180). Hopefully you'll always grab an IP address in the same range.
This question already has answers here:
JDBC connection failed, error: TCP/IP connection to host failed
(9 answers)
Closed 5 years ago.
I want to connect Java class file with SQL server 2008. I have logged in with SQL server authentication use: sa, pass:123456. But i am receiving error in connectivity.
static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String dburl = "jdbc:sqlserver://localhost\\SQL2008:1433;Database=Java";
static String user = "sa";
static String password = "123456";
public static void update() throws Exception{
String sql = "UPDATE Categories SET Id='COM' WHERE Id='LAP'";
Class.forName(driver);
Connection conn = DriverManager.getConnection(dburl, user, password);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
}
public static void main(String[] args) throws Exception {
Basic.update();
}
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1048)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:829)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:712)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Tutorials.jdbc.Basic.update(Basic.java:48)
at Tutorials.jdbc.Basic.main(Basic.java:72)
Probably a little late to answer this question, but doing it for my own benefit.
The actual TCP port to connect to through JDBC can be found in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\MSSQLServer\SuperSocketNetLib\Tcp
Use this value to connect to SQL Server.
For example
"jdbc:sqlserver://localhost\SQLEXPRESS:49922;databaseName=db;user=user;password=secret"
Also ensure that TCP connections are enabled in the server configuration tools.
I think that your dburl may be malformed.
Try this url instead.
String dburl= "jdbc:sqlserver://localhost\\SQL2008:1433;" +
"databaseName=Java;user=sa;password=123456";
Make sure you have Driver jar file and change your dburl to this as mentioned below
try this
String dburl ="jdbc:sqlserver://SQL2008:1433;DatabaseName=Java;user=sa;Password=123456";
Thanks you for your help, i'm fix my problem, it's not error on String dburl= "jdbc:sqlserver://localhost\SQL2008:1433;Database:Java"
it's error port 1433 no connect in SQL server 2008
Where i find the Protocol TCP/IP, if disabled then Enable it Click on TCP/IP, i'm find its properties.
In this properties Remove All the TCP Dynamic Ports and Add value of 1433 to all TCP Port and restart your SQL Server Services > SQL Server
And Its Done...
Thanks all for your help!
TCP/IP Connection Refused.
Run NETSTAT -A to see a list of ports that are open (they'll say LISTENING) If 1433 isn't among them, then SQL server isn't running or isn't listening on that port. Follow Michael Todd's advice from there.
If 1433 is on the list, check your local firewall to see if it allows connections to/from localhost and/or allows connections to 1433.
You can try TELNET 1433 and see if it connects (you should get a blank screen with a cursor blinking at the top left.) (You may have to enable the telnet client in Programs and Features.) If so then you have a problem with your code.
I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication, but I am receiving an error when connecting.
Error:
The TCP/IP connection to the host 127.0.0.1, 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.".
My code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
//"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" + "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set
while (rs.next())
{
System.out.println(rs.getInt(1));
}
Open SQL Server Configuration Manager, and then expand SQL Server 2012 Network Configuration.
Click Protocols for InstanceName, and then make sure TCP/IP is enabled in the right panel and double-click TCP/IP.
On the Protocol tab, notice the value of the Listen All item.
Click the IP Addresses tab:
If the value of Listen All is yes, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item under IPAll.
If the value of Listen All is no, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item for a specific IP address.
Make sure the TCP Port is 1433.
Click OK.
Easy Solution
Got to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager ->Expand SQL Server Network Configuration-> Protocol ->Enable TCP/IP Right box
Double Click on TCP/IP and go to IP Adresses Tap and Put port 1433 under TCP port.
The error is self explanatory:
Check if your SQL server is actually up and running
Check SQL server hostname, username and password is correct
Check there's no firewall rule blocking TCP connection to port 1433
Check the host is actually reachable
A good check I often use is to use telnet, eg on a windows command prompt run:
telnet 127.0.0.1 1433
If you get a blank screen it indicates network connection established successfully, and it's not a network problem. If you get 'Could not open connection to the host' then this is network problem
Go to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager.
If you see that SQL Server/ SQL Server Browser State is 'stopped'.Right click on SQL Server/SQL Server Browser and click start.
In some cases above state can stop though TCP connection to port 1433 is assigned.
Open %windir%\System32 folder and find SQLServerManagerXX.msc
For example:
C:\Windows\System32\SQLServerManager14.msc
Go to protocols settings then enable TCP/IP port is 1433 by default
As the error says, you need to make sure that your sql server is running and listening on port 1433. If server is running then you need to check whether there is some firewall rule rejecting the connection on port 1433.
Here are the commands that can be useful to troubleshoot:
Use netstat -a to check whether sql server is listening on the desired port
As gerrytan mentioned in answer, you can try to do the telnet on the host and port
important:
after any changes or new settings you must restart SQLSERVER service. run services.msc on Windows
If you are using a named instance, the port you using likely is 1434, instead of 1433, so please check that out using telnet or netstat aforementioned too.
Open Firewall GUI,Open TCP 1433 rule,go to scope tab and add your IP address under Remote IP Address and the problem is solved.
I am running a SQL Server Express 10.50.4000.0 on my machine.
I have enabled on the TCP/IP and made sure the port is set to 1433.
I have added an exception in my firewall for 1433.
The server service is running.
I have tried localhost and the computer name and both give the same error.
When i try localhost\SQLEXPRESS or [USER-PC]\SQLEXPRESS I received an error saying it could not find the database.
String driverName = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driverName);
String serverName = "localhost";
String instanceName = "ALLEN-PC\\SQLEXPRESS";
String serverPort = "1433";
String database = serverName +":" + serverPort+"/"+instanceName;
String url = "jdbc:jtds:sqlserver://" + database;
String username = "sa";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
I receive the error:
Could not connect to the database Network error IOException: Connection refused: connect
with no additional context to investigate.
Make Sure that your SQL Server Browser service is active.
Goto AllProgram->SQL Server ->configuraton tools->SQL Server Configuration Manager->SQL Server Network Configuration-> Select your server instance and right click on TCP/IP then goto IPAddresses. Change/Place port number for all IP's as 1433. Also make sure that the IP Address is Active and Enabled. Save it and restart the server.
There is no database server listening on localhost:1433
Check that the SQL Server Browser Service is running
So maybe using the ip address instead of localhost helps.
Your connection string needs to be in this format. Found here: http://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx
jdbc:jtds:sqlserver://<yourDBServerIPAddress>\SQLEXPRESS:1433;databaseName=AdventureWorks;user=sa;password=*****;
Open the Configuration Manager:
Start -> Microsoft SQL Server -> Configuration Tools -> SQL Server Configuration Manager
Enable the TCP/IP: from the left hand tree choose:
SQL Server Network Configuration-> Protocol for SQLEXPRESS-> TCP/IP
Right click and enable it.
Double click the TCP/IP and click on the “IP Addresses” Tab
Adding TCP/IP port : Enter TCP Port value to 1433 then click apply
Restart SQL Server: from the left hand tree, choose:
SQL Server Services -> SQL Server (SQLEXPRESS) -> right click and restart.
Solution 1:
Instead of using jtds driver use mssql-jdbc-9.4.0.jre8.jar
Replace:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = DriverManager.getConnection("jdbc:jtds:sqlserver://<IP>:port/<DatabaseName>",userid,password);
With:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.getConnection("jdbc:jtds:sqlserver://<IP>:port/<DatabaseName>",userid,password);
Solution 2:
Add ssl=request parameter in the connection string
con = DriverManager.getConnection("jdbc:jtds:sqlserver://<IP>:port/<DatabaseName>;ssl=request",userid,password);