Search code examples
javamysqlapimariadb-10.1

ConnectionException using Java MariaDB Connector


i want to establish a connection between my Java Program and my Database created with MariaDB on a different server (not localhost). Everything is fine with the ServerIP, Databasename, User and Password. But i get this error:

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

My Code:

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

}}

All the versions i use:

  • MariaDB: 10.1.44

  • MariaDB-Java-Connector: mariadb-java-client.1.2.0.jar

  • Java Version: Version 8 Update 161

  • Eclipse Version: 2019-03 (4.11.0)


Solution

  • Thank you for your answer davidbuzatto but the problem was easier to solve than i expected.

    Here is a Guide how i solved this problem for me:

    Step 1: Check your MySQL-Config File with (nano /etc/mysql/my.cnf in Ubuntu) replacing bind-adress: 172.0.0.1 with bind-adress: 0.0.0.0

    When this doesnt help... For me it didnt help....

    Step 2: Check the Firewall Settings on your Server and allow the access to the MySQL Server

    When this also doesnt help... For me it didnt....

    Step 3: Grant Access to your MySQL-Database

    Use this MySQL-Query: GRANT ALL ON yourDatabasename.* to yourUser@yourIP identified by 'passwordOfTheUser';