我试着解释我的问题!!!
我使用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,$password); $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } ....
试试这个
config.database.PHP
<?PHP class DatabaseConfig { const DBNAME = 'dbname'; const HOST = '123.1.233.123'; const USER = 'mysuperuser'; const PASSWORD = 'mysupperparrword'; const PORT = 5432; } ?>
class.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); } } ?>