MySQLは、ローカルホストで、[OK]を接続するが、IPアドレスで接続していません

NNNNN:

私はそれに接続するためにlocalhostを使用する場合には罰金を作品のMySQLからその情報を取得するJavaプログラムを持っていますが、私はそれにIPアドレスを入れたときに、それは仕事をしません。次のように私の接続コードと例外があります。

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();
    }
}

私が使用している場合String url = "jdbc:mysql:///ChatMaster";、それが正常に動作します。次のように私は取得しています例外はあります。

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

それは@Janを結局のところ。問題は私のコードではなかったか、IPアドレスの問題を得ることのいずれかが、それはデフォルトのリモートrootアクセスして、MySQLで無効になっているだけのことだった聖さんは正しい方向に私を指摘しました。私は次のリンクで答えを踏襲し、それが働きました。

MySQLへのリモート接続を許可する方法

注:独自に最初の答えが機能しなかった場合は必ずまた同じポストに第二の答えを追従させます。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=224406&siteId=1