无法从我的 Java 应用程序访问 mysql 数据库(在 AWS 实例上运行) [英] Can't access mysql database (running on AWS instance) from my java application

查看:40
本文介绍了无法从我的 Java 应用程序访问 mysql 数据库(在 AWS 实例上运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 AWS-EC2 Ubuntu 实例上运行的 xampp 服务器,我在那里有一个名为androiddb"的数据库.我可以通过在导航器中写入 http://public_ip/phpmyadmin 并输入root"用户而不输入密码来访问数据库.

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.

另一方面,我有一个 Java 应用程序需要与该数据库连接,我的代码中有以下行来执行此操作:

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.

我还查找了运行 mysql 的端口并使用了代码:

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

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

我在phpmyadmin的SQL部分写了这段代码,得到如下输出:

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

不知道端口0是什么意思,应该是3306(mysql默认端口)...

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

无论如何,我不知道是我的 xampp 配置有问题,还是 AWS 或其他任何问题.

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.

推荐答案

您应该确保您的 AWS 安全组在 3306 端口(或您配置的任何端口)上打开,以便他们可以访问数据库.

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

您可以通过尝试通过 SSH 连接到机器来解决问题,您可以在本地连接吗?如果是,请查看您的安全组并打开端口.如果您无法在本地连接,则说明数据库已关闭,您需要正确诊断和配置数据库.

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. 他检查了他的安全组并打开了正确的端口
  2. 在本地检查以查看 mySQL 未在任何端口上侦听
  3. netstat -tlnp 查看本地运行的 SQL
  4. 检查 my.ini 文件以查看 mysql 已配置为侦听端口
  5. 发现它不是并重新配置它以收听
  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

这篇关于无法从我的 Java 应用程序访问 mysql 数据库(在 AWS 实例上运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆