duanlaiquan8174 2014-08-05 20:44
浏览 158
已采纳

如何在MAMP中访问PDO?

I'm setting up a PDO connection in a test script:

use app\SomeDAO;

class SomeTest extends \PHPUnit_Framework_TestCase
{
    protected $db;

    public function setUp()
    {
        $dsn = "mysql:host=localhost;dbname=baseball;user=root;password=root";
        $this->db = new PDO($dsn);
    }

I'm getting an error:

PDOException: SQLSTATE[HY000] [2002] No such file or directory.

How can I use PDO here?

  • 写回答

1条回答 默认 最新

  • dongquechan4414 2014-08-05 20:50
    关注

    In Unix based (like linux, bsd, or OS X) systems, with MySQL localhost is secret code for try-to-use-a-socket, unless a you force it via a protocol flag to not do this (and no one ever does this). Just remember localhost usually equals socket file.

    If Mysql in your MAMP is running in non-socket mode, you can try replacing the host value with 127.0.0.1 which connects via TCP on via port to the local machine--you'll need to figure out which port it's running on if it's not the default port.

    If you look at the MAMP start script

      less /Applications/MAMP/bin/startMysql.sh
    

    You can see if it's starting in socket mode, and what file it's using if it has a config param like:

      --socket=/Applications/MAMP/tmp/mysql/mysql.sock 
    

    You can also investigate what open socket mysql might be using by following the answer in this question: error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'

    If you're running in socket mode, you need to make sure PDO knows the path to the socket file. One way to do this is specify the unix_socket instead of host in the dsn:

      $dsn =  "mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=baseball;user=root;password=root";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题