简体   繁体   English

无法连接Google App Engine和Google Cloud SQL

[英]Unable to Connect Google App Engine and Google Cloud SQL

I have been trying to connect to the sample PHP hello world app running in Google App Engine to Google Cloud SQL. 我一直在尝试将在Google App Engine运行的示例PHP hello world应用程序连接到Google Cloud SQL.

Im trying to test a sample database connection. 我正在尝试测试示例数据库连接。 Using an external client like Navicat I can get access, however, connecting the app directly to cloud sql is not working. 使用像Navicat这样的外部客户端,我可以访问,但是,将应用程序直接连接到cloud sql不能正常工作。

I have reviewed numerous stackoverflow similar problems and thoroughly worked through the examples provided by google, with no luck. 我已经回顾了许多stackoverflow类似的问题,并通过google提供的示例进行了彻底的研究,没有运气。

Here you can see the hello world file with the output of three different attempts to connect to the sql db, I am echoing the json of each object as well.... which is coming back empty or null. 在这里,您可以看到hello world文件,其中包含三种不同尝试来连接到sql db的输出,我也正在回显每个对象的json ....回传为空或null。

https://beaming-glyph-107521.appspot.com/ https://beaming-glyph-107521.appspot.com/ 在此处输入图片说明

Im not sure what else to do here 我不确定在这里还能做什么

My Google App has access to the Cloud SQL instance: 我的Google App可以访问Cloud SQL实例: 在此处输入图片说明

I right now my user sql user is root and the password blank, but Ive also tried a user with a defined password - still with no luck. 我现在我的用户sql用户是root用户,密码为空,但是Ive也尝试了一个具有定义密码的用户-仍然没有运气。

     <?php

     ini_set('display_errors',1);
     ini_set('display_startup_errors',1);
     error_reporting(-1);

       echo 'Hello, world!';

     //Copied and Pasted straight from the provided connection strings in Cloud SQL
     // Using PDO_MySQL (connecting from App Engine)
     $db = new pdo('mysql:unix_socket=/cloudsql/beaming-glyph-107521:testdb',
         'root',  // username
         ''       // password
     );

     // Using mysqli (connecting from App Engine)
     $sql = new mysqli(
         null, // host
         'root', // username
         '',     // password
         '', // database name
         null,
         '/cloudsql/beaming-glyph-107521:testdb'
     );

     // Using MySQL API (connecting from App Engine)
     $conn = mysql_connect(':/cloudsql/beaming-glyph-107521:testdb',
         'root', // username
         ''      // password
     );

     echo "<br><br/>pdo connection: ".json_encode($db);
     echo'<br><br/> msqli connection: '.json_encode($sql);
     echo '<br><br/> mysql conn: '.json_encode($conn);

The problem is in using jsonencode() to display the db object. 问题在于使用jsonencode()显示db对象。 For whatever reason it was showing it as blank, when in fact it actually existed. 实际上,无论出于何种原因,它都将其显示为空白。 After several more hours of investigation I was able to run a query and display the results immediately after the db object was created. 经过几个小时的调查,我能够运行查询并在创建db对象后立即显示结果。

So in conclusion, looks like I've been connecting all along. 总而言之,看起来我一直都在联系。 If you are trying to debug a db connection try running a test query and displaying the results immediately after creating the db object to test the connection. 如果要调试数据库连接,请尝试运行测试查询并在创建数据库对象以测试连接后立即显示结果。

    $db = new pdo('mysql:unix_socket=/cloudsql/table-1075:testdb;dbname=sampledb',
'root',  // username
''       // password
  );

 var_dump($db);
 $sql='SELECT * FROM table';
 foreach ($db->query($sql) as $row) {
 print $row['Index'] . "\t";

 }

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

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