gpt4 book ai didi

Java: 无法使用 JDBC 访问远程 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 00:06:21 25 4
gpt4 key购买 nike

我正在尝试使用我的 Java 代码访问安装在我的 Web 服务器中的 MySQL 数据库。我的 Java 应用程序是一个 Swing 应用程序,在我的 PC 上运行。下面是我的 Java 代码。

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;


public class TestDatabase {

private Connection con;

private void connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver");


con = DriverManager.getConnection("jdbc:mysql://72.0.0.0:3306/aceko_test","aceko_me","rtf7890");

System.out.println("Connection Established");
}
catch(Exception e)
{
e.printStackTrace();
}
}

public static void main(String[]args)
{
new TestDatabase().connect();
}

}

我收到以下错误。

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:358)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2535)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2320)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at TestDatabase.connect(TestDatabase.java:31)
at TestDatabase.main(TestDatabase.java:43)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:244)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:308)
... 16 more

我已在我的网络服务器的 CPanel 中尽我所能来分配远程访问。如下图所示,我提供了“任何访问权限”

enter image description here

如下图所示,我已经创建了数据库、用户,并将用户分配给了数据库。

enter image description here

请注意我在代码中提到的 IP 是假的,仅供 SO 使用。

最佳答案

在谷歌搜索我找到了this article显示如何连接 DBMS,但使用的是西类牙语。不管怎样,我试着翻译这篇文章:

1.-修改/etc/mysql/my.cnf文件您必须评论 skip-external-lockingbind-address 条目

    [mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
#skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1

2.- 使用以下命令向用户授予权限

haciendoti@haciendoti:~$ sudo mysql -u root -ppasswd

mysql > GRANT ALL ON *.* TO haciendoti@'%' IDENTIFIED BY 'passwd';

3.- 添加防火墙异常(exception)(我不知道你是否可以在你的服务器上这样做)

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

最好的问候。

关于Java: 无法使用 JDBC 访问远程 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27297808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com