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

2020-02-07 17:28发布

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.

14条回答
迷人小祖宗
2楼-- · 2020-02-07 18:07

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!

查看更多
3楼-- · 2020-02-07 18:10

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

查看更多
ゆ 、 Hurt°
4楼-- · 2020-02-07 18:15

change localhost to 127.0.0.1 in 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.

查看更多
▲ chillily
5楼-- · 2020-02-07 18:16

Try just

sudo service mysql restart

It's work for me

查看更多
Juvenile、少年°
6楼-- · 2020-02-07 18:19

If you used Mac OS and Brew try this:

mkdir /usr/local/etc/my.cnf.d
查看更多
Lonely孤独者°
7楼-- · 2020-02-07 18:20

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

查看更多
登录 后发表回答