0

I've read the similar questions and I tried what they said, but I couldn't get my App Engine app to connect to my Cloud SQL instance.

Here's what I've found is supposed to be correct:

index.php

$servername = null;
$username = "root";
$password = "rootPassword";
$database = "NewApp";
$port = null;
$socket = "/cloudsql/newapp-edc:us-central1:newapp0";

$connection = new mysqli($servername,
                $username,
                $password,
                $database,
                $port,
                $socket);

app.yaml

runtime: php72

entrypoint: serve index.php

env_variables:
    # Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
    # values obtained when configuring your Cloud SQL instance.
    MYSQL_USER: root
    MYSQL_PASSWORD: rootPassword
    MYSQL_DSN: mysql:dbname=NewApp;unix_socket=/cloudsql/newapp-edc:us-central1:newapp0

# Use the connection name obtained when configuring your Cloud SQL instance.
beta_settings:
    cloud_sql_instances: "newapp-edc:us-central1:newapp0"

I've gone through all of the similar questions and answers and all of the relevant Google Docs and I can't find the answer to this.

The current method results in "Connection refused," which means, I think, that I have the socket correct as before it said, "No such file or directory." Everything seems to be correct and I have permission for all apps in the same project, and the App Engine app and the Cloud SQL instance are in the same project, so that shouldn't be the issue.

JVE999
  • 3,327
  • 10
  • 54
  • 89

2 Answers2

0

As mentioned here in this similar post, if you would like to connect your Google App Engine to Cloud SQL you will need to enable Cloud SQL proxy since Google App Engine connects from inside of Google servers to your Cloud SQL instance via proxy.

If you would not like to use Cloud SQL proxy, you will need to to authorize your IP from your Cloud SQL instance, as shown here.

I hope this helps.

  • This is a little confusing. In the Google Cloud Console, I typed in "Cloud SQL Proxy" and it returned no results. Also, looking at the documentation, it says, "App Engine does not currently provide a way to map static IP addresses to an application," so I don't know what IP to allow. I figured since it was totally internal, I could just use the unix socket. – JVE999 Dec 30 '19 at 16:46
  • As for the Cloud SQL proxy is concerned, is not a tool that resides on the Google Cloud Console, you need to install it by following this [link.](https://cloud.google.com/sql/docs/mysql/connect-admin-proxy#install) As for the IP, you have to authorize the ** public IP for your Cloud SQL instance ** to be used by your App Engine app, in case you won't use the Cloud SQL proxy, as stated [here.](https://cloud.google.com/sql/docs/mysql/connect-external-app#appaccessIP) – Christopher Rodriguez Conde Dec 30 '19 at 16:55
  • You will find further information by refering to the [post](https://stackoverflow.com/questions/59497265/i-am-not-able-to-execute-select-records-from-a-table-in-database-in-google-sql-c) I mentioned before. I hope it helps. – Christopher Rodriguez Conde Dec 30 '19 at 16:55
  • I figured it out. Deleting it and creating a new one solved the issue. Thanks for the help. – JVE999 Dec 30 '19 at 17:27
0

Well, I ended up getting it to work by deleting that instance and creating a new one. The steps I posted were the same. My only guess to why it didn't work before is perhaps when I created a private ip address for the last one, the system disabled connecting via the unix socket. I can't think of anything else that could have created that issue.

In the end, it did work like I thought it should. I didn't need to use a proxy or go through extra steps to give permissions to the app. So, I had it right, just for some reason it wasn't working. Deleting the old SQL instance and creating a new one worked, however.

JVE999
  • 3,327
  • 10
  • 54
  • 89