通过 PDO 连接到 AWS RDS

Connecting to AWS RDS via PDO

我一直在尝试通过 PDO 将我的 PHP 应用程序连接到 AWS RDS 上的 MySQL 数据库。我在这里看到了一个类似的问题: 但这已经超过 4 年了,没有明确的答案。

我已经尝试了几种方法。首先,将主机名作为 ' 在 dsn 字符串中显式传递端口

$dsn = $dsn = "mysql:host=" . $this->host . ";port=". $this->port . ";dbname=" . $this->name . ";charset=utf8";

(下面注释掉)。都不行!

代码片段是:

      $dsn = null;
      $options = null;
      $this->host = SYSTEM_CONFIG["database"]["host"];
      $this->type = SYSTEM_CONFIG["database"]["type"];
      $this->name = SYSTEM_CONFIG["database"]["name"];
      $this->user = SYSTEM_CONFIG["database"]["user"];
      $this->pass = SYSTEM_CONFIG["database"]["pass"];
      /* New */
      $this->port = SYSTEM_CONFIG["database"]["port"];

      switch ($this->type) {
         case "SQLSRV":
            $dsn = "sqlsrv:Server=" . $this->host . ";Database=" . $this->name;
            $options = [
               PDO::ATTR_PERSISTENT => false,
               PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
               PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
               PDO::ATTR_STRINGIFY_FETCHES => false
            ];
            break;
         default:
            $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->name;
            //$dsn = "mysql:host=" . $this->host . ";port=". $this->port . ";dbname=" . $this->name . ";charset=utf8";
            $options = [
               PDO::ATTR_PERSISTENT => false,
               PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
               PDO::ATTR_EMULATE_PREPARES => false,
               PDO::ATTR_STRINGIFY_FETCHES => false
            ];
      }
      try {
         $this->pdo = new PDO($dsn, $this->user, $this->pass, $options);
      } catch (PDOException $e) {
         $this->logError($e);
      } catch (Exception $e) {
         $this->logError($e);
      }

的一件事是将IP地址和端口作为主机名以

的形式传递
$this->host = "<IP-address>:3306"

但是,我只是通过 ping 主机名找到了 IP 地址,我不确定这是静态 IP 地址还是动态 IP 地址(后者在配置文件中不好!)。

如有任何帮助,我们将不胜感激!

代码现在可以正常工作了,尽管非常令人沮丧的是我从来没有弄清楚为什么它一开始就不起作用!我怀疑这与没有正确获取端口号有关 - 可能是某个地方的拼写错误 'accidently' 在我尝试时得到了纠正(而不是故意的)。此代码现在有效(仅适用于 MySQL):

      $dsn = null;
      $options = null;
      $this->host = SYSTEM_CONFIG["database"]["host"];
      $this->type = SYSTEM_CONFIG["database"]["type"];
      $this->name = SYSTEM_CONFIG["database"]["name"];
      $this->user = SYSTEM_CONFIG["database"]["user"];
      $this->pass = SYSTEM_CONFIG["database"]["pass"];
      $this->port = SYSTEM_CONFIG["database"]["port"];

      switch ($this->type) {
         case "SQLSRV":
            // Other untested code...
            break;
         default: 
            $dsn = "mysql:host={$this->host};port={$this->port};dbname={$this->name};charset=utf8";
            $options = [
               PDO::ATTR_PERSISTENT => false,
               PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
               PDO::ATTR_EMULATE_PREPARES => false,
               PDO::ATTR_STRINGIFY_FETCHES => false
            ];
      }
      try {
         $this->pdo = new PDO($dsn, $this->user, $this->pass, $options);
      } catch (PDOException $e) {
         $this->logError($e);
      } catch (Exception $e) {
         $this->logError($e);
      }