简体   繁体   English

无法从我的 Java 应用程序访问 mysql 数据库(在 AWS 实例上运行)

[英]Can't access mysql database (running on AWS instance) from my java application

I have a xampp server running on a AWS-EC2 Ubuntu instance, and I have a database there that is called "androiddb".我有一个在 AWS-EC2 Ubuntu 实例上运行的 xampp 服务器,我在那里有一个名为“androiddb”的数据库。 I can access the database by writing in a navigator http://public_ip/phpmyadmin and entering with "root" user and no password.我可以通过在导航器http://public_ip/phpmyadmin 中写入并使用“root”用户输入而没有密码来访问数据库。

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:在另一方面,我有一个需要与该数据库连接的 Java 应用程序,我的代码中有以下行来执行此操作:

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:我还查找了运行 mysql 的端口,并使用了代码:

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:我在phpmyadmin的SQL部分写了这段代码,得到如下输出:

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)...不知道端口0是什么意思,应该是3306(mysql默认端口)...

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.无论如何,我不知道我是否在 xampp 配置上做错了什么,或者是否是 AWS 或其他任何问题。

Could someone help me with this?有人可以帮我解决这个问题吗? Thank you very much.非常感谢。

You should ensure your security groups for AWS are open on the 3306 port (or whatever you configure) so they can access the database.您应该确保 AWS 的安全组在 3306 端口(或您配置的任何端口)上打开,以便他们可以访问数据库。

You could trouble shoot by trying to SSH into the machine, can you connect locally?您可以通过尝试通过 SSH 连接到机器来解决问题,您可以在本地连接吗? 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在本地检查以查看 mySQL 没有在任何端口上侦听
  3. netstat -tlnp to check to see SQL running locally netstat -tlnp检查本地运行的 SQL
  4. Checked the my.ini file to see that mysql was configured to listen on a port检查 my.ini 文件以查看 mysql 已配置为侦听端口
  5. Found it wasn't and reconfigured it to listen发现它不是并重新配置它以收听

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.它可能有助于一些使用 phpMyAdmin 和 AWS 以及亚马逊的关系数据库服务 (RDS) 的人知道除非将 RDS 端点编辑到 config.inc.php 文件中的“localhost”或“127.0.0.1”位置,否则 phpMyAdmin 将无法工作. (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.概括一下,如果您使用的是foo并且它有一个配置文件,并且您希望连接到在 RDS 下实例化的数据库,您将需要该端点作为主机条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM