GoogleCloudPlatform / php-docs-samples

A collection of samples that demonstrate how to call Google Cloud services from PHP.
http://cloud.google.com/php
Apache License 2.0
966 stars 1.03k forks source link

SQLSTATE[HY000] [2002] No such file or directory #1015

Closed lpzdvd-packlink closed 4 years ago

lpzdvd-packlink commented 4 years ago

Hi everybody,

Unfortunately I'm another user trying to deploy a laravel 5.8 app on AE flex. I'm surprised about huge amount of people failing in same point, discussing about beta_settings, double quotes and all kind of things. After countless hours of attempts and lectures I've realized that /cloudsql socket is not present in any instance, so regarding of other configuration it seems to be a huge problem. This is my config:

database.php

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter(
                [
                    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                ]
            ) : [],
        ],

app.yaml

runtime: php
env: flex

runtime_config:
  document_root: public

skip_files:
  - .env
  - laradock/* #just for some local purposes

env_variables:
  APP_ENV: production
  APP_LOG: errorlog
  APP_KEY: #ommited
  # CACHE_DRIVER: file   #not using database because connection trouble
  # SESSION_DRIVER: file #not using database because connection trouble

  # database settings
  DB_HOST: 127.0.0.1 # would like to know if 127.0.0.1 or localhost is actually a thing
  DB_DATABASE: test
  DB_USERNAME: test
  DB_PASSWORD: XXXXX # ommited
  DB_SOCKET: "/cloudsql/<project>:<region>:<instance>" # ommited for security. would like to know if quotes are mandatory
  # APP_STORAGE: /tmp # not considering now
  # VIEW_COMPILED_PATH: /tmp # not considering now

beta_settings:
  cloud_sql_instances: "<project>:<region>:<instance>" # ommited for security. would like to know if quotes are mandatory

MySQL user setup

CREATE DATABASE test;
CREATE USER 'test'@'%' IDENTIFIED BY 'XXXXX';
GRANT ALL PRIVILEGES ON test.* TO 'user'@'%';

If I ssh into instance, /cloudsql folder does not exist. No socket present, all kind of errors related to socket no found / SQLSTATE[HY000] [2002] No such file or directory

side notes:

  1. I'm able to connect using cloud sql proxy and tcp connection:
./cloud_sql_proxy -instances=<project>:<region>:<instance>=tcp:3306
2020/01/29 11:34:02 failed to setup file descriptor limits: failed to set rlimit {&{8500 4096}} for max file descriptors: invalid argument
2020/01/29 11:34:03 Listening on 127.0.0.1:3306 for <project>:<region>:<instance>
2020/01/29 11:34:03 Ready for new connections
  1. Cloud SQL Admin API is enabled

  2. I've granted service-@gae-api-prod.google.com.iam.gserviceaccount.com Cloud SQL Admin role (also tried earlier to use client without success)

  3. I've granted @appspot.gserviceaccount.com Cloud SQL client role

What else can I do?

Thank you much, Regards

lpzdvd-packlink commented 4 years ago

Solved: https://github.com/GoogleCloudPlatform/community/issues/351#issuecomment-370056462

Unfortunately, during the build phase, the CloudSQL connection is not available.

That's why I'm only allowed to execute a migrate command from app container after a deployment. artisan migrate can not be run from composer.json.

Definitely it should be in the PHP docs regarding App Engine since I've read a lot of articles and issues trying to use migrate in gloud app deploy commands. It was very strange because cloudsql proxy container logs stated that it was ready to accept connections but none was ever made. app deploy command throwed SQLSTATE[HY000] [2002] No such file or directory errors.

Really tricky anyway.

LarryBarker commented 3 years ago

@lpzdvd-packlink Were you ever able to automate migrations after deploying?

lpzdvd-packlink commented 3 years ago

@LarryBarker Yes, I was able to run migrations using flex_exec tool. Syntax was outdated at that moment (see this) but it should work perfectly nowadays. Also take a look to the above link