1

I spent last 2 days on this issue doing research and troubleshooting, but could not find a way to fix this.

I am trying to connect to Google Cloud Sql instance (2nd gen mysql) from App Engine Standard Environment using Laravel Framework.

My app.yaml looks like this

runtime: php73

runtime_config:
  document_root: public

handlers:
  - url: /favicon\.ico
    static_files: public/favicon.ico
    upload: public/favicon\.ico

env_variables:
  DB_DATABASE: DB_NAME
  DB_USERNAME: USER_NAME
  DB_PASSWORD: PASSWORD
  DB_SOCKET: "/cloudsql/SOCKET_NAME"

I am getting 500 error (Error Reporting is empty) In Logger I get this

2020/03/22 22:40:08 [error] 20#20: *2 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to a member function connection() on null in /srv/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1255
Stack trace:
#0 /srv/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1221): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#1 /srv/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1051): Illuminate\Database\Eloquent\Model->getConnection()
#2 /srv/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(968): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#3 /srv/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1004): Illuminate\Database\Eloquent\Model->newModelQuery()
#4 /srv/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(957): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#5 /srv/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1620): Illuminate\Database\Eloquent\Model->newQuery()
#6 /srv...PHP message: PHP Fatal error:  Uncaught Error: Call to a member function connection() on null in /srv/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1255

Any ideas?

ArmeniaH
  • 733
  • 1
  • 8
  • 18

1 Answers1

-1

I wrote an answer: CONNECTING FROM APP ENGINE (FLEX AND STANDARD) TO CLOUD SQL USING TCP AND UNIX DOMAIN SOCKETS 2020

Remember that:

At this time App Engine standard enviroments do not support connecting to the Cloud SQL instance using TCP. Your code should not try to access the instance using an IP address (such as 127.0.0.1 or 172.17.0.1) unless you have configured Serverless VPC Access.So let's configure Serverless VPC Access.

EDIT

your env variable in app.yaml

env_variables:
  # Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
  # values obtained when configuring your Cloud SQL instance.
  CLOUDSQL_USER: "test"
  CLOUDSQL_PASSWORD: "root"
  CLOUDSQL_DSN: "mysql:dbname=data;unix_socket=/cloudsql/your-project:us-central1:your-instance"

and you connection code:


$dsn = getenv('CLOUDSQL_DSN');
$user = getenv('CLOUDSQL_USER');
$password = getenv('CLOUDSQL_PASSWORD');
// create the PDO client
$db = new PDO($dsn, $user, $password);

This was working for me. You can find a detailed example here:

git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
    cd php-docs-samples/appengine/php72/cloudsql
Community
  • 1
  • 1
marian.vladoi
  • 7,663
  • 1
  • 15
  • 29
  • Thanks for the info, I came up to your post during research, unfortunately this is more of a Laravel + App Engine issue. As I wrote, I am trying to connect using unix socket, which I specified in my env as /cloudsql/CONNECTION_NAME – ArmeniaH Mar 22 '20 at 23:22