Connecting from App Engine to Cloud SQL not working

I am trying to connect my application (from App Engine) to Cloud SQL and I must be missing something:

  • I added a Cloud SQL instance
  • I have authorized my application

  • I added the test_db database via PhpMyAdmin (resolved my IP and connected for that), verified that the user and password are working

  • Finally, I deployed the application with the following lines (to test the 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 lines returned False or Null (i.e. none of them work), help?

(if there is a solution, let it be stored with mysqli_real_connect (), because that's what WordPress uses)

UPDATE: New error
I am currently 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 am getting the following error

Unable to find "unix" socket porting - did you forget to enable this when you configured PHP?

+3


source to share


1 answer


Based on https://cloud.google.com/appengine/docs/php/cloud-sql/ , assuming you have 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>');

      

+1


source







All Articles