0

I have a xampp server running on a AWS-EC2 Ubuntu instance, and I have a database there that is called "androiddb". I can access the database by writing in a navigator http://public_ip/phpmyadmin and entering with "root" user and no password.

In another side I have a Java application that needs to connect with that database, and I have the following line in my code to do it:

con = DriverManager.getConnection("jdbc:mysql://public_ip/androiddb",
                                  "root", "");

When I run my application, I get the following error:

Error: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2569)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at Presentacion.Presentacion$2.mouseClicked(Presentacion.java:125)
at java.awt.Component.processMouseEvent(Component.java:6519)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)


** END NESTED EXCEPTION **



Last packet sent to the server was 19 ms ago.

Also I looked for the port where mysql was running and I used the code:

SHOW VARIABLES
WHERE Variable_name IN (
'hostname',
'port')

I write this piece of code in phpmyadmin in the SQL section, and I get the following output:

Variable_name   Value
hostname        ip-172-31-23-224
port            0

I don't know what does port 0 mean, it should be 3306 (default mysql port)...

Anyways, I don't know if I'm doing something wrong with the xampp configuration or if it's problem of AWS or anything else.

Could someone help me with this? Thank you very much.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Cayetano
  • 3
  • 5

2 Answers2

1

You should ensure your security groups for AWS are open on the 3306 port (or whatever you configure) so they can access the database.

You could trouble shoot by trying to SSH into the machine, can you connect locally? If yes, then look at your security groups and open the port. If you can't connect locally than the database is down and you need to diagnose and configure the DB correctly.

But a connection refused is more than likely a port not being open in the security group.

Just to add what solved his issue:

  1. He checked his security groups and opened the correct ports
  2. Checked locally to see that mySQL wasn't listening on any port
  3. netstat -tlnp to check to see SQL running locally
  4. Checked the my.ini file to see that mysql was configured to listen on a port
  5. Found it wasn't and reconfigured it to listen
Stephen Carman
  • 999
  • 7
  • 25
  • I can access the machine with SSH. In my security groups I have opened HTTP, SSH, MySQL/Aurora and ICMP ports from any IP address. Do you know how can I check what port is mysql running on? I tried that piece of code in my question but I got port 0.Thanks for your answer! – Cayetano Apr 15 '16 at 15:35
  • You can do `netstat -tlnp` to see what port it's listening on. Or you could install nmap and use that `nmap localhost` – Stephen Carman Apr 15 '16 at 15:36
  • I can only see 21, 22, 80 and 443 ports, so it's seems like mysql isn't listening. I've restarted mysql but still doesn't appear its port. – Cayetano Apr 15 '16 at 15:42
  • check your xampp mysql my.ini. For security reason, new mysql version will bind to 127.0.0.1 instead of listen to all that bind 0.0.0.0 – mootmoot Apr 15 '16 at 15:53
  • Where can I find that file? I'm using ubuntu – Cayetano Apr 15 '16 at 16:00
  • Thank you very much! I found it at /opt/lampp/etc/my.cnf . Now the ports appears as listening and I don't get port 0 in phpmyadmin, so I can access the database from my application – Cayetano Apr 15 '16 at 16:10
0

It may help some working with phpMyAdmin and AWS and Amazon's relational database service (RDS) to know that phpMyAdmin will not work unless the RDS endpoint is edited into the "localhost" or "127.0.0.1" location in the config.inc.php file. (It is a necessary but not sufficient fix -- you may need to do other things...)

Generalizing a bit, if you are using foo and it has a config file, and you are looking to connect to a database instantiated under RDS, you'll need that endpoint as host entry.

Joundill
  • 6,828
  • 12
  • 36
  • 50
Ross
  • 1
  • 1