如果我这样做:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/imageusers?autoReconnect=true&useSSL=false&relaxAutoCommit=true", "username", "password");
那么它工作得很好,但如果我这样做:
Connection myConn = DriverManager.getConnection("jdbc:mysql://IP:3306/imageusers?autoReconnect=true&useSSL=false&relaxAutoCommit=true", "username", "password");
然后它给了我一长串错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
我从这个网站获得了我的IP地址:http://whatismyipaddress.com/ 并在另一个查找器上仔细检查了它,结果是一样的,所以我认为我输入的 IP 地址没有错误。我还在 Windows 防火墙中对端口 3306 进行了异常(exception)处理,并检查了 MySQL 是否在 Windows 的服务应用程序中运行,确实如此。我也打开并运行了 MySQL 程序。我不知道还能做什么。
最佳答案
您在这里提供的是公共(public)IP。它不会将其转换为私有(private) IP 地址。尝试从连接到数据库服务器的计算机上 ping 到公共(public) IP。这也行不通。我建议您使用私有(private) IP 地址。
关于Java JDBC : MySQLNonTransientConnectionException when trying to connect with IP address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46615873/