Search code examples
phpgoogle-cloud-platformgoogle-cloud-sql

GCP CloudSQL Access from same GCP Project without Proxy


I'm totally stumped on what I think is probably something easy. Been searching for days on this with no luck as I learn how to use Cloud SQL. There are lots of examples on app engine, but almost none on the simplicity of connecting a GCP Virtual Machine to GCP Cloud SQL. The instructions from Google don't seem to be complete. Here is what I have:

In the same GCP Project I created a Bitnami LAMP VM. In the same project I've created a CloudSQL Instance with a single database, and two tables. My intent is to experiment with accessing CloudSQL via a PHP script from the VM. (Yes I know I could setup an SQL server on the VM, but my intent here is to learn how to access CloudSQL). Note, everything is in the same GCP project, so from what I can understand I should NOT have to use the SQL proxy.

I've enabled the CloudSQL API in the project as well.

<?php
$DB_NAME = "dwelling_winebot";
$DB_USER = "winebot_reader";
$DB_PASS = "winebot_reader";
$tableName = "wines";
$mysqli = new mysqli(null, $DB_USER, $DB_PASS, $DB_NAME,null, "/cloudsql/web-sites-123456:us-central1:winebot");

... More code below

The error I get in the apache error_log is:

[08-Oct-2018 05:00:24 America/Los_Angeles] PHP Warning:  mysqli::__constru
ct(): (HY000/2002): No such file or directory in /opt/bitnami/apache2/htdo
cs/winebot/serverside/inforunner.php on line 25

(Incidentally, line 25 is the $mysqli line and the php filename is inforunner.php that has the php code above )

Notable facts:

  • I CAN see the database instance if I run "gcloud sql instances
    describe winebot"
    • I have created a user named winebot_reader... Saying this I also tried 'root' with a "" password as some documents suggested. No luck.
    • The cloudsql instance name is infact the right project name, zone, and database from the console.
    • I do not seem to have a local /cloudsql folder with socket paths as some documents suggest I should have?
    • According to the console, the CloudSQL API is turned on
    • In the location field of the mysqli call (first parameter) I have tried, null, localhost, 127.0.0.1. All with the same result.

Any help would be greatly appreciated.


Solution

  • use the internal IP instead of a socket, which does not exist.

    see Connecting MySQL Client from Compute Engine for reference.