Mysql 与本地主机连接正常但无法与 ip 地址连接
Mysql connecting ok with local host but not connecting with ip address
我有一个 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();
}
}
当我使用 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"
正如错误告诉你的那样,ip地址没有访问这个数据库的权限,我认为这不是你的代码错误。
我不知道MySQL是否一样,但是对于postgresql我
需要在数据库中定义以允许远程连接。
我认为 inetAddress.getHostAdress()
将 return 主机名(例如 Rp-Salh)
所以我推荐你使用这个方法
inetAddress.getLocalHost()
正如我从错误日志中看到的那样。 InetAddress.getLocalHost();
没有返回正确的 IP 地址。
请尝试通过提供硬编码 IP 地址来连接它(仅用于测试以确定)。
您可以在 windows 中通过在 CMD 中键入 ipconfig
来获取系统 IP 地址。
您需要确定两件事。
检查MySQl端口3306
是否已经打开。这是示例远程连接字符串
字符串 url = "jdbc:mysql://192.168.1.121:3306/ChatMaster";
检查数据库用户名和密码是否正确。
更新mysql 配置文件(可能在服务器文件目录etc/mysql/my.conf 中)检查它是否配置了127.0.0.1(默认)作为主机地址并将其更改为您的IP。
事实证明@Jan。 St 为我指出了正确的方向,因为问题不在我的代码中或任何获取 ipaddress 的问题,只是默认情况下远程根访问在 mysql 中被禁用。我只是按照以下 link 中的答案进行操作,它起作用了。
How to allow remote connection to mysql
注意:如果第一个答案本身不起作用,请确保您也遵循相同的第二个答案post。
我有一个 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();
}
}
当我使用 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"
正如错误告诉你的那样,ip地址没有访问这个数据库的权限,我认为这不是你的代码错误。
我不知道MySQL是否一样,但是对于postgresql我 需要在数据库中定义以允许远程连接。
我认为 inetAddress.getHostAdress()
将 return 主机名(例如 Rp-Salh)
所以我推荐你使用这个方法
inetAddress.getLocalHost()
正如我从错误日志中看到的那样。 InetAddress.getLocalHost();
没有返回正确的 IP 地址。
请尝试通过提供硬编码 IP 地址来连接它(仅用于测试以确定)。
您可以在 windows 中通过在 CMD 中键入 ipconfig
来获取系统 IP 地址。
您需要确定两件事。
检查MySQl端口
3306
是否已经打开。这是示例远程连接字符串字符串 url = "jdbc:mysql://192.168.1.121:3306/ChatMaster";
检查数据库用户名和密码是否正确。
更新mysql 配置文件(可能在服务器文件目录etc/mysql/my.conf 中)检查它是否配置了127.0.0.1(默认)作为主机地址并将其更改为您的IP。
事实证明@Jan。 St 为我指出了正确的方向,因为问题不在我的代码中或任何获取 ipaddress 的问题,只是默认情况下远程根访问在 mysql 中被禁用。我只是按照以下 link 中的答案进行操作,它起作用了。
How to allow remote connection to mysql
注意:如果第一个答案本身不起作用,请确保您也遵循相同的第二个答案post。