2

I have a java program which takes its information from MySQL it works fine when I use localhost to connect to it but whenever i put ipaddress in it it does not work. My connection code and exception are as follows.

package connection;

import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 *
 * @author rpsal
 */
public class DBConnection {

    public static Connection connect()//working  on local host
    {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://"+getIpAddress()+"/ChatMaster";
            conn = DriverManager.getConnection(url, "root", "");
        } catch (Exception e) {
            System.out.println("Exception in connect" + e);
        }
        return conn;
    }

    public static String getIpAddress() throws Exception {
        InetAddress inetAddress = InetAddress.getLocalHost();
        return inetAddress.getHostAddress();
    }
}

When i use String url = "jdbc:mysql:///ChatMaster"; it works fine. The exception i am getting is as follows.

Exception in connectjava.sql.SQLException: null,  message from server: "Host 'Rp-Salh' is not allowed to connect to this MySQL server"
Nnnnn
  • 133
  • 3
  • 15

6 Answers6

2

As the error tells you, that the ip Adress hasn't the rights to access this database, I think it is not your code which is wrong.

I don't know if it is the same for MySQL, but for postgresql I needed to define in the database to allow remote connections.

Jan. St.
  • 31
  • 4
1

I think inetAddress.getHostAdress() will return the host name (suh as Rp-Salh) So I recommend you to use this method

inetAddress.getLocalHost()
Julian
  • 1,592
  • 1
  • 13
  • 33
  • But I need to use my database for everyone on my network. And database is only on one system. What I think local Host may not work – Nnnnn Nov 23 '18 at 07:20
1

As I can see from the error log. The InetAddress.getLocalHost(); is not returning the correct IP address.

Please try connection it by providing hard-coded IP address (Just for testing to get sure).

You can get system IP address in windows by typing ipconfig on CMD.

Deepak Kumar
  • 1,246
  • 14
  • 38
  • I checked what you said and there isn't any problem with getting the right ipaddress. I double checked first by manually putting the ipaddress and then checking what my function was returning. – Nnnnn Nov 23 '18 at 08:03
1

You need to make sure 2 things.

  1. Check MySQl port 3306 is already opened or not. Here are sample remote connection String

    String url = "jdbc:mysql://192.168.1.121:3306/ChatMaster";

  2. Check database user name and password is correct.

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
  • Port 3306 is open and username is also correct as it works with localhost and I haven't put any passwords yet. – Nnnnn Nov 23 '18 at 07:22
1

Update mysql config file (probably in server file directory etc/mysql/my.conf) check if it is configured with 127.0.0.1(default) as a host address and change it to your IP.

1

As it turns out @Jan. St 's pointed me to the right direction as the problem wasn't in my code or any of getting ipaddress problem it was just that by default remote root access is disabled in mysql. I just followed the answer in the following link and it worked.

How to allow remote connection to mysql

Note: make sure you also follow 2nd answer in the same post if first answer on its own did not work.

Nnnnn
  • 133
  • 3
  • 15