I've been using Laravel with gcloud to create a website (App Engine) and database (SQL) but for some reason I can't get the App Engine to connect to the database.
The error I'm getting is "SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from
users)"
from laravel's own debugging and "Failed to load resource: the server responded with a status of 500 ()"
from the browser inspect window.
It works fine if I run a local instance since I've allowed my IP to access the SQL database. I can also see the data using MySQL workbench.
I've used the following tutorial. There is also exists one for Standard Environment but I didn't even get this far on that on. Using Laravel 5.5 worked a bit better.
Other things I've tried includes connecting the App Engine like it's an external app that's not in the same project as the database. https://cloud.google.com/sql/docs/mysql/connect-app-engine which also includes granting roles to service accounts: https://cloud.google.com/iam/docs/granting-roles-to-service-accounts
I've also done the following steps:
Click Add, enter the email address as the member, and choose Cloud SQL Client as the role. Then click Add.
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: app-key
## Set these environment variables according to your CloudSQL configuration.
DB_CONNECTION: mysql
DB_HOST: sql-ip
DB_PORT: 3306
DB_DATABASE: database-name
DB_USERNAME: db-username
DB_PASSWORD: pass
DB_SOCKET: /cloudsql/instance-connection:name
beta_settings:
cloud_sql_instances: instance-connection:name```
"chmod -R 777 bootstrap\/cache",
"chmod -R 777 storage",
"php artisan config:cache"
]
It's also worth noting that the bootstrap config file has had errors that makes it point to local files on my computer and not on the uploaded files.
If you need any more information, ask.
Thanks.
I got it working thanks to Samuel Romero comment. I followed the following "tutorial": https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql making my app.yaml file to look as follows
runtime: php
env: flex
runtime_config:
document_root: public
# Ensure we skip ".env", which is only for local development
skip_files:
- .env
env_variables:
SQLALCHEMY_DATABASE_URI: >-
mysql+pymysql://USERNAME:PASSWORD@/DATABASE?unix_socket=/INSTANCE_CONNECTION_NAME
# Put production environment variables here.
APP_LOG: errorlog
APP_KEY: APPKEY
## Set these environment variables according to your CloudSQL configuration.
DB_CONNECTION: mysql
DB_HOST: localhost;unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME
DB_DATABASE: DATABASE
DB_USERNAME: USERNAME
DB_PASSWORD: PASSWORD
beta_settings:
cloud_sql_instances: INSTANCE_CONNECTION_NAME
Apart from that enabling the Cloud SQL API, which I should have had enabled but maybe got disabled by some reason. And then I also ran "gcloud auth application-default login" in the Cloud SDK.
Hopefully this will help someone, and if there is a security issue with this please tell me.