There has been an error processing your request

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

Getting the above error please someone provide me solution to solve this.

  • I've fixed it by changing connection string, make sure that your mysql is on and you have all the tables exists in the database. Commented Jun 5, 2015 at 15:03

17 Answers 17


I had the same issue while installing Magento 2.1.0 with PHP 7.0.13.
I could resolve this issue by setting the database hostname as instead of localhost .

  • Where should this be done?
    – joeybab3
    Commented Aug 26, 2019 at 17:09
  • inside ./app/etc/env.php
    – Jino
    Commented Sep 19, 2019 at 8:24
  • This worked for me, Thank you. Commented Aug 8, 2021 at 12:40
  • Works for me, thanks! Commented Aug 25, 2021 at 14:09

If your server listed in app/etc/local.xml is localhost and this error appears in Magento's error log, but Magento still is running ok, then mysql was restarted and the socket /tmp/mysql.sock or whatever socket file is set up in my.cnf temporarily disappeared.

Because it's missing, the PDO mysql connector reports SQLSTATE[HY000] [2002] No such file or directory

If you are continually getting this error thrown and have no access to Magento aside from the Exception Error page, then mysql has crashed completely and you need your hosting provider to find out why and restart mysql.


This seems to be wrong PHP version issue, you need to select which php have to use to run the command as follows:

/Applications/MAMP/bin/php/php7.1.20/bin/php bin/magento setup:upgrade

PS: Above sample is for MAC, you can do it similar way for windows or unix as well.

  • Great! it is also workable for me. Commented May 10, 2020 at 5:02

set the database hostname as instead of localhost.


This can also happen if ur filesystem is full ... df -h 1st ...ofcourse, mostly u shud get alerts before that...

mysqld wont be running normally, and would be in a "safe" mode



You have to change the hostname from localhost to in env configuration file in your magento root directory

  • Open app/etc/env.php
  • change 'host' => 'localhost' to 'host' =>''
  • Save file and retry

I had this same issue, and I used below to select which php have to use. /usr/local/etc/php/7.2/bin/php bin/magento setup:di:compile

See official docs: SQLSTATE[HY000] [2002] No such file or directory


for mac machine I have used this command and it will work

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

Make sure you edit your app/etc/local.xml properly. If you have no prefixes for your tables make sure the proper line is:




This is commonly caused by a different instance of PHP being used by the PHP command line compared to the web server. In particular, mysql.sock might be misconfigured if this is the case.

To determine whether or not you’re using one instance of PHP:

  1. Log in to your Magento server.
  2. Enter the following command:

    php -i | grep 'php.ini'

This determines the settings used by the PHP command-line interface (CLI). Note where php.ini is located.

  1. If you haven’t already done so, set up a phpinfo.php on your web server.
  2. Compare the value of Loaded Configuration File from phpinfo.php with the one from php -i.

phpinfo.php shows the php.ini file used by your web server. If it’s different from what's in PHP's php.ini, you must make all PHP settings consistent for both the PHP CLI and the web server.

For additional details, consult the documentation provided with your web server.


I have several versions of PHP available to me via the shell - typing php and pressing tab shows which ones my autocomplete knows about.

For me, PHP 5.4, 5.5 and 5.6 were all working fine, but PHP 7 wasn't finding the mysql.sock, and was crashing with the "SQLSTATE[HY000] [2002] No such file or directory error" discussed in this question.

# Prints [ok]
echo "<?php print(new mysqli('localhost', 'root', 'root', 'mysql') ? '[ok]' : '[error]').PHP_EOL; ?>" | php-5.6.20

# Prints [error] and shows a warning
echo "<?php print(new mysqli('localhost', 'root', 'root', 'mysql') ? '[ok]' : '[error]').PHP_EOL; ?>" | php-7.0.8

To see where your mysql server has its socket, use this command:

cat /etc/mysql/my.cnf | grep socket

In my case, with Percona Mysql installed in my vagrant system the sock socket file happened to live here:


(notice that it is called mysqld not simply mysql)

I have several versions of php to choose from and these commands showed me which ones had issues and which ones were fine:

The answer for me was simply to include code in the vagrant provisioner script to create a symlink between /tmp/mysql.sock and /var/run/mysqld/mysqld.sock

if [ ! -L /tmp/mysql.sock ]; then
    echo "Creating symbolic link for php 7 to connect to mysql.sock"
    ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

With the new symlink in place, the test script given before now works perfectly for all versions of PHP and I can use localhost in my code instead of having to give the IP address.


I get the same error "SQLSTATE[HY000] [2002] No such file or directory". And found that MySQL service was stopped. I started my MySQL service and its working fine.


I am using linux, run following commands.

/opt/lampp/bin/php  bin/magento setup:upgrade

And Working fine for me.


Depending on your Server Setup, look in your tmp/mysql folder and you may find a mysql.sock.lock file. You can safely remove this file and try your command again


Just try restarting the MySql

service mysql start

I had the same issue while installing Magento 2.3.5 with PHP 7.3.33. I could resolve this issue by setting the database hostname as mysql instead of localhost.

Note : I am using docker with mysql container.


just clean your cache it will work

bin/magento ca:cl && chmod -R 777 var/ pub/ generated/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.