是否要从Google Compute Engine访问Google Cloud SQL实例? [英] Accessing Google Cloud SQL instance from Google Compute Engine?

查看:137
本文介绍了是否要从Google Compute Engine访问Google Cloud SQL实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了几个小时之后,这是我可以找到的唯一从GAE外部访问Cloud SQL的真实文档: https://developers.google.com/cloud-sql/docs/external

After spending a few hours, this is the only real documentation I can find for accessing Cloud SQL from outside of GAE: https://developers.google.com/cloud-sql/docs/external

问题是,这是针对Java应用程序(通过JDBC)的.

The problem is, this is for a Java application (via JDBC).

我需要从PHP,Dart或NodeJS应用程序中访问Cloud SQL DB.我认为,通过授予我的GCE实例连接到Cloud SQL的权限,这将很容易.但是,套接字字符串的任何排列(使用mysql驱动程序)似乎都不有效.

I need to access my Cloud SQL DB from within a PHP, Dart, or NodeJS application. I thought by giving my GCE instance rights to connect to Cloud SQL, this would be easy. But no arrangement of socket strings (using mysql drivers) seems to be effective.

为了论证,假设我正在尝试连接PHP应用程序.我的mysql连接数组如下所示:

For argument's sake, let's say I'm trying to connect with a PHP app. My mysql connection array looks like this:

(
  'driver'    => 'mysql',
  'unix_socket'     => '/cloudsql/project-id:instance-id',
  'host'      => 'localhost',
  'database'  => 'dbname',
  'username'  => 'root',
  'password'  => '',
  'charset'   => 'utf8',
  'collation' => 'utf8_unicode_ci',
  'prefix'    => '',
)

这和我的经验差不多,但是会出现一个通用的无法通过套接字连接到本地MySQL服务器"错误.

This is as close as I got, but I'll get a generic "Can't connect to local MySQL server through socket" error.

推荐答案

虽然这是一个比较老的问题,但我只是想分享我在此方面的发现.

While this is an older question, I just thought I should share what I've found in regards to this.

首先,您正在尝试连接到GCE实例上的MySQL Server,而不是远程CloudSQL实例.

First off, you were attempting to connect to a MySQL Server on your GCE instance, not your remote CloudSQL instance.

开始

  1. 进入您的仪表板并为您的CloudSQL实例请求IP.
  2. 转到CloudSQL访问控制并添加您的GCE IP地址.
  3. 通过mysql-client从GCE连接到CloudSQL并添加一个新的(非root用户)
  4. 使用CloudSQL IP和新的非root用户从GCE PHP文件访问CloudSQL.

希望这会有所帮助.

这篇关于是否要从Google Compute Engine访问Google Cloud SQL实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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