php-docs-samples icon indicating copy to clipboard operation
php-docs-samples copied to clipboard

Issue running laravel 5.4 on app engine with second generation cloud sql instance

Open iz-ben opened this issue 9 years ago • 42 comments

I created a laravel application on app engine by following the steps highlighted here https://cloud.google.com/community/tutorials/run-laravel-on-appengine-flexible

I setup everything as indicated and I can even acess the database from my mysql client using all the users I created and make changes to the database, but when I run the application from the app engine url I get this error

PDOException in PDOConnection.php line 43: SQLSTATE[HY000] [2002] No such file or directory

 in PDOConnection.php line 43
at PDO->__construct('mysql:unix_socket=/cloudsql/{removed};dbname={removed}', '{removed}', '{removed}', array(0, 2, 0, false, false)) in PDOConnection.php line 43
at PDOConnection->__construct('mysql:unix_socket=/cloudsql{removed};dbname={removed}', {removed}', {removed}', array(0, 2, 0, false, false)) in Connector.php line 65
at Connector->createPdoConnection('mysql:unix_socket=/cloudsql/{removed};dbname={removed}', '{removed}', '{removed}', array(0, 2, 0, false, false)) in Connector.php line 44
at Connector->createConnection('mysql:unix_socket=/cloudsql{removed};dbname={removed}', array('driver' => 'mysql', 'host' => 'localhost', 'port' => '3306', 'database' => {removed}', 'username' => '{removed}', 'password' => {removed}', 'unix_socket' => '/cloudsql/{removed}', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'name' => 'mysql'), array(0, 2, 0, false, false)) in MySqlConnector.php line 24
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'port' => '3306', 'database' => '{removed}', 'username' => '{removed}', 'password' => '{removed}', 'unix_socket' => '/cloudsql/{removed}', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'name' => 'mysql')) in ConnectionFactory.php line 183

The thing I don't understand is why I am able to connect to the database using PDO in the standard environment but can't in the flexible and this only affects the laravel installation as provided in the aforementioned tutorial.

Need help because it has been several days without any results

iz-ben avatar May 18 '17 10:05 iz-ben

@iz-ben What does your app.yaml look like? Be sure to declare your CloudSQL connection name in app.yaml like this:

# Use the connection name obtained when configuring your Cloud SQL instance.
beta_settings:
    cloud_sql_instances: "CONNECTION_NAME"

bshaffer avatar May 23 '17 00:05 bshaffer

Did you fix this? I am having the same issue here: https://github.com/GoogleCloudPlatform/php-docs-samples/issues/377

ghost avatar May 30 '17 14:05 ghost

The issue could be you must use at least version 5.4.16 of Laravel, which has the appropriate change to support the DB_SOCKET environment variable. Check config/database.php to ensure the socket parameter is there, and if it isn't, add it manually.

bshaffer avatar May 30 '17 15:05 bshaffer

Yeah that fixed it for me, thank you so much. closed my other ticket.

ghost avatar May 30 '17 17:05 ghost

Sorry @bshaffer. I was away for a while. I'm using app.yaml as described in the file in the repository. I've also tried this configuration by @McGreeb updating the relevant connection details, but I'm still getting the error

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `sessions` where `id` = SS8nl94ojIxI5uaHMbA5nRSPnJssxoFeOom2C5ZM limit 1)

I'm stumped because the SQL instance lives in the same project as the app engine.

The output from php artisan --version shows Laravel Framework 5.4.24

iz-ben avatar May 31 '17 08:05 iz-ben

@iz-ben this is indeed very strange. One way you can debug this is see if the socket exists on the machine. SSH into one of your production app engine instance and do an ls -la /cloudsql and you should see a file there matching the values in app.yaml. Additionally, you can verify the socket exists in the container itself by running sudo docker exec -it gaeapp /bin/bash and run the same ls command.

bshaffer avatar Jun 07 '17 18:06 bshaffer

closing due to inactivity

bshaffer avatar Aug 09 '17 13:08 bshaffer

Somebody claims the issue was solved by removing the quotes from the cloud instance in beta settings. So from cloud_sql_instances: "YOUR_CLOUDSQL_CONNECTION_NAME" to cloud_sql_instances: YOUR_CLOUDSQL_CONNECTION_NAME

bshaffer avatar Oct 05 '17 17:10 bshaffer

Have same issue. Laravel 5.5.19 version.

My config:

runtime: php
env: flex

runtime_config:
  document_root: public

skip_files: 
    -.env

beta_settings:
  cloud_sql_instances: ttt-180713:europe-west1:ttt-mysql

env_variables:
  APP_LOG: errorlog
  APP_DEBUG: true
  APP_KEY: base64:oqUyw6PCgTeACFhautIOEx8Uia958GBodb4p+9KouVk=
  STORAGE_DIR: /tmp
  CACHE_DRIVER: database
  SESSION_DRIVER: database
  DB_CONNECTION: mysql
  DB_HOST: 127.0.0.1
  DB_PORT: 3306
  DB_DATABASE: ttt
  DB_USERNAME: ttt
  DB_PASSWORD: ''
  DB_SOCKET: "/cloudsql/ttt-180713:europe-west1:ttt-mysql"

Can anybody help or give advice how I can debug or fix it?

dimakrol avatar Oct 29 '17 15:10 dimakrol

I have the same issue, I cannot connect to db via socket. I'm using "laravel/lumen-framework": "5.5.*"

anhkhoi avatar Nov 05 '17 05:11 anhkhoi

Can anyone give link to app.yaml what works. Thanks.

dimakrol avatar Nov 05 '17 17:11 dimakrol

@anhkhoi I am not sure how the lumen framework fits in to the rest of Laravel, but you need to use uses laravel/laravel:5.4 and above.

@dimakrol What is the error you're receiving? Also, I only see a Laravel 5.5.0, and no 5.5.19.

bshaffer avatar Nov 06 '17 19:11 bshaffer

I have the same issue deploying my Laravel 5.5 app on GAE.

I tried to remove this:

SESSION_DRIVER: database

and it's work but other issue came up. My API returned 500 error. I suspect it is because of the session.

Any solution to enable SESSION_DRIVER: database ?

RajaAsyraf avatar Nov 09 '17 02:11 RajaAsyraf

I'd be interested to know the message of the 500 error. Also, did you follow the tutorial to enable DB sessions?

bshaffer avatar Nov 10 '17 00:11 bshaffer

Any resolution? Having the same issues. None of the solutions help.

ghost avatar Nov 17 '17 15:11 ghost

@bshaffer yes, i follow exactly the same tutorial given. I suspect the 500 error happened because of no session from database is configured since i removed this SESSION_DRIVER: database.

I tried set the configuration in app.yaml the way same from the given tutorial as below:

runtime: php
env: flex

runtime_config:
  document_root: public

# Ensure we skip ".env", which is only for local development
skip_files:
  - .env

env_variables:
  # Put production environment variables here.
  APP_LOG: errorlog
  APP_KEY: YOUR_APP_KEY
  STORAGE_DIR: /tmp
  CACHE_DRIVER: database
  SESSION_DRIVER: database
  ## Set these environment variables according to your CloudSQL configuration.
  DB_HOST: localhost
  DB_DATABASE: laravel
  DB_USERNAME: root
  DB_PASSWORD: YOUR_DB_PASSWORD
  DB_SOCKET: "/cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME"

beta_settings:
    # for Cloud SQL, set this value to the Cloud SQL connection name,
    # e.g. "project:region:cloudsql-instance"
    cloud_sql_instances: "YOUR_CLOUDSQL_CONNECTION_NAME"

And I skip the step third step in this tutorial which : https://cloud.google.com/community/tutorials/run-laravel-on-appengine-flexible

I didn't add this script in composer.json : "post-install-cmd": [ "chmod -R 755 bootstrap/cache", "php artisan cache:clear" ]

If the error still persist, try to install the fresh laravel 5.5 app with mysql with new google project.

I managed to solve this by installing a fresh new laravel app to google app engine but somehow I really have no idea why this happen.

Hope this help.

RajaAsyraf avatar Nov 18 '17 02:11 RajaAsyraf

@RajaAsyraf so your deployment only worked if you omitted the post-install-cmd?

bshaffer avatar Nov 20 '17 17:11 bshaffer

@iz-ben Activate your API clicking here: https://console.cloud.google.com/flows/enableapi?apiid=sqladmin&redirect=https:%2F%2Fconsole.cloud.google.com&_ga=2.164522367.-1360075521.1510572080

brenogalinari avatar Nov 29 '17 19:11 brenogalinari

@brenogalinari thanks. I'll try that. I had actually given up on Laravel on appengine, and switched to using datastore and expressjs for my application

iz-ben avatar Dec 05 '17 09:12 iz-ben

@bshaffer the solution provided by @brenogalinari worked for me, after all these months. Maybe the documentation should be updated to include that.

Thank you @brenogalinari for the soultion

iz-ben avatar Dec 05 '17 12:12 iz-ben