【问题标题】:Connect to Cloud SQL via PHP hosted on Compute Engine通过 Compute Engine 上托管的 PHP 连接到 Cloud SQL
【发布时间】:2023-03-04 10:02:01
【问题描述】:

如何通过托管在 Compute Engine VM 上的 php 文件连接到 Cloud SQL 实例?

• 我在 Cloud SQL 中有一个名为 bd-sales 的 MySQL(1º 代)实例和一个名为 sales 的数据库

• 我有一个内置在 Compute Engine 中的 Linux(Debian) 虚拟机

Obs.:我想使用 PDO 建立连接

我试过了,但是没用:

<?php
    function getConnection(){
        try {
          $connection = new PDO('mysql:host=999.999.999.99;dbname=sales', 'user', 'pass');
          $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          return $connection;
          } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
            return;
        }
    }

    header("Access-Control-Allow-Origin: *");
    header('Content-Type: application/json');
?>

【问题讨论】:

    标签: php mysql google-cloud-platform google-compute-engine google-cloud-sql


    【解决方案1】:

    我解决了问题,更改了连接字符串:

    <?php
        function getConnection(){
            try {                                {GCSQL IPv4}                         {instance name}                 {Database name}
              $connection = new PDO('mysql:host=999.999.999.99;unix_socket=/cloudsql/instance:bd-instance;charset=utf8;dbname=vendas', 'user', 'pass');
              $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              return $connection;
              } catch(PDOException $e) {
                echo 'ERROR: ' . $e->getMessage();
                return;
            }
        }
    
    header("Access-Control-Allow-Origin: *");
    header('Content-Type: application/json');
    
    ?>     
    

    【讨论】:

      猜你喜欢
      • 2014-05-13
      • 2018-08-15
      • 2018-05-01
      • 1970-01-01
      • 2020-05-17
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多