App Engine Java Servlet 无法连接到 Cloud SQL [英] App Engine Java Servlet does not connect to Cloud SQL

查看:26
本文介绍了App Engine Java Servlet 无法连接到 Cloud SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用应用程序引擎创建了一个 Java Web servlet,该 servlet 向数据库发出请求.我已经使用本地数据库在本地测试了 servlet,它运行良好,然后我继续在本地测试 servlet,但我访问了 Cloud SQL 数据库,这也运行良好.

I have created a java web servlet using app engine, the servlet makes requests to a database. I have tested the servlet locally using a local database and it worked perfectly, i then proceeded to test the servlet locally but istead accessed the Cloud SQL database, this also worked perfectly.

我的问题是在我部署 servlet 后出现的.部署后,所有数据库请求都会返回以下内容:

My problem arises after i deploy the servlet. Once deployed all database requests return the following:

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.

我在云控制台中进行了检查,我的应用程序已正确添加到访问控制"选项卡下的云 SQL 授权 App Engine 应用程序中.

I checked within the cloud console, and my app was properly added to the cloud SQL Authorized App Engine Applications under the Access Control tab.

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

Has anyone had similar problems with deployed app engine servlets? Any solutions or advice out there? I would appreciate any and all help!!!

更新:

上述错误是使用以下代码访问数据库产生的

The above error was generated using the following code to access the db

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

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

the same error was acheived using this code, note that it is very similar to the code shown in the example here 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);

在使用 appid 和实例 ID 设置 url 时,我遵循了此 stackoverflow 帖子中显示的格式提示:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

I followed the formatting tips show in this stackoverflow post when it came to setting the url using appid and instance id: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

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

Using this code resulted in the following different error:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

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

I'm assuming it says localhost because my cloudsql database is set to follow the app engine servlet. Also, know that both of these methods worked fine when running the servlet locally and accessing the cloud sql database.

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

any thoughts? i don't know what else to try:[

推荐答案

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

When connecting to Cloud SQL from an authorized App Engine application, the password is not required (actually it will fail if you try to connect with password).

>

将您的连接字符串更改为 jdbc:google:mysql:///?user=root 省略 &password= 部分

这篇关于App Engine Java Servlet 无法连接到 Cloud SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆