Accessing Google Cloud SQL instance from Google Compute Engine?

試著忘記壹切 提交于 2020-01-17 04:04:07

问题


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

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

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.

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'    => '',
)

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


回答1:


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

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

To begin

  1. Go into your dashboard and request an IP for your CloudSQL Instance.
  2. Go to CloudSQL Access Control and add your GCE IP address.
  3. Connect to CloudSQL from GCE via mysql-client and add a new (non-root) user
  4. Use the CloudSQL IP and the new non-root user to access CloudSQL from GCE PHP files.

Hope this helps.




回答2:


The Cloud SQL team are working on improving the connectivity from Compute Engine. If you send this question to google-cloud-sql-discuss@googlegroups.com, they will be able to follow up.




回答3:


You could connect indirectly I.E. Create a Java-based App Engine App that provides an interface to the database for you, and consume that interface from your PHP app?

For example: Java App Engine App has a 'getEmployees' method call that calls a Select query on the DB and then formats and returns the results as a JSON file. Your PHP app would then call this method and consume the JSON...



来源:https://stackoverflow.com/questions/19352528/accessing-google-cloud-sql-instance-from-google-compute-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!