Question

im trying to connect to google cloudsql from appengine, but im facing this error:

No connection could be made because the target machine actively refused it

my php file, that i have uploaded to appengine:

$sql = new mysqli(null,
'myuser', // username
'mypass'
'mydb',
null,
'/cloudsql/**:**'
);
if ($mysqli->connect_error) {
echo 'no';
die('Connect Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}
else {
echo 'yes\n';
}

the appengine is allowed from the cloud sql. what should i do ?

Was it helpful?

Solution

I have been trying to replicate the error you are getting but am unable to reproduce it when using code sample you provided.

Also your code sample has a few mistakes, you create your connection as $sql but then check $mysqli for connection errors, you should be checking $sql. So I have changed all occurances of $mysqli to $sql and you the more conventional way to check for a connection error is to check connect_errno and then read the message from connect_error

$sql = new mysqli(null,
    'your-username', // username
    'your_password_or_blank_if_using_root',
    'database_name',
    null,
    '/cloudsql/project_id:instance_name'
);

if ($sql->connect_errno) {
    echo 'no';
    die('Connect Error (' . $sql->connect_errno . ') '. $sql->connect_error);
} else {
    echo 'yes\n';
}

OTHER TIPS

  1. The socket file is actually /cloudsql/project_id:region:instance_id (i.e myproject-prod:us-central1:instance-1)
  2. If you are on Flexible VM you might want to turn on debugging on one of your instances, then SSH to it, you can then verify the socket file indeed exists and try to connect over to it:

    sudo apt-get update sudo apt-get install mysql-client mysql -u your-username -S /cloud/sql/your-socket-file -p

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top