App Engine Java Servlet无法连接到Cloud SQL

Gab*_*lev 10 java sql google-app-engine servlets google-cloud-sql

我使用app引擎创建了一个java web servlet,servlet向数据库发出请求.我已经使用本地数据库在本地测试了servlet并且它工作得很好,然后我继续在本地测试servlet但是仍然访问了Cloud SQL数据库,这也很有效.

在部署servlet之后我的问题出现了.部署完所有数据库请求后返回以下内容:

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.
Run Code Online (Sandbox Code Playgroud)

我在云控制台中查看,我的应用程序已正确添加到访问控制选项卡下的云SQL授权应用程序引擎应用程序中.

是否有人在部署的应用引擎servlet上遇到过类似的问题?那里有任何解决方案或建议吗?我将不胜感激任何帮助!

更新:

使用以下代码生成上述错误以访问db

Class.forName("com.mysql.jdbc.Driver");
Url = "jdbc:mysql://<ip-address-cloudsql>:3306/<dbname>";
Connection con = DriverManager.getConnection (Url,"root",<password>);
Run Code Online (Sandbox Code Playgroud)

使用此代码实现了同样的错误,请注意它与此处示例中显示的代码非常相似https://developers.google.com/appengine/docs/java/cloud-sql/

Class.forName("com.mysql.jdbc.GoogleDriver");
Url = "jdbc:google:mysql://<appID:instanceID>/<dbname>? 
user=root&password=<password>";
Connection con = DriverManager.getConnection (Url);
Run Code Online (Sandbox Code Playgroud)

当我使用appid和实例id来设置url时,我按照此stackoverflow文章中的格式化提示显示: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

使用此代码导致以下不同的错误:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
Run Code Online (Sandbox Code Playgroud)

我假设它说localhost因为我的cloudsql数据库设置为遵循app引擎servlet.另外,要知道这两种方法在本地运行servlet和访问cloud sql数据库时都能正常工作.

有什么想法吗?我不知道还有什么可以尝试:[

mom*_*omo 19

从授权的App Engine应用程序连接到Cloud SQL时,不需要密码(实际上,如果您尝试使用密码连接,则会失败).

更改连接字符串以jdbc:google:mysql://<appID:instanceID>/<dbname>? user=root省略该&password=<password>部分

  • 哇,这节省了我几个小时.尝试使用密码时出现错误消息并不完全有用. (4认同)
  • 是的,它浪费了很多时间来弄清楚它为什么不起作用.确实不是一个有用的信息 (3认同)
  • 只花了 2 个小时:(他们本可以在他们的文档中更加强调这一点...... (2认同)