3

I'm trying to connect my App (from App Engine) to Cloud SQL, and I must be missing something:

  • I added a Cloud SQL instance
  • I Authorized my App

  • I added a test_db database via PhpMyAdmin (authorized my IP and connected to do that), tested that the user & password work

  • Finally I deployed an app with the following lines (to test connection):

.

$dbh = mysqli_init();
var_dump( mysqli_real_connect( $dbh, ':/cloudsql/my_project_id:database_name', 'test_db', 'test_db', 'test_db' ) );
var_dump( mysqli_real_connect( $dbh, 'localhost:/cloudsql/my_project_id:database_name', 'test_db', 'test_db', 'test_db' ) );
var_dump( mysqli_real_connect( $dbh, 'test_db', 'test_db', 'test_db', ':/cloudsql/my_project_id:database_name', 3306 ) );
var_dump( mysqli_real_connect( $dbh, 'test_db', 'test_db', 'test_db', 'localhost:/cloudsql/my_project_id:database_name', 3306 ) );
var_dump( mysqli_real_connect( $dbh, ':/cloudsql/my_project_id:database_name', 'test_db', 'test_db', null, 3306 ) );
var_dump( mysqli_real_connect( $dbh, 'localhost:/cloudsql/my_project_id:database_name', 'test_db', 'test_db', null, 3306 ) );
var_dump( mysqli_real_connect( $dbh, null, 'test_db', 'test_db', 'test_db', ':/cloudsql/my_project_id:database_name', 3306 ) );
var_dump( mysqli_real_connect( $dbh, 'localhost', 'test_db', 'test_db', 'test_db', ':/cloudsql/my_project_id:database_name', 3306 ) );
var_dump( mysqli_real_connect( $dbh, null, 'test_db', 'test_db', 'test_db', ':/cloudsql/my_project_id:database_name' ) );
var_dump( mysqli_real_connect( $dbh, 'localhost', 'test_db', 'test_db', 'test_db', ':/cloudsql/my_project_id:database_name' ) );
var_dump( $dbh->connect_error );

All if these rows returned False or Null (i.e. none of them work), help?
(if there's a solution let's please keep it with mysqli_real_connect() because that's what WordPress is using)

UPDATE: New Error
Currently I'm using this line:

var_dump( mysqli_real_connect( $dbh, 'localhost', 'test_db', 'test_db', 'test_db', 3306, 'mysql:unix_socket=/cloudsql/my_project_id:database_name' ) );

and I'm getting the following error

Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?

Asaf
  • 8,106
  • 19
  • 66
  • 116

1 Answers1

1

Based on https://cloud.google.com/appengine/docs/php/cloud-sql/, assuming you've already created user "test_db" with password "test_db" with access to database "test_db", you should do

mysqli_real_connect( $dbh, null, 'test_db', 'test_db', 'test_db', null, '/cloudsql/<your-project-id>:<your-instance-name>');
Mars
  • 1,422
  • 8
  • 9
  • Doesn't work either. I think the main error to solve now is the one I mentioned at the bottom of the question: "Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?" – Asaf Nov 19 '14 at 13:27
  • What was the error message when you used this to connect? I also assume you've already created the corresponding user & database in CloudSQL? – Mars Nov 19 '14 at 19:45
  • This is what I get now: "Access denied for user 'test_db'@'localhost' (using password: YES)" that doesn't make sense because I'm able to connect using phpmyadmin (via IP address, but I'd rather use the unix socket because it's safer obviously..) – Asaf Nov 20 '14 at 08:40
  • 1
    [**SOLVED!**](http://stackoverflow.com/a/21716370/333283) - `mysqli_real_connect( $dbh, null, 'test_db', null, 'test_db', null, '/cloudsql/:')` - leaving the password as "null" solved it! – Asaf Nov 20 '14 at 09:02