2014-10-01 40 views
0

我的MySQL隨機第二個端口分配給這樣的請求地址:MySQL的隨機分配第二個端口/ PHP

[2002]無法分配請求的地址(試圖通過TCP連接://127.0.0.1:3306: 3306)

此行爲在我的本地主機和我的服務器上觸發,所以我想它可能是我的代碼。我通過一個自寫的類來連接,它只使用常量來連接(這是正確的,在這些常量中沒有第二個端口),所以我非常無知,爲什麼有時會觸發這種行爲,以及第二個端口來自哪裏從。發生此錯誤時,腳本的執行將終止。

如果有人想瀏覽,我在這篇文章中添加了這個類。

任何建議,歡迎修復或環繞此。

提前致謝!

class mysql{ 

     protected $query    = false; 
     protected $lastquery   = false;   
     protected $result    = false; 
     protected $row     = false; 
     protected $args     = array(''); 
     protected static $mysqli  = null; 

     public function __construct(\mysqli $mysqli = null){ 
      self::$mysqli = $mysqli; 
      $this->establishAutoConnection(); 
     } 

     // Tries to establish connection, if none is set. 
     protected function establishAutoConnection(){ 
      IF(empty(self::$mysqli)): 
       SWITCH($_SERVER['SERVER_NAME']): 
        case 'localhost': 
         self::$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, MYSQL_DATABASE); 
         break; 
        case 'AN IP.IP.IP': 
         $this->connectToSlave(); 
         break; 
        default: 
         self::$mysqli = new mysqli(MYSQL_HOST_SERVER, MYSQL_USER_SERVER, MYSQL_PASSWD_SERVER, MYSQL_DATABASE_SERVER); 
       ENDSWITCH; 
      ENDIF; 
     } 

     public function connectToSlave(){ 
      self::$mysqli = new mysqli(SLAVE_HOST_SERVER, SLAVE_USER_SERVER, SLAVE_PASSWD_SERVER, SLAVE_DATABASE_SERVER); 
     } 

     public function connectToMaster(){ 
      self::$mysqli = new mysqli(MASTER_HOST_SERVER, MASTER_USER_SERVER, MASTER_PASSWD_SERVER, MASTER_DATABASE_SERVER); 
     } 

     // Sets the PDO arguments, which need to be replaced by '?' 
     public function setArgs(&$data, $type = false){ 
      $type = $type ?: $this->getTypeString($data); 
      $this->args[0] .= $type; 
      $this->args[] = &$data; 
      return $this; 
     } 

     // Reset function needs to be called in order to make a new query. 
     public function reset(){ 
      $this->args  = array(''); 
      $this->row  = false; 
      $this->result = false; 
      return $this; 
     } 

     // Loops through the found results. 
     public function loopThroughResults(){ 
      return ($this->row = $this->result->fetch_assoc()) 
       ? true 
       : false; 
     } 

     // Returns the row unformatted. If no result is found an emtpy array is returned. 
     public function getRow(){ 
      $this->row = $this->row ?: $this->result->fetch_assoc(); 
      return $this->row ?: array(); 
     } 

     // Returns the first result of the first row. 
     public function getSingleResult(){ 
      FOREACH($this->getRow() as $assoc => $value): 
       return $value ?: false; 
      ENDFOREACH; 
      return false; 
     } 

     // Returns the result by rowname 
     public function getByName($name){ 
      return isset($this->row[$name]) 
       ? $this->row[$name] 
       : false; 
     } 

     // If a new query is made, while the former query has not been resetted, a warning is stored or an error is thrown. 
     protected function isResetted(){ 
      IF($this->result): 
       $this->warning("PDO has not been resetted from query: ". $this->lastquery ." // new query: ". $this->query); 
      ENDIF; 
     } 

     // Executes the prepared query. 
     public function query($sql){ 
      $this->query = $sql; 
      $this->isResetted(); 
      $this->lastquery = $sql; 
      IF($prep = self::$mysqli->prepare($this->query)): 
       IF(count($this->args) > 1): 
        call_user_func_array(array($prep, 'bind_param'), $this->args); 
       ENDIF; 
       $prep->execute(); 
       $this->result = $prep->get_result(); 
       $prep->close(); 
      ELSE: 
       $this->error("Query $sql failed to prepare."); 
      ENDIF; 
     } 

     // Automatically generates the string of types for the submitted params if not set. ("ssisdss") etc. 
     protected function getTypeString($string){ 
      SWITCH(gettype($string)): 
       case 'string': 
        return 's'; 
       case 'double': 
        return 'd'; 
       case 'boolean': 
       case 'integer': 
        return 'i'; 
       case 'array': 
        $this->error('Unserialized array submitted to PDO.'); 
        break; 
       default: 
        $this->error('Unknown param type submitted to PDO. ' . print_r($string) . ' type: ' . gettype($string)); 
        break; 
      ENDSWITCH; 
     } 

     protected function error($msg){ 
      IF(!new error($msg)): 
       trigger_error($msg); 
      ENDIF; 
     } 

     protected function warning($msg){ 
      IF(!new warning($msg)): 
       trigger_error($msg); 
      ENDIF; 
     } 

    } 

回答

1

您是否在您的定義中添加了端口號?所以例如

MYSQL_HOST = 127.0.0.1:3306 
MYSQL_HOST_SERVER = 127.0.0.1:3306 

mysqli將在您的服務器定義中使用他的默認端口設置。因此,如果您在此處添加端口號,結果將與請求錯誤相同:

MYSQL_HOST = 127.0.0.1:3306:3306 
MYSQL_HOST_SERVER = 127.0.0.1:3306:3306