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.
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.
Locate your config.inc.php file which relates to PHPMyAdmin.
Locate the line $cfg['Servers'][$i]['host'] mostly this line might have been commented out by default, if so please uncomment it.
And replace that line with following:
$cfg['Servers'][$i]['host'] = '127.0.0.1';
Save it and restart the MySQL Database from your XAMPP control panel (manager-osx).
Change your $host parameter value in the mysqli_connect() method to following:
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.
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."
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.
so then get back to the same file config.inc.php you were editing and do as follows:
I had the same problem. In my /etc/mysql/my.cnf was no path more to mysqld.sock defined. So I started a search to find it with: find / -type s | grep mysqld.sock but it could not be found.
I reinstalled mysql with: apt install mysql-server-5.7 (please check before execute the current sql-server-version).
mysqli_connect(): (HY000/2002): No such file or directory
I was facing same issue on debian9 VM, I tried to restart MySQL but it didn't solve the issue, after that I increased the RAM (I was reduced) and it worked.
I had the same problem. I was running CentOS 8 with SELinux enforcing, and I was getting the error mentioned in the question (mysqli_real_connect(): (HY000/2002): No such file or directory) despite having all the configurations fixed correctly. I later got out of trouble after allowing MySQL connections through SELinux.
Check SELinux status using this command:
sestatus
Allow Apache to connect database through SELinux
setsebool httpd_can_network_connect_db 1
Use -P option makes the change permanent. Without this option, the boolean would be reset to 0 at reboot.