通过Java连接到远程数据库

时间:2013-12-26 08:25:57

标签: java mysql database raspberry-pi

我在raspberry pi上创建了一个mysql数据库,我希望通过在同一本地网络中的笔记本电脑上运行的java应用程序来访问它。

我尝试将同一个应用程序与网络上另一台计算机上的另一个数据库连接起来并且运行正常。我不知道为什么它不能与RPI合作。

我通过以下命令连接:

String URL = "jdbc:mysql://192.168.11.157/DB_name";
try {
connection = DriverManager.getConnection(URL, USER, PASSWORD);
            } catch (SQLException e) {
                e.printStackTrace();
            }

但是,抛出异常并且ERROR:无法连接到数据库。打印到控制台。

编辑:

知道:  *我可以通过在相同的覆盆子pi上运行的本地应用程序访问数据库(我使用@local)。  *可以成功ping&使用raspberry pi的SSH,甚至通过SSH运行上一点中提到的程序,它运行正常。

堆栈跟踪:

 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(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:358)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2489)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.climacon.jdbc.db.JDBCMySQLConnection.createConnection(JDBCMySQLConnection.java:42)
at com.climacon.jdbc.db.JDBCMySQLConnection.getConnection(JDBCMySQLConnection.java:51)
at com.climacon.jdbc.main.ClimaConJDBC.get_Nodeinfo_From_DB(ClimaConJDBC.java:110)
at com.climacon.gui.ClimaCon2$5.actionPerformed(ClimaCon2.java:279)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

6 个答案:

答案 0 :(得分:4)

我认为您无法访问远程mysql的原因可能是由my.cnf中的远程访问限制造成的 请检查线路
bind-address:设置为127.0.0.1
在此限制下,您无法远程连接该mysql服务器;
要解决这个问题,您需要将此行更改为:
bind-address: 0.0.0.0

此外,您应指定连接端口,如下所示:
String URL = "jdbc:mysql://192.168.11.157:3306/DB_name";
因为mysql默认会连接端口3306 希望我的回答可以帮到你!

答案 1 :(得分:0)

您必须检查以下内容:

  1. 数据库是否有效?它是否会响应除您之外的其他应用程序?
  2. 网络连接是否正常(例如防火墙)?
  3. 检查connection strings(也许端口是必需的,但我不确定)。
  4. 我对树莓并不熟悉。它似乎正在使用Linux操作系统(如果我错了,请纠正我)。在this帖子中,给出了如何在设备上禁用防火墙的说明。它说防火墙是linux系统上常用的iptables防火墙。我发现this iptables tutorial (based on ubuntu)非常有用。也许它会给你一些指导。

    希望我帮忙!

答案 2 :(得分:0)

您可以ping raspberry pi,但我不确定界面使用的是什么。

为什么不使用MySQL Ping并查看它是否正在响应? 否则它必须是网络问题。解决它,然后连接。没有替代品。

您可以向数据库服务器发出轻量级ping以检查连接可用性 请阅读以下讨论和文章:

  1. Ping MySQL Server
  2. Ping syntax and example
  3. My answer to one of such Communication link failures

答案 3 :(得分:0)

解决:

  1. 进入my.cnf文件(sudo nano /etc/mysql/my.cnf)文件 查找"bind-address"并将其设为“bind-address = * “
  2. 重新加载MySQL配置(sudo service mysql reload)
  3. 重启MySQL服务器(sudo service mysql restart)

答案 4 :(得分:-1)

网址“jdbc:mysql:// localhost:3306 / mydb”。

你丢失了3306端口。

答案 5 :(得分:-3)

您需要将异常打印到控制台,而不是打印客户“错误”,如果您这样做  因此,你无法得到问题的关键