GAE PHP won't connect to Cloud SQL

2019-07-25 16:24发布

问题:

Can't seem to get GAE PHP to connect to Cloud SQL, using mysqli, says: Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?

I've authorized the app and made sure they're both in the same region i.e US

any ideas would be great, thanks

$mysqli = new mysqli(null, "USERNAME", "PASSWORD", "DATABASE", null,
"/cloudsql/PROJECT_NAME:db2");

when i use :/cloudsql/PROJECT_NAME:db2 it's saying there's error with 'sock'

When i use /cloudsql/PROJECT_NAME:db2 without : it's saying cannot connect to user@localhost

回答1:

If you are using default authentication, try using “root” as the username, null as password, and the instance-id listed in the Cloud SQL panel, like so. Example:

$conn = new mysqli(null, "root", null, "<databasename>", null, "/cloudsql/<instance id>”);


回答2:

In case anyone viewing this post and getting the 'Unable to find the socket transport "unix"'(that was me) and is using PDO instead of sqli, here is the proper code.

$db = new pdo("mysql:unix_socket=/cloudsql/$GAE_ProjectName:$location:$dbInstance;dbname=$dbName",$username,$password)

These, /cloudsql/$GAE_ProjectName:$location:$dbInstance, can all be found together and copy pasted from the dashboard of your SQL instance under "Connect to this instance" and then "Instance connection name."

The rest of it is just defining those variables and making sure the passwords are right!