0
votes

I'm having a problem to connect my DB cloud sql with php. I've tried every possible way to connect (pdo, mysqli & mysql) none of them worked.

I've put an IPv4 for the could sql instance and authorized the compute engine IP in the allowed networks section. When I'm trying to connect from the compute engine with

mysql --host=cloud_sql_IP --user=my_user --password

it's working and I'm able to see the tables.

On the php side I've put this code:

    $db = mysql_connect(<CLOUD_SQL_IPV4>, 'root', '');
    if (!$db){
       die('Connect Error (' . mysql_error());
    }

and I get "Connect Error (Permission denied)"

when I'm trying this way:

$conn = mysql_connect(":/cloudsql/<COMPUTE_INSTANCE>:<DB>", "root", "");
if (!$conn) {
    die('Connect Error (' . mysql_error());
}

I'm getting: "Connect Error (No such file or directory"

What else should I do?

Thanks!

1

1 Answers

6
votes

Well after diggin into it for 2 days I managed to connect cloud DB

first run this for the permission fix (centos)

setsebool httpd_can_network_connect=1

And for the php it should be written this way:

new mysqli(
    <IP_V4>, // host
    'root', // username
    '',     // password
    <DB_NAME>, // database name
    null,
    '/cloudsql/<GCE_INSTANCE>:<DB>'
);