我有一个谷歌应用引擎连接的项目,我试图连接到谷歌云 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=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 实例中提供任何明确的权限才能连接?
欢迎提出任何建议。
奇怪的是,我可以通过转到项目->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 客户端连接 我可以访问数据库,但是在代码中提供相同的密码时它拒绝连接。
任何建议将不胜感激。 谢谢, 劳拉
最佳答案
Instance Id
默认包含项目 Id。所以不要再显式添加project Id。
项目编号:sampleapp
Instance 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";
关于java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065653/