【发布时间】:2015-02-25 04:57:06
【问题描述】:
我有一个在 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.
谢谢
【问题讨论】: