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?