我试着解释我的问题!!!
我使用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);
}
....
答案 0 :(得分:3)
试试这个
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);
}
}
?>