简体   繁体   English

连接到本地数据库服务器时出错

[英]Error connecting to local db server

I have the following code: 我有以下代码:

try { 
      $db_conn = new PDO('mysql:host='.$host.';dbname=stats;port='.$port, $un, $pw);
    } catch (PDOException $e) {
      WriteLog("Could not connect to database!\nError: ".$e->getMessage());
      exit;
    }

    try { 
      $db_conn2 = new PDO('mysql:host=localhost;dbname=log', $un2, $pw2);
    } catch (PDOException $e) {
      WriteLog("Could not connect to database[2]!\nError: ".$e->getMessage());
      exit;
    }

It connects without errors to the first server (not local), but then it fails to connect to the local server. 它没有错误地连接到第一台服务器(不是本地服务器),但是随后却无法连接到本地服务器。 I get this error message: 我收到此错误消息:

Error: SQLSTATE[HY000] [2002] No such file or directory (||) 错误:SQLSTATE [HY000] [2002]没有这样的文件或目录(||)

I'm running PHP v5.4.27 我正在运行PHP v5.4.27

Solved it. 解决了。 Changing from localhost to 127.0.0.1 seems to fix it. 从本地主机更改为127.0.0.1似乎可以解决此问题。 I'm not sure why 我不确定为什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM