0

I'm trying to connect to google sql cloud instance from custom runtime environment in App Engine.

When I follow the doc to connect using unix domain socket, it works. The problem is when I try to connect using a TCP connect. It shows:

Warning: mysqli_connect(): (HY000/2002): Connection refused in 
/var/www/html/index.php on line 3
Connect error: Connection refused

This is my app.yaml file:

runtime: custom
env: flex

beta_settings:
  cloud_sql_instances: testing-mvalcam:europe-west1:testdb=tcp:3306

resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

The Dockerfile:

FROM php:7.0-apache

ENV PORT 8080
CMD sed -i "s/80/$PORT/g" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && docker-php-entrypoint apache2-foreground

RUN docker-php-ext-install mysqli
RUN a2enmod rewrite

COPY ./src /var/www/html

EXPOSE $PORT

And index.php:

<?php

$link = mysqli_connect('127.0.0.1', 'root', 'root', 'test');

if (!$link){
    die('Connect error: '. mysqli_connect_error());
}

echo 'successfully connected';
mysqli_close($link);

?>

What am I doing Wrong?

4
  • Can you explain where did you get that error message, i.e. by ssh’ing to the Compute Engine running the docker process and then making an HTTP GET request using the local ip address and the port you exposed? Also, do you have the ‘Cloud SQL Admin API’ enabled for your project (you can check that through console.cloud.google.com/apis/api/sqladmin.googleapis.com/…)? Commented Mar 13, 2019 at 9:16
  • I did get the error directly when I access to the project url. Index.php loads, but connection to cloud sql is refused. And yes, I have Cloud SQL Admin API enabled. the strange thing is that when i use unix socket type connection (to /cloudsql/<INSTANCE_CONNECTION_NAME>) it works. its only when i try to connect using TCP when i get this error. Commented Mar 13, 2019 at 15:51
  • Ok, if you can ssh to one of the Compute Engine vm instances associated with the App Engine application (you can see them in the instances section on the App Engine menu in the Google Cloud Console), and run the command ‘netstat -tln’. In the output check for ‘local_ip_address:8080’ and insert ‘local_ip_address’ on the index.php as the first parameter of the mysqli_connect call instead of ‘127.0.01’. Commented Mar 13, 2019 at 16:52
  • Exactly!!, that was the problem. When I ssh to the instance and run 'netstat -tln' I get exposed ips like: 172.17.0.1:3306 and 172.17.0.1:8080. I don't understand. Why is it like this?. Anyhow, the official documentation is not correct. In it, it's clearly indicated that you have to point to localhost to connect to the cloud sql instance. Write a answer if you want, to be able to rate you and mark as valid answer. Thank you!. Commented Mar 13, 2019 at 22:28

2 Answers 2

1

The ip address ‘172.17.0.1’ is related with the docker container where the webserver is running, you can get more context on that in this documentation.

The documentation page you’re using might be lacking on adjusting the use case if you’re deploying with a presence of a Dockerfile. In the following documentation you can read more information about App Engine flexible runtimes.

Sign up to request clarification or add additional context in comments.

Comments

1

As demonstrated by the documentation you’re using (remember to click on the TCP CONNECTION tab on this page), on the section of the app.yaml related to Cloud SQL instances information about the TCP port in use by the database server is needed.

1 Comment

Yes sorry. This is the socked conf that I used before. With this cloud_sql_instances param, it shows same result. I fix the original question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.