使用简单的Java程序(JDBC)连接到Google SQL [英] Connecting to Google SQL with a simple Java program (JDBC)

查看:188
本文介绍了使用简单的Java程序(JDBC)连接到Google SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Java程序(不是Web应用程序/Google Engine/等). 我翻阅了Google Cloud SQL网站上的所有指南,但不知道如何连接到服务器.

I have a simple Java program (not a web application/Google Engine/etc.). I went over every guide in Google Cloud SQL website, and I can't figure out how to connect to the server.

现在我有

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection("jdbc:mysql://x:3306/y?user=root"); 

其中x是实例的IP地址(从实例的属性中获取),而y只是实例ID.

Where x is IP address of the instance (taken from the properties of the instance) and y is simply the instance ID.

我做错什么了吗? 我还根据建议的教程之一为根设置了密码.我应该在代码中的某个地方指定它吗?

Am I doing something wrong? I also set a password for the root as one of the tutorials suggested. Should I specify it somewhere in the code?

谢谢.

编辑

通过mohan rathour的链接,我获得了 Cloud SQL MySQL套接字工厂,现在我的代码如下:

Following mohan rathour's link, I obtained Cloud SQL MySQL Socket Factory and my code now looks like:

Class.forName("com.mysql.jdbc.Driver");

String jdbcUrl = String.format("jdbc:mysql://google/%s?cloudSqlInstance=%s&"+"socketFactory=com.google.cloud.sql.mysql.SocketFactory",
                    "dbname",
                    "instanceConnectionName");

connection = DriverManager.getConnection(jdbcUrl, "userName", "password");

我在控制台中得到以下内容:

I get the following in my console:

INFO: Connecting to Cloud SQL instance [instanceName].
com.google.cloud.sql.mysql.SslSocketFactory getInstance
INFO: First Cloud SQL connection, generating RSA key pair.

但随后出现此异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

解决方案

无需使用"Cloud SQL MySQL套接字工厂".

No need to use "Cloud SQL MySQL Socket Factory".

在Google SQL面板中,转到您的实例面板->访问控制->授权,添加以下网络地址,然后单击保存.

In the Google SQL panel, go to your instance panel -> Access Control -> Authorization, add the following network address and click save.

0.0.0.0/0

现在您可以使用以下代码进行连接:

Now you can connect with this code:

Class.forName("com.mysql.jdbc.Driver");
...
String url= String.format("jdbc:mysql://x:3306/y");
connection = DriverManager.getConnection(url, z, w);

其中x是您的实例IP(从面板中获取),y是实例名称,z是数据库(不是帐户)用户名(通常是"root"),w是用户密码(您必须设置它.请参阅Google的教程.

Where x is your instance IP (get it from the panel), y is the instance name, z is the database (not account) user name (usually "root") and w is the user password (you have to set it. look at Google's tutorials).

推荐答案

默认情况下,Cloud SQL实例不允许公共IP上的连接.您必须添加要连接的单个子网. 您可以在此处查看完整的文档: https://cloud.google.com/sql/docs/external#appaccessIP

By default, Cloud SQL instances do not allow connections on the public IP. You must add individual subnets from which you would like to connect. You can see the full documentation here: https://cloud.google.com/sql/docs/external#appaccessIP

一种不需要将IP地址列入白名单的替代方法是使用Cloud SQL套接字库,该库使用具有短期证书的SSL会话: https://cloud.google.com/sql/docs/external#java

An alternative that does not require whitelisting IP addresses is using the Cloud SQL Socket Library that uses an SSL session with short-lived certs: https://cloud.google.com/sql/docs/external#java

不建议将0.0.0.0/0列入白名单,因为它会将数据库打开到整个世界.

Whitelisting 0.0.0.0/0 is not recommended as it opens the database to the entire world.

这篇关于使用简单的Java程序(JDBC)连接到Google SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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