簡體   English   中英

沒有應用引擎,如何連接Google Cloud實例

[英]How could i connect with Google cloud instance without app engine

我正在嘗試連接到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並使用mysql連接器Java 8。

歡迎使用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 您還可以找到用於連接數據庫的其他選項,例如使用代理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM