postgresql – PHP PDO PGPOOL PGSQL – SQLSTATE [HY000]:一般错误:7没有连接到服务器

前端之家收集整理的这篇文章主要介绍了postgresql – PHP PDO PGPOOL PGSQL – SQLSTATE [HY000]:一般错误:7没有连接到服务器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试着解释我的问题!!!

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

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

我已经在谷歌和这里搜索了,但似乎没有人试过这样做.有谁有想法吗?

编辑:

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

  1. include 'manage_db.PHP';
  2. include_once 'properties.PHP';
  3. global $properties;
  4.  
  5. $dsn = 'pgsql:dbname=' . $properties['db_pgpool'] . ';host=localhost;port=' . $properties['port_pgpool'];
  6.  
  7. try{
  8. $mgmtDb = new ManageDb($dsn,$properties['username_pgpool'],$properties['password_pgpool']);
  9. } catch (Exception $e) {
  10. echo 'PDO - Caught exception: ',$e->getMessage(),"\n";
  11. }

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

  1. class ManageDb {
  2. var $db;
  3.  
  4. function ManageDb($dsn,$username,$password){
  5. $this->db = new PDO($dsn,$password);
  6. $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  7. }
  8.  
  9. ....
试试这个

config.database.PHP

  1. <?PHP
  2. class DatabaseConfig {
  3.  
  4. const DBNAME = 'dbname';
  5. const HOST = '123.1.233.123';
  6. const USER = 'mysuperuser';
  7. const PASSWORD = 'mysupperparrword';
  8. const PORT = 5432;
  9. }
  10. ?>

class.database.PHP

  1. <?PHP
  2.  
  3. include('config.database.PHP');
  4.  
  5. class Database {
  6.  
  7. protected static $instance = null;
  8.  
  9. final private function __construct() {}
  10. final private function __destruct() {
  11. self::$instance = null;
  12. }
  13.  
  14. final private function __clone() {}
  15.  
  16. public static function getInstance() {
  17. if (self::$instance === null) {
  18. try {
  19. self::$instance = new PDO(
  20. 'pgsql:host=' . DatabaseConfig::HOST .
  21. ';port=' . DatabaseConfig::PORT .
  22. ';dbname=' . DatabaseConfig::DBNAME .
  23. ';user=' . DatabaseConfig::USER .
  24. ';password=' . DatabaseConfig::PASSWORD
  25. );
  26. self::$instance->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  27. self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES,true);
  28. } catch (PDOException $e) {
  29. die('Database connection could not be established.');
  30. }
  31. }
  32.  
  33. return self::$instance;
  34. }
  35. public static function __callStatic($method,$args) {
  36. return call_user_func_array(array(self::instance(),$method),$args);
  37. }
  38. }
  39. ?>

猜你在找的Postgre SQL相关文章