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

后端 未结 17 1991
长发绾君心
长发绾君心 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 08:50

    The reason is that php cannot find the correct path of mysql.sock.

    Please make sure that your mysql is running first.

    Then, please confirm that which path is the mysql.sock located, for example /tmp/mysql.sock

    then add this path string to php.ini:

    • mysql.default_socket = /tmp/mysql.sock
    • mysqli.default_socket = /tmp/mysql.sock
    • pdo_mysql.default_socket = /tmp/mysql.sock

    Finally, restart Apache.

    0 讨论(0)
  • 2020-11-22 08:51

    The mySQL client by default attempts to connect through a local file called a socket instead of connecting to the loopback address (127.0.0.1) for localhost.

    The default location of this socket file, at least on OSX, is /tmp/mysql.sock.

    QUICK, LESS ELEGANT SOLUTION

    Create a symlink to fool the OS into finding the correct socket.

    ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp
    

    PROPER SOLUTION

    Change the socket path defined in the startMysql.sh file in /Applications/MAMP/bin.

    0 讨论(0)
  • 2020-11-22 08:52

    I was having the same problem and this is how I fixed it:

    I had this and it didn't work:

    $con = mysql_connect('localhost', 'root', '1234');
    

    I did this and it worked:

    $con = mysql_connect(':/Applications/MAMP/tmp/mysql/mysql.sock', 'root', '1234');
    

    Instead of using the mysql server, I connected directly to the Unix Socket. Worked for me.

    0 讨论(0)
  • 2020-11-22 08:54

    Mac OS X EL Capitan + MAMP Pro Do this

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

    Then do this

    cd /tmp
    sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
    

    Hope this saves you some time.

    0 讨论(0)
  • 2020-11-22 08:54

    i was having the same issue

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

    [ErrorException] Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in …htdocs/Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php

    So the solution is to make a symlink to the sock file thus resolving the issue. Do the following to resolve it:

    $ sudo mkdir /private/var/mysql/

    $ sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /private/var/mysql/mysql.sock

    source:http://www.reecefowell.com/2012/07/21/symfony2-cli-does-not-connect-to-mysql-while-browser-works-fine/

    0 讨论(0)
  • 2020-11-22 08:55

    Fix the looming 2002 socket error – which is linking where MySQL places the socket and where OSX thinks it should be, MySQL puts it in /tmp and OSX looks for it in /var/mysql the socket is a type of file that allows mysql client/server communication.

    sudo mkdir /var/mysql
    

    and then

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

    source: http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/

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