使用Java MariaDB连接器的ConnectionException



我想在我的Java程序和我在不同服务器(而不是localhost(上用MariaDB创建的数据库之间建立连接。ServerIP、Databasename、User和Password都很好。但我得到了这个错误:

Exception in thread "main" java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=MY_SERVER_IP)(port=3306)(type=master) : Connection timed out: connect
at org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:136)
at org.mariadb.jdbc.internal.SQLExceptionMapper.throwException(SQLExceptionMapper.java:106)
at org.mariadb.jdbc.Driver.connect(Driver.java:106)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MySQL_Tester.LoadDriver.main(LoadDriver.java:11)
Caused by: org.mariadb.jdbc.internal.common.QueryException: Could not connect to address=        (host=MY_SERVER_IP)(port=3306)(type=master) : Connection timed out: connect
at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connectWithoutProxy(MySQLProtocol.java:629)
at org.mariadb.jdbc.internal.common.Utils.retrieveProxy(Utils.java:541)
at org.mariadb.jdbc.Driver.connect(Driver.java:101)
... 3 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:288)
at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connectWithoutProxy(MySQLProtocol.java:624)
... 5 more

我的代码:

package MySQL_Tester;
import java.sql.*;
public class LoadDriver {
static Connection sqlHandler = null;
public static void main(String[]args) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Class.forName("org.mariadb.jdbc.Driver").newInstance();
sqlHandler=DriverManager.getConnection("jdbc:mysql://MY_SERVER_IP/DATABASENAME?user=USERNAME&password=PASSWORD");

}}

我使用的所有版本:

  • MariaDB:10.1.44

  • MariaDB Java连接器:MariaDB-Java-client1.2.0.jar

  • Java版本:版本8更新161

  • Eclipse版本:2019-03(4.11.0(

您必须用实际值更新连接字符串。

谢谢你的回答davidbuzatto,但这个问题比我预期的更容易解决。

以下是我如何为我解决这个问题的指南:

步骤1:使用(Ubuntu中的nano/etc/MySQL/my.cnf(检查MySQL配置文件将绑定地址172.0.0.1替换为绑定地址0.0.0.0

当这没有帮助的时候。。。对我来说没有帮助。。。。

步骤2:检查服务器上的防火墙设置,并允许访问MySQL Server

当这也没有帮助的时候。。。对我来说,它没有。。。。

第3步:授予MySQL数据库访问权限

使用此MySQL查询:GRANT ALL ON yourDatabasename.*以yourUser@yourIP由"passwordOfTheUser"标识;

相关内容

  • 没有找到相关文章

最新更新