com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

时间:2013-11-19 07:30:41

标签: java mysql google-app-engine jdbc google-cloud-sql

我有一个谷歌应用程序引擎连接项目,我试图连接到谷歌云sql数据库。我上传我的项目谷歌和通过它我尝试连接到数据库。

我的连接网址如下 - >

Class.forName("com.mysql.jdbc.GoogleDriver");
String password="";
    url = "jdbc:google:mysql://my-project-id:my-instance-name/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";

    Connection conn = DriverManager.getConnection(url);

我正试图与我得到异常的网址建立连接

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.

然后我将以下字符串添加到我的url-> &安培; autoReconnect的=真安培; failOverReadOnly =假安培; maxReconnects为= 10

然后收到了另一个例外 - >

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 10 times. Giving up.

我试过ping数据库实例的ip地址,但它运行正常。

我是否需要在我的云sql实例中提供任何明确的权限才能连接?

欢迎任何建议。

奇怪的是,我可以通过转到project-> google-> app engine-> usegoogle cloud sql instance-> configure来使用eclipse连接数据库。我使用相同的用户名( root)和密码(“”)在这里和代码中的url一样,但由于某种原因它拒绝通过代码连接。 我也启用了

<use-google-connector-j>true</use-google-connector-j>  
我的标签中的

标签 应用服务引擎-web.xml中

我将root密码从“”更改为某个值,我可以通过mysql客户端连接 我可以访问数据库,但在代码中提供相同的密码时,它拒绝连接。

任何建议将不胜感激。 谢谢, 劳拉

1 个答案:

答案 0 :(得分:0)

Instance Id默认包含项目ID。所以不要再次明确添加项目ID。

项目ID:sampleapp

实例ID:sampleapp:instance1(这是google cloud sql默认创建的方式)

连接字符串:

<强>正确:

jdbc:google:mysql://sampleapp:instance1/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";

正确

jdbc:google:mysql://sampleapp:sampleapp:instance1/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";