1

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?

Community
  • 1
  • 1
yaylitzis
  • 5,354
  • 17
  • 62
  • 107
  • 1
    Your database Server is listening on localhost, you use the external ip address. Change `jdbc:mysql://192.168.0.4:3306/myDataBase` to `jdbc:mysql://localhost:3306/myDataBase` – Jens Mar 30 '17 at 10:23

1 Answers1

2

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.

Community
  • 1
  • 1
freedev
  • 25,946
  • 8
  • 108
  • 125
  • Thanks for all the info! I am not connecting locally so I have to find how I can change where MySQL is listening.. – yaylitzis Mar 30 '17 at 10:31
  • Just edit `my.cfg` file and edit `bind-address = 0.0.0.0` - read [How to Bind to Static IP address in MySQL](http://askubuntu.com/questions/33018/how-to-bind-to-static-ip-address-in-mysql) – freedev Mar 30 '17 at 10:32
  • 1
    Thanks! In `Ubuntu 16.04` the file is located in `/etc/mysql/mysql.conf.d/mysqld.cnf ` – yaylitzis Mar 30 '17 at 10:36
  • @yaylitzis great ;) – freedev Mar 30 '17 at 10:37