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

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.

279947 次浏览

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.

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

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!

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.

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

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

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.

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

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.

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.

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.

If you used Mac OS and Brew try this:

mkdir /usr/local/etc/my.cnf.d

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

Try just

sudo service mysql restart

It worked for me

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.

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.

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

In linux just do

sudo systemctl stop mysql


sudo systemctl start mysql

worked for me