数据库
首页 > 数据库> > java – com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

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

作者:互联网

我有一个谷歌应用程序引擎连接项目,我试图连接到谷歌云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);

它是在我尝试与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>  

在我的标签
AppEngine上-web.xml中

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

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

解决方法:

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

标签:google-cloud-sql,java,mysql,google-app-engine,jdbc
来源: https://codeday.me/bug/20191003/1847998.html