gpt4 book ai didi

java - 如何在 AWS 实例中设置与 Mysql 的 jdbc 连接?

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:30 27 4
gpt4 key购买 nike

我有一个在 ec2 实例中运行的 mysql,当我尝试使用该 mysql 时,我可以执行以下操作:

dilin-mbp:~ dilin$ ssh -i /Users/dilin/Documents/SelfProject/MM_AWS_MainKey.pem ec2-user@54.69.23.214  
Last login: Tue Feb 24 05:07:47 2015 from c-73-189-116-135.hsd1.ca.comcast.net

__| __|_ )
_| ( / Amazon Linux AMI
___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2014.09-release-notes/
[ec2-user@ip-172-31-45-13 ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1969
Server version: 5.5.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from .....;

就是我需要先用一个pem文件访问服务器,然后才能使用mysql。

问题来了,在JDBC中,如何配置pem文件让自己连上?

我天真的实现:

    String url = "jdbc:mysql://54.69.23.214:3306/";
String dbName = "snapsono";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "pwd";
SnapList snapList = new SnapList();
try {
Driver mysqlDriver = (Driver) Class.forName(driver).newInstance();
DriverManager.registerDriver(mysqlDriver);
logger.info("about to get access");
Connection conn = DriverManager.getConnection(url + dbName,
userName, password);
logger.info("get Initialized!!!");
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM event");
while (res.next()) {
int id = res.getInt("userid");
String idUrl = res.getString("url");
SnapProfile profile = new SnapProfile(id,idUrl);
snapList.getList().add(profile);
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}

在目前的实现中,根据日志,我无法建立连接,服务器也没有响应。我想那是因为我没有设置 pem 文件,谁能帮我指出设置?

[INFO] Starting scanner at interval of 40 seconds.
Feb 24, 2015 8:51:16 PM snap.sono.demo.impl.GeneralImpl getDbHello
INFO: >>> enter getDbHello
Feb 24, 2015 8:51:16 PM snap.sono.demo.impl.GeneralImpl getDbHello
INFO: about to get access
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.

谢谢

最佳答案

这是因为您的 AWS 实例配置为仅从本地主机连接到端口 3306,更准确地说,它只允许从 EC2 实例 IP 连接。

所以,
登录到您的 AWS 控制台 并编辑分配的 Security Group Policy 并将其设置为允许从任何 IP 连接。
您可以引用此链接来编辑安全组策略,它适用于 SSH,但过程与 MySQL 相同,只是您需要为 MySQL 添加新规则。

.AWS REF.

其次,创建一个新的mysql用户如下
创建由“pass123”标识的用户“appuser”@“localhost”;
创建由“pass123”标识的用户“appuser”@“%”;
将 *.* 上的所有权限授予 'appuser'@'localhost'
将 *.* 上的所有权限授予 'appuser'@'%';
这样你就可以从任何 IP 连接到 mysql 或者如果你有一个静态的,那么出于安全考虑,我建议你使用它而不是 % : )

关于java - 如何在 AWS 实例中设置与 Mysql 的 jdbc 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28711429/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com