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?
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.