Error connecting to Google Cloud SQL from App Engi

2019-08-17 14:39发布

问题:

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?

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



回答2:

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.