Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in

后端 未结 17 1992
长发绾君心
长发绾君心 2020-11-22 08:27

I\'m trying to connect to my MySQL DB with the Terminal on my Apple (With PHP).

Yesterday it worked fine, and now I suddenly get the error in the title.

The

相关标签:
17条回答
  • 2020-11-22 09:02

    Another solution is to fix the socket location in the php.ini configuration file like this:

    pdo_mysql.default_socket=/tmp/mysql.sock
    

    Of course, the symlink works too, so its a matter of preference which one you change.

    0 讨论(0)
  • 2020-11-22 09:04

    For some reason mysql on OS X gets the locations of the required socket file a bit wrong, but thankfully the solution is as simple as setting up a symbolic link.

    You may have a socket (appearing as a zero length file) as /tmp/mysql.sock or /var/mysql/mysql.sock, but one or more apps is looking in the other location for it. Find out with this command:

    ls -l /tmp/mysql.sock /var/mysql/mysql.sock
    

    Rather than move the socket, edit config files, and have to remember to keep edited files local and away from servers where the paths are correct, simply create a symbolic link so your Mac finds the required socket, even when it's looking in the wrong place!

    If you have /tmp/mysql.sock but no /var/mysql/mysql.sock then...

    cd /var 
    sudo mkdir mysql
    sudo chmod 755 mysql
    cd mysql
    sudo ln -s /tmp/mysql.sock mysql.sock
    

    If you have /var/mysql/mysql.sock but no /tmp/mysql.sock then...

    cd /tmp
    ln -s /var/mysql/mysql.sock mysql.sock
    

    You will need permissions to create the directory and link, so just prefix the commands above with sudo if necessary.

    0 讨论(0)
  • 2020-11-22 09:05

    I am on XAMPP on Mac OS X, and Brian Lowe's solution above worked with a slight modification.

    The mysql.sock file is actually in "/Applications/xampp/xamppfiles/var/mysql/" folder. So had to link it up both in /tmp and /var/mysql. I haven't checked which one is used by PHP command line, but this did the fix, so I am happy :-)

    sudo su
    ln -s /Applications/xampp/xamppfiles/var/mysql/mysql.sock /tmp/mysql.sock
    mkdir /var/mysql
    ln -s /Applications/xampp/xamppfiles/var/mysql/mysql.sock /var/mysql/mysql.sock
    
    0 讨论(0)
  • 2020-11-22 09:09

    I also had this error, but could only fix it through the suggestion here.

    To summarize, use:

    127.0.0.1
    

    Instead of:

    localhost
    

    The reason is that "localhost" is a special name for the MySQL driver making it use the UNIX socket to connect to MySQL instead of the a TCP socket.

    0 讨论(0)
  • 2020-11-22 09:14

    I got the same errors. Mysql was running as a standalone application before I started phpMyAdmin.

    I just stopped mysql Then sudo /Applications/XAMPP/xamppfiles/xampp stop sudo /Applications/XAMPP/xamppfiles/xampp start

    It worked fine

    0 讨论(0)
提交回复
热议问题