远程数据库

时间:2015-07-21 20:11:08

标签: java database jdbc

我尝试在java中创建一个适用于远程数据库的应用程序。这些应用程序在本地数据库上运行良好。我也包括了端口号。我很害怕它可能是我正在使用的IP地址。

我心里想,世界各地可能有成百上千的人拥有与我相同的知识产权......互联网如何看待我是哪一个,或者保持对它的保护。我相信你不能远程访问家庭网络的IP电脑,但我可能错了......老实说,我不知道。

这是我的功能。

public void db() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection myCon = DriverManager.getConnection("jdbc:mysql://IPOfWebSite:3306/DatabaseName", "UserName", "Password"); 
        System.out.println("Connected");
        myCon.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

在远程数据库上我添加了我的公共IP地址,但仍然收到此错误

"C:\Program Files\Java\jdk1.8.0_25\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 14.1.3\bin" -Dfile.encoding=windows-1252 -classpath "C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;H:\Java Dev\Simple_java_DB\out\production\Simple_java_DB;H:\Java Dev\Simple_java_DB\mysql-connector-java-5.1.35\mysql-connector-java-5.1.35\mysql-connector-java-5.1.35-bin.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 14.1.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
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:389)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    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:389)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at Main.db(Main.java:24)
    at Main.main(Main.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.net.ConnectException: Connection refused: 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 com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
    ... 21 more

任何帮助都会很棒。顺便说一句,我检查了我的防火墙......它应该允许端口接收和发送。

2 个答案:

答案 0 :(得分:1)

MySQL默认只侦听本地接口(loopback)。因此,您只能从本地计算机上访问它。

要改变这一点,请调整MySQL conf:

[mysqld] 
bind-address=YOUR-SERVER-IP

此外,请确保skip-networking配置已注释掉或不存在。

更多详情here

答案 1 :(得分:0)

1)确保您要添加外部 IP,而不是内部网络IP(192.168.x.x)。

转到:https://www.whatismyip.com/并使用该IP将您的IP列入远程数据库防火墙和/或访问限制的白名单。

2)确保您的mysql用户设置了正确的主机,“user”@“your-external-ip”或“user”@“%”(允许该用户)从任何IP连接。)

3)确保my.conf已设置为允许远程连接。确保您没有绑定到127.0.0.1,而是暂时执行:

bind-address = 0.0.0.0;

您应该将其设置为服务器的实际IP,但0.0.0.0适用于任何IP,应该暂时使用以验证这是您的问题。从长远来看,将其保留为0.0.0.0会带来安全风险。

还要确保您没有跳过网络。确保此行已注释掉(前面有#):

# skip-networking