1

I have a mysql database created on a raspberry pi, I want to access it through a java application running on a laptop in the same local network.

I tried connecting that same application with another database on another computer on the network and it worked. I don't know why it isn't working with the RPI.

i connect through the following command:

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

However, an exception is thrown and ERROR: Unable to Connect to Database. is printed to the console.

Edit:

Knowing that: * I can access the database through a local app running on the same raspberry pi (i used @local). * Can Successfully ping & SSH with the raspberry pi, even run the program mentioned in the previous point through the SSH and it works fine.

The stack trace:

 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)
  • For a start you should print out the exception that you are getting, it may just let you know what the problem is. – Scary Wombat Dec 26 '13 at 08:28
  • Replace catch block with } catch (SQLException e) { e.printStackTrace(); } Then post the exception – Sachin Dec 26 '13 at 08:30

6 Answers6

4

I think the reason you can not access the remote mysql may be due to the remote access restriction in my.cnf
please check the line
bind-address: is set to 127.0.0.1
under this restriction, you can not connect remotely with that mysql server;
to solve this, you need to change this line to:
bind-address: 0.0.0.0

in addition you should specify the port for connection as below:
String URL = "jdbc:mysql://192.168.11.157:3306/DB_name";
because mysql will connect port 3306 in default.
wish my answer could help you!

Rugal
  • 2,612
  • 1
  • 15
  • 16
  • sorry , but i don't really know where i can find mysql.cnf or how to open it, i am new to mysql , databases & servers in general. – Shady Ahmed Abdelaal Dec 26 '13 at 10:42
  • if you are using linux, you can find it in /etc/mysql/my.cnf. sorry for the typo in my answer! – Rugal Dec 26 '13 at 11:29
  • I found the following tutorial: 1. Grant access to your remote machine. 2. Go into the my.cnf file (sudo nano /etc/mysql/my.cnf) file and look for "bind-address" and comment this out (put a # in front of the line) 3. Reload MySQL config (service mysql reload) 4. Restart MySQL server (service mysql restart) Since i have already done number 1 , i did number 2 (also tried to make it 0.0.0.0 as you said). However, when i use service mysql reload , i get the following message: Fatal error in defaults handling. Program aborted – Shady Ahmed Abdelaal Dec 26 '13 at 12:10
  • You directed me in the right way , it was the bind-address as you thought, however, must use reload after modifying. Thank you – Shady Ahmed Abdelaal Dec 26 '13 at 12:58
  • actually I just like to restart after modified my.cnf – Rugal Dec 26 '13 at 14:17
0

You have to check the next things:

  1. Is the database active? Does it respond to another application except yours?
  2. Is the network connection OK (eg firewall)?
  3. Check the connection strings (perhaps the port is necessairy but I am not sure).

I am not really familiar with the raspberry. It seems that it's using a linux operating system (correct me if I am wrong). In this post, instruction are given on how to disable firewall on the device. It says that the firewall is the usual iptables firewall used on linux systems. I find this iptables tutorial (based on ubuntu) very useful. Perhaps it would give you some guidance.

Hope I helped!

Community
  • 1
  • 1
Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36
  • 1- Until now, the database only responds to another java application that runs on the same raspberry pi (i am using @localhost in that application), but i am trying to build the first app to access it from another computer and that's what isn't working. 2- No idea about any firewall settings on the raspberry pi. 3- I think i got it right, because i was able to access the same database, when it was on my computer. I just used the ip as mentioned above. – Shady Ahmed Abdelaal Dec 26 '13 at 10:45
  • Check my edited answer. I put the details on the main answer body to be more easily accessible for other users. – Pantelis Natsiavas Dec 26 '13 at 10:55
  • Thanks for your concern, but i want to ask a question before going into these details. If there is a problem with the firewall , will i be able to SSH into the Raspberry pi & Also could ping from the terminal ? Because both work fine with me right now. – Shady Ahmed Abdelaal Dec 26 '13 at 11:04
  • I cannot really give you an answer on accessing the Raspberry pi through SSH. I cannot see a reason why you wouldn't be able to access it through SSH, but as I said I am not an expert on this device. I suppose [this](http://cplus.about.com/od/raspberrypi/a/How-Do-I-Setup-Ssh-On-Raspberry-Pi.htm) could provide some guidance... – Pantelis Natsiavas Dec 26 '13 at 11:17
  • You misunderstood me, What i am saying is that I CAN SUCCESSFULLY Access the raspberry pi through SSH, so, can the problem i am facing still be a firewall problem? IF it was a firewall problem, isn't it supposed that SSH will not work too ? – Shady Ahmed Abdelaal Dec 26 '13 at 11:21
  • Sorry. I indeed misunderstood. Yes, it could still be a firewall problem, despite the working SSH. [SSH uses well known port 22](http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers). This means that in some systems, these ports are by default open. On the other hand, the firewall could have the port used by JDBC (3306) closed. – Pantelis Natsiavas Dec 26 '13 at 12:09
0

You may be able to ping the raspberry pi but I am not sure what the interface used is.

Why don't you use MySQL Ping and see if it is responding?
Else it must be a network issue. Get it resolved and then connect. No alternatives for this.

You can issue a light weight ping to your database server to check the connection availability.
Please read following discussion and articles:

  1. Ping MySQL Server
  2. Ping syntax and example
  3. My answer to one of such Communication link failures.
Community
  • 1
  • 1
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
0

Solved:

  1. Go into the my.cnf file (sudo nano /etc/mysql/my.cnf) file and look for "bind-address" and make it “bind-address = * “
  2. Reload MySQL config (sudo service mysql reload)
  3. Restart MySQL server (sudo service mysql restart)
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
-1

The url "jdbc:mysql://localhost:3306/mydb".

You lost the port 3306.

huanhuanw
  • 43
  • 4
-3

you need to print the exception to the console ,rather than print a customer "error",If you do as that ,you can't get the key of the problem