Mysql connecting ok with local host but not connecting with ip address

Nnnnn :

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 :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=113894&siteId=1