没有应用引擎,如何连接Google Cloud实例

时间:2018-09-20 11:10:52

标签: java google-cloud-platform

我正在尝试连接到Google云端实例,但是给它带来了一些麻烦。非常感谢您能找到最简单的方法。

我也是使用数据库和Java的新手。

这是我的代码:

package com.google.cloud.sql.mysql;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* A sample app that connects to a Cloud SQL instance and lists all available 
tables in a database.
 */
public class example{
  public static void main(String[] args) throws IOException, SQLException {
// TODO: fill this in
// The instance connection name can be obtained from the instance overview page in Cloud Console
// or by running "gcloud sql instances describe <instance> | grep connectionName".
String instanceConnectionName = "eco-codex-216813:asia-southeast1:instance1";

// TODO: fill this in
// The database from which to list tables.
String databaseName = "mysql";

String username = "root";

// TODO: fill this in
// This is the password that was set via the Cloud Console or empty if never set
// (not recommended).
String password = "1412";


//[START doc-example]
String jdbcUrl = String.format(
    "jdbc:mysql://google/%s?cloudSqlInstance=%s"
        + "&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
    databaseName,
    instanceConnectionName);

Connection connection = DriverManager.getConnection(jdbcUrl, username, password);


try (Statement statement = connection.createStatement()) {
  ResultSet resultSet = statement.executeQuery("SHOW TABLES");
  while (resultSet.next()) {
    System.out.println(resultSet.getString(1));
    }
   }
  }
}

但是我遇到了这个错误:

<i>
run:
Exception in thread "main" java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on google:3,306.

Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.


    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:470)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.google.cloud.sql.mysql.example.main(example.java:45)
Caused by: java.lang.NullPointerException
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:976)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:822)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
    ... 5 more
C:\Users\Zack\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)</i>
使用NetBeans IDE的

im 并使用mysql连接器Java 8。

1 个答案:

答案 0 :(得分:0)

欢迎使用Stackoverflow和Google Cloud!我假设您的意思是带有MySQL引擎的Google Cloud SQL实例和“ Google cloud实例”。您需要打开从运行Java程序的计算机到Cloud SQL实例的网络级别的访问权限。在Cloud SQL中,默认行为是从外部阻止此访问。您可以在云控制台中编辑实例,然后在“授权网络”部分中添加您的IP地址或IP范围以允许连接。

如果您的Java机器和Cloud SQL之间没有其他防火墙,那么在进行此更改之后,您应该可以连接。希望对您有帮助!

您可以从以下页面找到有关Cloud SQL连接的更多信息:https://cloud.google.com/sql/docs/mysql/connect-external-app#appaccessIP。您还可以找到其他用于连接到数据库的选项,例如与代理。