0

Hello to the beloved StackOverflow Community.

So, I am new to Java and MySQL development and, long story short, I'm trying to make devices communicate over the internet using a MySQL database (and the tools provided by MySQL Workbench (v5.6) ), my PC as a server and a Java programm that I created. And all that with no providing a domain name, but my server IP. I've done my research, but now I'm in the point where I have to cry for help.

The Problem: It seems that all are working JUST FINE when my connect variable gets connection from localhost:3306, BUT I just want to access it from the internet and I tried various things that just didn't work out at all.

Failure 1 - I tried to manipulate the my.ini file on C:\ProgramData\MySQL Workbench 5.6\ setting the bind-address to 127.0.0.1, then to my IPv4 I got from ipconfig (CMD) and then to 'mypublicip' from http://www.whatismypublicip.com/ .

and it didn't work.

Failure 2 - I tried to edit the connection from MySQL Workbench -> Hostname= 'mypublicip', port=3306, Username and Password as it was when I was testing it with Hostname=localhost.

That just didn't work for the MySQL Workbench. I could not connect to my server from Workbench and when I tried to set up the Remote Management dialog I didn't know what works and what doesn't.

Failure 3 - I tried keeping as Hostname=localhost at Workbench and I changed the getConnection() method to return connection from the 'mypublicip' ip. I couldn't work with my IPv6 (I don't know how to use it in getConnection()), so I used a public id I found from http://www.whatismypublicip.com/ . When I did that an Error Occured saying this: "Cannot connect to database server. 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." . That's why I tried the failure no.1

That's the code from my Java App that creates the connection between the programm and the db. connectMessage is a JFrame that gives the suitable message to the user about the success or the failure of the connection, the error code and the error message:

package aPackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
*
* @author manosmer
*/
public class MySQLConnector {
    private Connection connect;

    public MySQLConnector(){
        connect = null;
    }

    public Connection connecting(UsersInfo user) throws ClassNotFoundException{
        try{
            Class.forName("com.mysql.jdbc.Driver");           
            connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/connectmessages", user.getName(), user.getPassword());

            connectMessage messageconn = new connectMessage();
            messageconn.setConnection(connect, user);

        }catch(SQLException ex){
            UserLogin errorlogin = new UserLogin();
            connectMessage messageconn = new connectMessage();
            messageconn.failed(ex);
        }
        return connect;
    }  
}
  • 2
    `128.0.0.1`? hopefully that's just a typo... if you meant 127.0.0.1, then that's NOT a valid "internet" address. it's not routeable, and is intended to allow a machine to talk to itself. you cannot talk to another machine's 127.0.0.1, because ALL ip machines are automatically 127.0.0.1 – Marc B Jan 14 '16 at 21:24
  • 1
    you have a problem connecting remotely and local works just fine, so why are you pasting code from your local connection? paste the code that you have issues with. – eis Jan 14 '16 at 21:27
  • @MarcB yeah I did a typo there.... I know that, that's why it worked... you are missing the point I think – Manos Mertzianis Jan 14 '16 at 21:39
  • @eis I'm posting the code that has actually a problem..... you just have to replace 'localhost' in `getConnection()` with the 'mypublicip'. That's when the problem is born – Manos Mertzianis Jan 14 '16 at 21:41
  • so have you actually enabled remote connections in your mysql configuration? enabled networking, granted remote access to your user etc? – eis Jan 14 '16 at 22:07
  • @eis right now I have `Hostname` equal to "localhost", server is up and running and I've set the User's privileges with `from Host` equals "%" and DBA privileges. Is that what you're asking? Sorry for my lack of knowledge.... I'm really new at this thing – Manos Mertzianis Jan 14 '16 at 22:12

3 Answers3

0

If your sql server behind router, try to reroute traffic from some router port to your server MySql port.

eg04lt3r
  • 2,467
  • 14
  • 19
0

I found the problem! The real problem was that I did not open a port at my router firewall for the program to have access via internet! Go to your Internet Wizard, make a port (the same your MySQL Server uses) and apply it to your server machine. Then you have to find your IP and use it in your code. That's all! Happy coding.

0

Everything you have tried so far is the exact opposite of what you should have been doing. Causing the MySQL server to bind to 127.0.0.1 or 'localhost' will prevent any connection from outside the localhost.

What you need to do is have it bind to 0.0.0.0 and open port 3306 in your firewall.

user207421
  • 305,947
  • 44
  • 307
  • 483