mysqli_real_connect(): (HY000/2002): No such file or directory

252,112

Solution 1

change localhost to 127.0.0.1 in /etc/phpmyadmin/config.inc.php

$cfg['Servers'][$i]['host'] = '127.0.0.1';

The reason for this is that pma tries to connect to the mysql.socket if you use localhost. If you use 127.0.0.1 PMA makes a TCP connection which should work.

Solution 2

This worked for me.

Locate config.sample.inc.php

Change

$cfg['Servers'][$i]['host'] = 'localhost';

into

$cfg['Servers'][$i]['host'] = '127.0.0.1';

Save.

Then rename the file and remove sample from the name.

Solution 3

Maybe your SQL server has been stopped

sudo /etc/init.d/mysql start

or

sudo service mysqld start

and useservice mysql status to check status

EDIT adding from comments:

Use:

sudo systemctl enable mysql 

to keep running MySQL.

Solution 4

I'm trying this before

cd /opt/lampp/phpmyadmin

Then

gedit config.inc.php

Find this

$cfg['Servers'][$i]['host'] = 

If there is localhost change it to 127.0.0.1

Note : if there is '//' remove // before $cfg['Servers'][$i]['host']

I checked again http://localhost/phpmyadmin/

Mysqli said:

"phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server."

I'm opening again config.inc.php and I found

$cfg['Servers'][$i]['password'] =

Fill the password with your password

It worked for me. It may work for you too.

Solution 5

mysqli_connect(): (HY000/2002): No such file or directory

Background:

I was just wanted to run some PHPUnit tests on my Mac, using Terminal. Some of the classes I wanna test was having to connect MySQL DB which was created and managed by PHPMyAdmin, and the web app I was working was working fine in the localhost. So when I ran that testcases I got the following error on my terminal:

mysqli_connect(): (HY000/2002): No such file or directory

Solution:

So with the itchiness I had to resolve it and run my test I searched in few SO Q&A threads and tried out. And a combination of changes worked for me.

  1. Locate your config.inc.php file which relates to PHPMyAdmin.
  2. Locate the line $cfg['Servers'][$i]['host'] mostly this line might have been commented out by default, if so please uncomment it.
  3. And replace that line with following:

$cfg['Servers'][$i]['host'] = '127.0.0.1';

  1. Save it and restart the MySQL Database from your XAMPP control panel (manager-osx).
  2. Change your $host parameter value in the mysqli_connect() method to following:

$_connection = mysqli_connect(**"localhost: 3306"**, $_mysql_username, $_mysql_password, $_database);

Note: This 3306 is my MySQL port number which is its default. You should better check what's your actual MySQL Port number before going to follow these steps.

And that's all. For me only these set of steps worked and nothing else. I ran my Unit Tests and it's working fine and the DB data were also updated properly according to the tests.

Why this works:

The closest reason I could have found is that it works because sometimes the mysqli_connect method requires a working socket(IP Address of the DB Host along with the Port number) of the database. So if you have commented out the $cfg['Servers'][$i]['host'] = '127.0.0.1'; line or have set 'localhost' as the value in it, it ignores the port number. But if you wanna use a socket, then you have to use '127.0.0.1' or the real hostname. (for me it appears to be regardless of the default port number we really have, we have to do the above steps.) Read the following link of PHPMyAdmin for further details.

Hope this might be helpful to somebody else out there.

Cheers!

Share:
252,112
Admin
Author by

Admin

Updated on February 17, 2021

Comments

  • Admin
    Admin over 3 years
    mysqli_real_connect(): (HY000/2002): No such file or directory
    

    PhpMyAdmin error on MacOS. I want answer I really have no idea what I need to do to resolve this.

  • Nilpo
    Nilpo about 7 years
    I was glad to find this answer to get things working, but what's the real fix? This is a workaround. You should be able to use localhost.
  • imclean
    imclean almost 7 years
    You'd think so, but apparently not. Regarding the "connection type" setting: "Deprecated since version 4.7.0: This setting is no longer used as of 4.7.0, since MySQL decides the connection type based on host, so it could lead to unexpected results." Ref: docs.phpmyadmin.net/en/latest/config.html
  • dadan
    dadan almost 7 years
    save me a lot of time
  • Bachsau
    Bachsau about 6 years
    The "real fix" is to read and understand the MySQL documentation, then edit your configuration files and have a system, where you know what's going on, instead of just following some advice and forget everything as soon as it's working. If you want to use localhost you have to configure MySQL to listen on a socket instead of TCP/IP. I would always recommend that, when your Database is on the same machine.
  • Paweł Tomkiel
    Paweł Tomkiel almost 6 years
    That's totally unrelated.
  • ImFarhad
    ImFarhad over 5 years
    As of Oct 2018 , MAMP Path should be /Applications/MAMP/bin/phpMyAdmin/config.inc.php. It worked like charm :-)
  • GAV
    GAV over 5 years
    service mysqld restart
  • AnthonyB
    AnthonyB about 5 years
    Your answer in not related to the question. Renaming log files will not help mysqli to find a socket or a connection to the MySQL server. If we misunderstood your answer you might need to update it, otherwise deleting this answer would be better. Please read the how to answer guide.
  • Sebj
    Sebj over 4 years
    Definitely the best answer for macOS users!
  • COS
    COS over 4 years
    It worked for me but the big question is why it stopped? Cause i'm getting this very often.
  • Sultanos
    Sultanos over 4 years
    While using php and httpd installed from HOMEBREW i had to use the legacy Password encryption to make it work before that using native PHP and httpd from Macos Catalina i had no need to change it, so when i reinstalled PHP and HTTPD from homebrew i lost connection... Thanks for the answer
  • ashukid
    ashukid over 4 years
    thanks a lot man. I was stuck with this for a while. Didn't know the problem is so small.
  • Abdulla Nilam
    Abdulla Nilam almost 4 years
    @Komi use sudo systemctl enable mysql to keep running MySQL. sorry for the late reply. I missed this completely.
  • Abdulla Nilam
    Abdulla Nilam about 3 years
    This is not about a port for sure
  • Abdulla Nilam
    Abdulla Nilam about 3 years
    This answer already there in multiple posts above with +1 > points
  • peppy
    peppy about 3 years
    What do I do if my MySQL server is hosted on a different IP, such as "64.233.191.255"? I have my MySQL bind=64.233.191.255 , in my config-db.php file, I have: $dbserver='64.233.191.255'; (I tried your solution anyways, still getting the error). I'm trying to get phpmyadmin 4:4.9.5+dfsg1-2 working on Ubuntu 20.04
  • gulab patel
    gulab patel about 2 years
    does it delete mysql data ? or just it will start only ?
  • demenvil
    demenvil about 2 years
    Hello, Just restart the server
  • mondayrris
    mondayrris about 2 years
    Very useful to save my life. Thanks my friend.
  • Abdulla Nilam
    Abdulla Nilam about 2 years
    @mondayrris happy to help <3