0

I tried creating an app in java that works with a remote database. The apps works fine on a local database. I have included the the port number as well. I am scared that it may be the IP address I am using.

I thought to myself there may be hundreds if not thousands of pc's around the world with the same IP as mine... and how does the Internet see which one I am, or stay secure about it. I am sure you don't give the remote access the IP of you computer of you home network, but I may be wrong... I honestly don't know.

This is my function.

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();
 }
}

On the Remote DB I have added my public IP address, but still received this error

"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

Any help will be great. By the way I did check my firewall... it should allow the port to receive and sent.

Tiny
28k113 gold badges352 silver badges611 bronze badges
asked Jul 21, 2015 at 20:11
5
  • are you trying to access it with an IP that looks like 192.168..? Commented Jul 21, 2015 at 20:12
  • Are you sure the ports that you're trying to connect on aren't blocked? Commented Jul 21, 2015 at 20:13
  • yes, my computers IP address is 192.168. Commented Jul 21, 2015 at 20:13
  • you realize that except for the 3 "private" blocks, IP addresses cannot be "shared"? And those private block IPs are NOT permitted to be routed over the public internet. Commented Jul 21, 2015 at 20:14
  • I have added an exception in the firewall, so they should be open Commented Jul 21, 2015 at 20:14

2 Answers 2

1

MySQL does by default only listen to the local interface (loopback). So you can only reach it from the local machine.

To change this, adapt the MySQL conf:

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

Also, make sure the skip-networking configuration is commented out or not present.

More details here.

answered Jul 21, 2015 at 20:24
Sign up to request clarification or add additional context in comments.

2 Comments

I am not in control of the server, so i can't access the conf files... Its a hosted web site, where I want to access the DB
Did you talk to the administrator of the website and he told you the DB is actually accessible. DBs of most websites are not accessible. You can try this with a MySQL administration tool like MySQL Workbench. If that doesn't work, the problem is very likely not in your code.
0

1) Make sure you're adding your external IP, not your internal network IP (192.168.x.x).

Go to: https://www.whatismyip.com/ and use that IP for whitelisting your IP in the remote DB's firewall and/or access restrictions.

2) Make sure your mysql user is set up with the correct host, either 'user'@'your-external-ip' or 'user'@'%' (to allow that user to connect from any IP).

3) Make sure your my.conf is set up to allow remote connections. Make sure you're not binding to 127.0.0.1, instead temporarily do:

bind-address = 0.0.0.0;

You should set this to your server's actual IP, but 0.0.0.0 will work for any IP and should just be used temporarily to verify that this is your problem. Long term it could pose a security risk to leave it as 0.0.0.0.

Also make sure that you're not skipping networking. Make sure this line is commented out (with a # in front of it):

# skip-networking
answered Jul 21, 2015 at 20:15

4 Comments

Updated my answer. Is your user added to mysql correctly?
yes that is correct. double checking my open ports now
my port 3306 is definitely open... have no idea what can be wrong
Sounds like your mysql config (my.conf) isn't set up for remote connections. See my updated answer

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.