This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
                                
                                    (36个答案)
                                
                        
                                2年前关闭。
            
                    
我的Java应用程序出现问题已有一段时间了。为了弄清楚从新驱动程序到注册驱动程序到更改MySQL数据库权限,我尝试了几种方法。我只有在连接外部IP时才会遇到此问题。我认为可能与IPV6有关,但是我试图在树莓派上禁用它,但是当我运行“ netstat -tlnp”时,我得到了:

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN      547/java
tcp        0      0 0.0.0.0:8200            0.0.0.0:*               LISTEN      547/java
tcp        0      0 0.0.0.0:8009            0.0.0.0:*               LISTEN      547/java
tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp6       0      0 :::5900                 :::*                    LISTEN      -
tcp6       0      0 :::8080                 :::*                    LISTEN      -
tcp6       0      0 :::22                   :::*                    LISTEN


这表明MySQL服务器仍在tcp6上运行,端口是8080,我从标准3600更改了它。我的Java主类如下:`public static String IP; // MySQL服务器IP的NULL变量
    public static final String user =“ user”; // mySQL服务器的用户名
    public static final String password =“ pass”; // mySQL服务器TODO的密码了解如何隐藏密码
    静态连接conn; // NULL连接以连接到mySQL服务器
    公共静态扫描仪控制台= new Scanner(System.in); //用于从控制台获取数字和字符串

/**
 * Runs the program
 * @param args
 */
public static void main(String[] args) {
    System.out.print("Internal or External IP (1 for Internal or 2 for external): ");
    int choice = console.nextInt();
    switch (choice){
    case 1:
        IP = "10.0.0.183";
        break;
    case 2:
        IP = "external ipv4 address";
        break;
    }
    //TRY CATCH method to ensure that the mySQL connection was successful
    try{
        conn = DriverManager.getConnection("jdbc:mysql://"+IP+":8080/mydb",user,password);
    }catch(SQLException e){
        System.out.println(e.getLocalizedMessage());
        System.exit(1);
    }`


这是我尝试连接到外部IPV4地址时出现的错误`通信链接失败

成功发送到服务器的最后一个数据包是0毫秒前。驱动程序尚未从服务器收到任何数据包。

有没有人有解决方案或需要我帮忙解决这个问题!谢谢!

最佳答案

您是否使用此10.0.0.183创建了MySQL用户?
请使用它创建MySQL用户并提供授权并进行检查

关于java - 无法从外部IPV4地址连接到MySQL服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53384847/

10-10 00:54
查看更多