110
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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
luigi
  • 1,111
  • 2
  • 7
  • 3

19 Answers19

146

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.

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
naiz0
  • 1,601
  • 1
  • 9
  • 4
  • 7
    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`. – Nilpo Mar 17 '17 at 22:08
  • 2
    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: https://docs.phpmyadmin.net/en/latest/config.html – imclean May 29 '17 at 23:48
  • 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. – Bachsau Mar 24 '18 at 00:03
  • thanks a lot man. I was stuck with this for a while. Didn't know the problem is so small. – ashukid Feb 13 '20 at 17:36
  • 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 – peppy May 01 '21 at 00:08
31

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.

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
  • 1
    thanks for pointing out to make sure to remove sample from the name where ever it could be written. – til Jan 30 '23 at 16:56
27

Maybe your SQL server has been stopped.

sudo /etc/init.d/mysql start

or

sudo service mysqld start # older versions
sudo service mysql start # latest versions

And use service mysql status to check the status.


To keep running MySQL.

Use:

sudo systemctl enable MySQL # older versions
sudo systemctl start mysql.service # latest versions
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
23

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
faruq ah
  • 231
  • 2
  • 3
11

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!

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
9

I fixed the issue today using the steps below:

  1. If config.inc.php does not exists in phpMyadmin directory Copy config.sample.inc.php to config.inc.php.

  2. Add socket to /* Server parameters */

    $cfg['Servers'][$i]['socket'] = '/tmp/mysql.sock';
    
  3. Save the file and access phpmyadmin through url.

If you are using the mysql 8.0.12 you should use legacy password encryption as the strong encryption is not supported by the php client. enter image description here

Ketan Yekale
  • 2,108
  • 3
  • 26
  • 33
  • Definitely the best answer for macOS users! – Sebj Sep 16 '19 at 12:32
  • 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 – Sultanos Nov 12 '19 at 13:19
5

First of all, make sure the mysql service is running:

ps elf|grep mysql

you will see an output like this if is running:

4 S root      9642     1  0  80   0 -  2859 do_wai 23:08 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/tomcat-machine.local.pid
4 S mysql     9716  9642  0  80   0 - 200986 poll_s 23:08 pts/0   00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/tomcat-machine.local.err --pid-file=/var/lib/mysql/tomcat-machine.local.pid

then change localhost to 127.0.0.1 in config.inc.php

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

The default password for "root" user is empty "" do not type anything for the password

If it does not allow empty password you need to edit config.inc.php again and change:

$cfg['Servers'][$i]['AllowNoPassword'] = true;

The problem is a combination of config settings and MySQL not running. This was what helped me to fix this error.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
AbelSurace
  • 2,263
  • 1
  • 12
  • 16
3

Try just

sudo service mysql restart

It worked for me

Dharman
  • 30,962
  • 25
  • 85
  • 135
demenvil
  • 1,089
  • 1
  • 12
  • 25
1

If you are using archlinux or Ubuntu just type the commands below:

cd /opt/lampp/phpmyadmin

then

sudo gedit config.inc.php

Then when you access the file look for the scripts below,

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

remove '//' so that it remains as shown below:

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

Then change the 'localhost' to '127.0.0.1' and the script should turn as shown below:

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

Get back to your browser and type http://127.0.0.1/phpmyadmin/

If it is still refusing

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:

cd /opt/lampp/phpmyadmin

then

sudo gedit config.inc.php

search for the script below

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

Fill the password with your root password

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

And it should work.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Lamech Desai
  • 709
  • 6
  • 11
1

I faced this problem on CentOS.

First I try this

sudo service mysql restart

Then error shows up

Redirecting to /bin/systemctl restart mysql.service
Failed to restart mysql.service: Unit not found.

Then I Found this

Command this solve my problem

systemctl start mariadb.service
Stephen Chen
  • 3,027
  • 2
  • 27
  • 39
0

If your website was running fine and now unexpected ERROR: (HY000/2002): No such file or directory

Then you should NOT CHANGE any configuration file!

First check server error.log file, for Nginx it's located:

/var/log/nginx/error.log

Or for Apache try:

/var/log/apache2/error_log

In my case I could see PHP Fatal error: OUT OF MEMORY in error.log for nginx.

The solution: Add more RAM to server.


I tried to reboot the web server first before upgrading/adding RAM. Then I got rid of ERROR (HY000/2002) but got some ERROR 50X: Connection Error...

At this point I added 2GB RAM, rebooted the web server and all was working fine!

When the web server was up I noticed 200+ cron-jobs was stuck due to a loop bug in 1 PHP script.

So check which script is consuming memory.

Cyborg
  • 1,437
  • 19
  • 40
0

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).

This solved my problem.

lortschi
  • 2,768
  • 2
  • 18
  • 12
0
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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

If you are using SELinux, check this out.

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.

setsebool -P httpd_can_network_connect_db 1
Arafat Hasan
  • 2,811
  • 3
  • 21
  • 38
-1

If you used Mac OS and Brew try this:

mkdir /usr/local/etc/my.cnf.d
grantDEV
  • 552
  • 1
  • 6
  • 10
-1

I'm using Ubuntu 22.04 and I found that mysql service is not working. It works perfectly after running this command

sudo service mysql start

Hope it helps somewone

Go3shom
  • 9
  • 5
-2

In linux just do

sudo systemctl stop mysql

sudo systemctl start mysql

worked for me

-3

If above solutions doesn't work, try to change the default por from 3306, to another one (i.e. 3307)

fsalazar_sch
  • 348
  • 2
  • 6
  • 17
-8

You just need to rename ib_logfile0 and ib_logfile1 as ib_logfile_0 and ib_logfile_1. Then your problem would be solved.

Dann
  • 1
  • 2
  • That's totally unrelated. – Paweł Tomkiel Aug 17 '18 at 03:55
  • 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](https://stackoverflow.com/help/how-to-answer) guide. – Anthony May 12 '19 at 19:57