6

我试着解释我遇到的问题!!!

我使用 PDO 扩展通过 pgpool-II 连接到 PostgreSQL。它在 Apache 中运行良好,但从 PHP CLI(在同一台机器上)我收到此 PDO 错误:

SQLSTATE[HY000]: 一般错误: 7 没有连接到服务器

我已经在谷歌和这里搜索过,但似乎没有人尝试过这样做。有人有什么主意吗?

编辑:

这是我用来建立连接的代码:

include 'manage_db.php';
include_once 'properties.php';
global $properties;

$dsn = 'pgsql:dbname=' . $properties['db_pgpool'] . ';host=localhost;port=' . $properties['port_pgpool'];

try{
   $mgmtDb = new ManageDb($dsn, $properties['username_pgpool'], $properties['password_pgpool']);
} catch (Exception $e) {
   echo 'PDO - Caught exception: ',  $e->getMessage(), "\n";
}

ManageDB 是我自己的类,它实现了一些实用功能以及创建数据库连接:

class ManageDb {
        var $db;

        function ManageDb($dsn, $username, $password){
            $this->db = new PDO($dsn, $username, $password);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }

....
4

1 回答 1

4

尝试这个


config.database.php

<?php
class DatabaseConfig {

    const DBNAME    = 'dbname';
    const HOST      = '123.1.233.123';
    const USER      = 'mysuperuser';
    const PASSWORD  = 'mysupperparrword';
    const PORT      = 5432; 
}
?>


类.database.php

<?php

include('config.database.php');

class Database {

    protected static $instance = null;

    final private function __construct() {}
    final private function __destruct() {
        self::$instance = null;
    }

    final private function __clone() {}

    public static function getInstance() {
        if (self::$instance === null) {
            try {
                self::$instance = new PDO(
                    'pgsql:host='   . DatabaseConfig::HOST . 
                    ';port='        . DatabaseConfig::PORT . 
                    ';dbname='      . DatabaseConfig::DBNAME . 
                    ';user='        . DatabaseConfig::USER . 
                    ';password='    . DatabaseConfig::PASSWORD
                );
                self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
            } catch (PDOException $e) {
                die('Database connection could not be established.');
            }
        }

        return self::$instance;
    }
    public static function __callStatic($method, $args) {
        return call_user_func_array(array(self::instance(), $method), $args);
    }
}
?>
于 2013-02-03T08:13:16.330 回答