ConnectionException 使用 Java MariaDB 连接器

ConnectionException using Java MariaDB Connector

我想在我的 Java 程序和我在不同服务器(不是本地主机)上使用 MariaDB 创建的数据库之间建立连接。服务器 IP、数据库名称、用户和密码一切正常。 但是我得到这个错误:

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");
    

}}

我使用的所有版本:

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

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

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

第 1 步: 使用(nano /etc/mysql/my.cnf in Ubuntu)检查您的 MySQL-Config 文件 将 bind-adress: 172.0.0.1 替换为 bind-adress: 0.0.0.0

当这没有帮助时...对我来说它没有帮助....

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

当这也没有帮助时......对我来说它没有....

第 3 步: 授予对您的 MySQL-Database

的访问权限

使用这个 MySQL-Query: GRANT ALL ON yourDatabasename.* to yourUser@yourIP identified by 'passwordOfTheUser';