简体   繁体   English

需要帮助将App Engine PHP与Google Cloud SQL数据库连接

[英]Need help connecting my App Engine PHP with my Google Cloud SQL Database

Below is the sql connection code for my php script as it attempts to connect to my Google SQL Cloud Database 以下是我的php脚本在尝试连接到我的Google SQL云数据库时的sql连接代码

<?php

$con=mysqli_connect(":/cloudsql/projectID:google-cloud-instance","root","root","DATABASE");
// Check connection
if (mysqli_connect_errno())
{
echo "Oops! Something Must Have Went Wrong?: " . mysqli_connect_error()
;`

but when I process the overall php script i get the following error: 但是当我处理整个php脚本时,出现以下错误:

"php_network_getaddresses: gethostbyname failed. errno=0Error:"

I am a beginner at this, so if someone can help me.. 我是一个初学者,所以如果有人可以帮助我..

  1. Understand what this means 了解这意味着什么
  2. How the heck can I fix? 我该如何解决?

Thanks in Advance for the help! 先谢谢您的帮助!

You need to pass the cloud SQL instance name as the 'socket' parameter to the MySQLi constructor . 您需要将云SQL实例名称作为'socket'参数传递给MySQLi 构造函数 For example 例如

$instance_name = ":/cloudsql/projectID:google-cloud-instance";
$c = new mysqli(null, $username, $password, $database, 0, $instance_name);

if you're using the default auth (application level auth) then you can pass root and no password to connect. 如果您使用的是默认身份验证(应用程序级别身份验证),则可以传递root密码而无需连接密码。

$instance_name = ":/cloudsql/projectID:google-cloud-instance";
$c = new mysqli(null, "root", "", $database, 0, $instance_name);

See https://developers.google.com/appengine/docs/php/cloud-sql/#PHP_Connecting_to_your_Cloud_SQL_instance . 请参阅https://developers.google.com/appengine/docs/php/cloud-sql/#PHP_Connecting_to_your_Cloud_SQL_instance It seems like you are mixing mysqli and mysql_connect syntaxes. 似乎您正在混合mysqli和mysql_connect语法。 Pick one and follow the example in the documentation. 选择一个并按照文档中的示例进行操作。

For CloudSQL, it's recommended that you use the unix socket. 对于CloudSQL,建议您使用unix套接字。

If you're using mysqli_connect, you can specify the default socket in php.ini. 如果使用的是mysqli_connect,则可以在php.ini中指定默认套接字。

php.ini mysqli.default_socket= '/cloudsql/CONNECTION_NAME'

$db = mysqli_connect('localhost', 'USER', 'PASSWORD', 'DB'); $ db = mysqli_connect('localhost','USER','PASSWORD','DB');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM