doucai9270 2015-08-14 22:26
浏览 36
已采纳

为什么我不能通过终端运行有效的PDO连接?

My working index.php that runs in a browser looks like this:

<?php
    $dbh = new PDO(...whatever

It works in a bowser just fine. I can do anything.

Running in terminal like:

shell> php index.php

I get some error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Library/WebServer/Documents/index.php:3
Stack trace:
#0 /Library/WebServer/Documents/index.php(3): PDO->__construct('mysql:host=loca...', 'root', 'frekinpassword')
#1 {main}
  thrown in /Library/WebServer/Documents/index.php on line 3

What's going on here?

  • 写回答

2条回答 默认 最新

  • doumi1311 2015-08-14 22:30
    关注

    I think, your unix_socket changed and you didn't change your php client php.ini

    First of all, get your unix_socket location.

    $ mysql -uroot -p
    

    Enter your mysql password and login your mysql server from command line.

    mysql> show variables like '%sock%';
    +---------------+---------------------------------------+
    | Variable_name | Value                                 |
    +---------------+---------------------------------------+
    | socket        | /opt/local/var/run/mysql5/mysqld.sock |
    +---------------+---------------------------------------+
    

    Your unix_soket could be diffrent.

    Then change your php.ini, find your php.ini file from

    <? phpinfo();
    

    You maybe install many php with different version, so please don't assume your php.ini file location, get it from your 'phpinfo';

    Change your php.ini:

    mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
    
    mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock
    
    pdo_mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
    

    Then restart your apache or php-fpm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀