0
votes

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.

3

3 Answers

1
votes

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.

0
votes

The Cloud SQL team are working on improving the connectivity from Compute Engine. If you send this question to [email protected], they will be able to follow up.

0
votes

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