Unable to access Google Cloud SQL from GAE: “SQLSTATE[HY000] [2002] Unknown error 4294967295”

给你一囗甜甜゛ 提交于 2019-12-13 03:48:44

问题


I'm trying to create a program that shows the emails of teachers. The name and emails of these teachers are in a database.

I took the sample code from Google and modified it, but it does not seem to work. I am also unable to resolve the error, as the message given is "Unknown error". How should I go about to debug / fix this error?

I have restarted the sql server, and tried tweaking the code in many many ways, but it doesn't seem to want to give me my result :( I have added the gae app to the applications list for the database as well.

The relevant code snippet is found here:

For security, I have changed the code so that the IP is 111.111.111.111 and the database is db.

<h2>Teachers:</h2>
  <?php
  // Create a connection.

  $db = null;
  if (isset($_SERVER['SERVER_SOFTWARE']) &&
  strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) {
    // Connect from App Engine.
    try{
       $db = new PDO('mysql:host=111.111.111.111:3306;dbname=db', 'root', 'password');
    }catch(PDOException $ex){
        die(json_encode(
            array('outcome' => false, 'message' => 'Unable to connect.', 'error' => $ex->getMessage())
            )
        );
    }
  }

  try {
    // Show existing guestbook entries.
    foreach($db->query('SELECT * from db.teacher') as $row) {
            echo "<div><strong>" . $row['title'] . " " .$row['name'] . "</strong> has email <br> " . $row['email'] . "</div>";
     }
  } catch (PDOException $ex) {
    echo "An error occurred.";
  }
  $db = null;
  ?>

When I go to the website, this is what I am greeted with:

Teachers:

{"outcome":false,"message":"Unable to connect.","error":"SQLSTATE[HY000] [2002] Unknown error 4294967295"}

Thank you!!


回答1:


You don't need to specify the IP address

$db = new pdo('mysql:unix_socket=/cloudsql/<your-project-id>:<your-instance-name>;dbname=<database-name>',
  'root',  // username
  ''       // password
  );

https://developers.google.com/appengine/docs/php/cloud-sql/

If your app and DB are connected properly (you have given permission to that GAE app to access that Google SQL database etc) then it should "just work".

If you want to connect by IP you have to go in to the instance and give it an IP address manually.




回答2:


When connecting from a GAP authorized app, you don't need to use password. Did you try this? Connect without password.



来源:https://stackoverflow.com/questions/23581543/unable-to-access-google-cloud-sql-from-gae-sqlstatehy000-2002-unknown-erro

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