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

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

我的连接URL如下 – >

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); 

它正是在我尝试与url建立连接的时候我得到了一个exception

 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 = true&failOverReadOnly = false&maxReconnects = 10

然后收到了一个不同的例外 – >

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

我试过ping数据库实例的IP地址,它工作正常。

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

欢迎任何建议。

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

 true 

我的appengine-web.xml中的标记

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

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

默认情况下, 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";