user3498228 user3498228 - 3 months ago 10
PHP Question

connection to google cloudsql

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 ?

Answer

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';
}
Comments