0
votes

i'm new at Google Cloud and need help to connect my PHP App Engine in Standard Enviroment with Google Cloud SQL (MySQL). They are in the same region and the connect from MySQL Workbench works. In the Workbench i use the public ip from the Google SQL instance. But MySQLi gives me the error: "Connection refused". I tried it with PDO too and it gives me the same error.

Thats my PHP Code:

<?php
$host = '127.0.0.1';
$user = 'root';
$dbpassword = 'password';
$database = 'database';
$port = 3306;

$mysqli = new mysqli($host, $user, $dbpassword, $database, $port);
if ($mysqli->connect_errno) {
    die("Verbindung fehlgeschlagen: ".$mysqli->connect_error);
}

And thats the app.yaml:

runtime: php72
handlers:
- url: /css
  static_dir: css

- url: /src
  static_dir: src

- url: /js
  static_dir: js

beta_settings:
  cloud_sql_instances: "project:europe-west3:instanceid=tcp:3306"

I hope anyone can help me. Thanks.

Edit: I also tried:

$mysqli = new mysqli(null, $user, $dbpassword, $database, null, "/cloudsql/{connectionstring}");
if ($mysqli->connect_errno) {
    die("Verbindung fehlgeschlagen: ".$mysqli->connect_error);
}

And it gives me: "No such file or directory"

1

1 Answers

0
votes

This question has been addressed here, you can refer to the last answer which further specifies how to connect.

You need to ensure that the sqladmin API is enabled, add a file php.ini at the project root level (same as app.yaml) and add the following entry:

Extension = “mysqli.so”

Add some environment variables (data source name, MySQL user and password)

MYSQL_DSN: mysql:unix_socket=/cloudsql/datahouse;dbname=mydatabase

MYSQL_USER: root

MYSQL_PASSWORD: 'mypassword'

Finally the name of your connection string should be "/cloudsql/project-id";

$instance_name = "/cloudsql/YOUR_PROJECT_ID_WITHOUT_DB_NAME";

$mysqli = mysqli_connect(null, YOUR_DB_USER ,YOUR_DB_PASS, 'DB_NAME',null,$instance_name);

Let me know if that lets you connect.