[英]Cannot connect to MySQL database running on AWS
public static void main (String[] args) {
String host = "jdbc:mysql://ec2-user@ec2-xx-xxx-xx-xxx.compute-1.amazonaws.com:3306/";
String uname = "root";
String db = "customers";
String password = "somepassword";
String driver = "com.mysql.jdbc.Driver";
int i = 0;
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(host+db, uname, password);
Statement st = conn.createStatement();
i = st.executeUpdate("INSERT INTO users " +
"VALUES ('Simpson1', 'mail@example.com', 'Springfield')");
if(i==1)
System.out.println("Sucessfully Updated table");
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
I am trying to connect to an AWS DB, but I am getting the following exception and error while I run the program above: 我正在尝试连接到AWS DB,但是当我运行上面的程序时,我收到以下异常和错误:
Got an exception! 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.
Is it error in the hostname I am trying to connect to? 我尝试连接的主机名是否有错误?
Based on the connection string and URL of the database, I see that you are connecting to the database installed in EC2. 根据数据库的连接字符串和URL,我看到您正在连接到EC2中安装的数据库。 I assume that you are trying to connect to instance from / via external internet.
我假设你正试图从/通过外部互联网连接到实例。
Please check the following settings 请检查以下设置
The best litmus test to confirm whether it is problem of connection / connectivity setting is to try running the above code inside the same ec2 instance in which the database instance is connected. 确认是否存在连接/连接设置问题的最佳试金石是尝试在连接数据库实例的同一ec2实例中运行上述代码。
Seems that you have to open port 3306 to the world (which is not a good idea by the way and you may want to revise this in production). 似乎你必须向世界开放3306端口(顺便说一下这不是一个好主意,你可能想在生产中修改它)。 To do that you need to
要做到这一点,你需要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.