Google cloud sql with php

梦想与她 提交于 2019-12-08 10:05:10

问题


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:


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>'
);


来源:https://stackoverflow.com/questions/32044160/google-cloud-sql-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!