英文:
ConnectionException using Java MariaDB Connector
问题
我想在我的Java程序和在不同服务器上(而不是本地主机)使用MariaDB创建的数据库之间建立连接。关于服务器IP、数据库名称、用户和密码的所有信息都是正确的。
但是我遇到了以下错误:
Exception in thread "main" java.sql.SQLNonTransientConnectionException: 无法连接到地址=(host=MY_SERVER_IP)(port=3306)(type=master) : 连接超时
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: 无法连接到地址=(host=MY_SERVER_IP)(port=3306)(type=master) : 连接超时
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: 连接超时
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-Connector版本: mariadb-java-client.1.2.0.jar
- Java版本: 版本 8 更新 161
- Eclipse版本: 2019年03月 (4.11.0)
英文:
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)
答案1
得分: 1
你必须使用真实的数值更新你的连接字符串。
英文:
You must update your connection string with the real values.
答案2
得分: 0
谢谢您的答复,davidbuzatto,但问题比我预期的要容易解决。
以下是我解决此问题的指南:
步骤1: 使用以下命令检查您的MySQL配置文件(在Ubuntu中使用命令:nano /etc/mysql/my.cnf)
将 bind-adress: 172.0.0.1 替换为 bind-adress: 0.0.0.0
如果这不起作用... 对我来说,这没有帮助...
步骤2: 检查服务器的防火墙设置,并允许访问MySQL服务器。
如果这也没有帮助... 对我来说,这没有帮助...
步骤3: 授予MySQL数据库访问权限。
使用以下MySQL查询:GRANT ALL ON yourDatabasename.* to yourUser@yourIP identified by 'passwordOfTheUser';
英文:
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';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论