问题描述
我在将数据库云sql与php连接时遇到问题.我尝试了所有可能的连接方式(pdo,mysqli和mysql)他们都没有工作.
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.
我为can sql实例放置了一个IPv4,并在允许的网络部分中授权了计算引擎IP.当我尝试使用以下方式从计算引擎进行连接时:
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.
在php端,我输入了以下代码:
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)"
当我尝试这种方式时:
$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"
我还应该做什么?
谢谢!
推荐答案
挖了2天之后,我设法连接了云数据库
Well after diggin into it for 2 days I managed to connect cloud DB
首先运行此文件以获取权限修复(centos)
first run this for the permission fix (centos)
setsebool httpd_can_network_connect=1
对于php,应该这样写:
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>'
);
这篇关于谷歌云SQL与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!