Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mysql与本地主机连接正常,但未与ip地址连接_Java_Mysql_Localhost_Ip Address - Fatal编程技术网

Java Mysql与本地主机连接正常,但未与ip地址连接

Java Mysql与本地主机连接正常,但未与ip地址连接,java,mysql,localhost,ip-address,Java,Mysql,Localhost,Ip Address,我有一个java程序,它从MySQL获取信息。当我使用localhost连接到它时,它工作得很好,但每当我将ipaddress放入它时,它就不工作了。 我的连接代码和异常如下 package connection; import java.net.InetAddress; import java.sql.Connection; import java.sql.DriverManager; /** * * @author rpsal */ public class DBConnectio

我有一个java程序,它从MySQL获取信息。当我使用localhost连接到它时,它工作得很好,但每当我将ipaddress放入它时,它就不工作了。 我的连接代码和异常如下

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();
    }
}
Exception in connectjava.sql.SQLException: null,  message from server: "Host 'Rp-Salh' is not allowed to connect to this MySQL server"
当我使用
String url=“jdbc:mysql:///ChatMaster";工作正常。
我得到的例外情况如下

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();
    }
}
Exception in connectjava.sql.SQLException: null,  message from server: "Host 'Rp-Salh' is not allowed to connect to this MySQL server"

正如错误告诉你的,ip地址没有访问这个数据库的权限,我认为这不是你的代码错了

我不知道MySQL是否也一样,但是postgresql I
需要在数据库中定义以允许远程连接。

我认为
inetAddress.getHostAddress()
将返回主机名(suh为Rp-Salh) 所以我建议你使用这种方法

inetAddress.getLocalHost()

正如我从错误日志中看到的。
InetAddress.getLocalHost()未返回正确的IP地址

请尝试通过提供硬编码的IP地址连接它(仅用于测试以确保)


您可以在windows中通过在CMD上键入
ipconfig
来获取系统IP地址。

您需要确保两件事

  • 检查MySQl端口
    3306
    是否已打开。下面是示例远程连接字符串

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

  • 检查数据库用户名和密码是否正确


  • 更新mysql配置文件(可能在服务器文件目录etc/mysql/my.conf中)检查是否配置了127.0.0.1(默认)作为主机地址,并将其更改为您的IP。

    事实证明@Jan.St为我指明了正确的方向,因为问题不在我的代码中,也不在获取IP地址的问题中,只是默认情况下mysql中禁用了远程根访问。我只是在下面的链接中找到了答案,结果成功了


    注意:如果第一个答案本身不起作用,请确保您也遵循同一帖子中的第二个答案。

    我认为SQL Server已配置,来自outsite的所有请求(locahost除外)都会被阻止。我需要它为我网络上的任何系统工作,这是主要问题。您需要更改数据库配置。但我需要为网络上的每个人使用我的数据库。数据库只在一个系统上。我认为本地主机可能不工作端口3306是打开的,用户名也是正确的,因为它与本地主机一起工作,而且我还没有输入任何密码。我检查了您所说的,获得正确的IP地址没有任何问题。我先手动输入ipaddress,然后检查我的函数返回的内容,然后进行了双重检查。感谢man为我指出了正确的方向+1。