简体   繁体   English

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

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

I´m trying to connect to a google cloud instance, but havong some trouble with it. 我正在尝试连接到Google云实例,但是却遇到了一些麻烦。 I´d appreciate help to find the easiest way to do so. 非常感谢您能找到最简单的方法。

Also I´m New to working with Databases and Java. 我也是使用数据库和Java的新手。

here is my code: 这是我的代码:

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));
    }
   }
  }
}

but i got this error : 但我得到这个错误:

<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>

im using NetBeans IDE and using mysql connector java 8. 我使用NetBeans IDE并使用mysql连接器Java 8。

Welcome to Stackoverflow and Google Cloud! 欢迎使用Stackoverflow和Google Cloud! I assume you mean Google Cloud SQL Instance with MySQL engine with "Google cloud instance". 我假设您的意思是带有MySQL引擎的Google Cloud SQL实例和“ Google cloud实例”。 You need to open network level access from the machine running the Java program to the Cloud SQL Instance. 您需要打开从运行Java程序的计算机到Cloud SQL实例的网络级别的访问权限。 In Cloud SQL the default behavior is to block this access from outside. 在Cloud SQL中,默认行为是从外部阻止此访问。 You can edit the instance in the cloud console and in "Authorize network" part add your IP address or IP range to allow connection from. 您可以在云控制台中编辑实例,然后在“授权网络”部分中添加您的IP地址或IP范围以允许连接。

If there is no other firewalls between your Java machine and Cloud SQL, you should be able to connect after this change. 如果您的Java机器和Cloud SQL之间没有其他防火墙,那么在进行此更改之后,您应该能够连接。 Hope it helps! 希望能帮助到你!

You can find more information on Cloud SQL connectivity from this page: https://cloud.google.com/sql/docs/mysql/connect-external-app#appaccessIP . 您可以从以下页面找到有关Cloud SQL连接的更多信息: https : //cloud.google.com/sql/docs/mysql/connect-external-app#appaccessIP You can also find other options for connecting to db, eg with a proxy. 您还可以找到用于连接数据库的其他选项,例如使用代理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Eclipse中进行开发时无法从Google App Engine连接到Google Cloud SQL实例 - Unable to connect to Google Cloud SQL instance from Google App Engine when developing in Eclipse 使用Google App Engine端点连接到Cloud SQL - Connect to Cloud SQL with Google App Engine Endpoint 如何在谷歌应用引擎中制作常驻实例? - How do I make a resident instance in google app engine? 将Google App Engine Eclipse插件连接到Cloud SQL - Connect Google App Engine Eclipse Plugin to Cloud SQL Google Cloud Memorystore通过“ App Engine Java标准环境”进行连接 - Google Cloud Memorystore connect from “App Engine Java Standard Environment” 如何通过Google Cloud Endpoints将两个Android应用连接到同一App Engine数据存储区? - How to connect two Android apps through to the same App Engine Datastore through Google Cloud Endpoints? Google APP Engine 和云 sql :: 无法在 Google 云 sql(我的 sql)中连接 Spring boot 应用程序 - Google APP Engine and cloud sql :: Unable to connect Spring boot app in Google cloud sql (my sql) 如何在Google App Engine上连接到远程RDBMS - How to connect to a remote RDBMS on Google App Engine 我无法从Google App Engine上已部署的后端Servlet连接到我的Google Cloud sql数据库 - I can't connect to my google cloud sql database from my deployed backend servlet on the google app engine 如何在不下载Google Cloud Java SDK的情况下对服务帐户进行身份验证(不在App Engine上) - How to authenticate a service account without download for the Google Cloud Java SDK (not on App Engine)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM