Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /private/tmp/wordpress/wp-includes/wp-db.php on line 1452
Asked Answered
C

7

12

I'm trying to run PHPUnit to unittest a WordPress plugin, but the error in the title keeps showing up.

I used WP-CLI to setup the unittests, but also WP-CLI throws a similar error when I try to run it.

I use MAMP to run the database.

I have setup WP-CLI and PHPUnit as phars, that are aliased in ~/.bash-profile, and ran with the default "php" supplied by OS X. Changing this, and running WP-CLI and PHPUnit with the latest PHP version supplied by MAMP fixed WP-CLI(It was running and connecting to the database just fine) but PHPUnit was still throwing the same error.

I have tried editing the wp-config.php file, and setting the host to ":/path/to/mamp/mysql.socket", "localhost:/path/to/mamp/mysql.socket" and "127.0.0.1", none of which helped.

I'm totally stuck, and don't know what to try next.

Calzada answered 27/9, 2015 at 16:0 Comment(4)
Possibly a duplicate of https://mcmap.net/q/116055/-warning-mysql_connect-2002-no-such-file-or-directory-trying-to-connect-via-unix-tmp-mysql-sock-in/1924128. See especially https://mcmap.net/q/116055/-warning-mysql_connect-2002-no-such-file-or-directory-trying-to-connect-via-unix-tmp-mysql-sock-in.Estebanesteem
Possible duplicate of Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) inGalloromance
In my case It was Azure issue with connection string #56007765Chlorobenzene
So weird. Switching from "localhost" to "127.0.0.1" worked for me. In a rootless podman container, in the same pod as a mysql container, but still. Odd. Thanks!Andras
T
29

I just ran into this error - have you checked the schema exists that your wp-config.php is specifying?

In my case I'd outright forgotten to create it, so the fix was as simple as a CREATE DATABASE wordpress.

I've also run into this error when the wp-config.php database host is wrong (try swapping localhost and 127.0.0.1).

Tailgate answered 12/12, 2015 at 17:23 Comment(5)
Changing the localhost to 127.0.0.1 did it for me, thanks a ton!Exclude
Happened after installing PHP 7.1, this solved the issue, tx.Bibeau
Changing to 127.0.0.1 is not intended behavior. It is a workaround. Please see my answer if you want to correct the underlying problem.Galloromance
Changing localhost to the service name in docker did work for me.Protractile
So weird. Switching from "localhost" to "127.0.0.1" worked for me. In a rootless podman container, in the same pod as a mysql container, but still. Odd. Thanks!Andras
G
15

First, be sure that MySql is actually running. It won't create a socket file if the process hasn't started.

netstat -tulpn | grep mysql

or

ps -e | grep mysql

If MySql is running, changing your database host from localhost to 127.0.0.1 in wp-config.php works, but it's only a workaround.

When you specify localhost, the mysqli_real_connect() function tries to connect to your database through a Unix socket that it cannot find (hence the "no such file" error.) When you specify 127.0.0.1, it attempts to connect to your database using the default TCP port (typically 3306) which works.

That does not fix the problem that PHP does not know where to find your MySql socket. You need to configure the following options in your php.ini. The location to the socket will vary depending on your OS, but you can typically find it by running locate mysql.sock. Here's the settings that work for me on CentOS 6.8.

php.ini

pdo_mysql.default_socket = /var/lib/mysql/mysql.sock
mysqli.default_socket = /var/lib/mysql/mysql.sock

Other systems commonly use /tmp/mysqld.sock. You can verify the configured location by checking the MySql configuration file my.cfg.

Once you've configured PHP to point to the correct socket location, restart Apache/nginx so that it picks up the new settings.

Now you can use localhost as intended.

Galloromance answered 18/3, 2017 at 1:36 Comment(5)
This is very good explanation (+1) but allow me to criticize (not You, just mysqli driver developers decision): "When you specify localhost, the mysqli_real_connect() function tries to connect to your database through a Unix socket that it cannot find" When I put localhost string into any configuration of an application I work on I'm thinking about this as a domain name. How come this domain name becomes magical string inside of mysqli_real_connect that tells it to connect through a socket? I criticize this because my issue similar to OPs was a consequence of using MySQL server in DockerHocus
@Hocus You raise a good question. However, it's not a mysql developer decision, it's actually an underlying Unix practice. When you specify "localhost" it uses a local Unix socket. This filesystem-based method provides better security (can only be used locally) and better performance (no TCP overhead). Supplying any other hostname or IP address makes an external call which requires TCP/IP. This is how Unix networking works by design. Using 127.0.0.1 forces your system to make an external call to itself. This design allows you to connect to your server the way that external clients would.Galloromance
Thanks for more clarification about how Linux works, I recall I could have more issues regarding accessing servers by localhost domain name. Now I know it is broader case concerning more Linux software.Hocus
The user is using a MAC so the netstat answer does not work. Consider using a different set of options.Professoriate
Hi Nilpo, I have updated socket path in php.ini and using localhost, but still getting the asme issue. I am on the aws ec2 hosting Linux AMI. Do you have any idea about this?Garniture
H
2

Just go to your your phpMyAdmin, click on your database and copy the ip where is running the service and replace on your wp-config.php file:

/** MySQL hostname */ define('DB_HOST', 'localhost');

/** MySQL hostname */ define('DB_HOST', 'ip_where_is_running_mysqlserver');

Replace Localhost to ip of running MySQL Server

Hansom answered 7/12, 2017 at 10:28 Comment(1)
Thanks @Gilson Jelembi! Your solution worked for me. When using MAMP on my Mac I opened up phpMyAdmin and saw "Server: localhost:8889" text near the top of phpMyAdmin. Then I added define('DB_HOST', 'localhost:8889'); in my wp-config.php file, restarted MAMP, and opened up my local URL in a browser and it worked.Coalition
M
0

It made me confused but I solved eventually.

My MySQL port is 3308, so I change "127.0.0.1" to "localhost:3308" in

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

It works!

In addition, I set

$cfg['Servers'][$i]['controluser'] = '';

$cfg['Servers'][$i]['controlpass'] = '';

And...

$cfg['Servers'][$i]['auth_type'] = 'cookie';

But I think the most key is port changed.

Muttonchops answered 6/5, 2018 at 5:55 Comment(0)
P
0

Creating symbolic for wp-cli to find is more efficient and remove the issue of editing many files and keeping track with your wp-config file.

I did this for the MAC Sierra and MAMP PRO,

Create symbolic link of the mysql socket file after locating it using netstat

netstat -a | grep mysql

Create file in var folder.

cd /var 
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock

wp plugin list from your WordPress installed folder works

/path/to/wordpress/installation $ wp plugin list
-------------------------------------------------+----------+-----------+---------+
| name                                            | status   | update    | version |
+-------------------------------------------------+----------+-----------+---------+
| advanced-custom-fields                          | active   | none      | 5.9.0   |
| akismet                                         | active   | none      | 4.1.6   |
| bbp-voting                                      | inactive | available | 1.3.5   |
| breadcrumb-navxt                                | active   | none      | 6.5.0   |
| contact-form-7                                  | active   | none      | 5.2.2   |
| flamingo                                        | active   | none      | 2.2     |
| keydesign-addon                                 | active   | none      | 3.2     |
| post-my-contact-form-7                          | active   | none      | 4.1.8   |
Professoriate answered 8/9, 2020 at 2:9 Comment(0)
N
0

I'm using Mac, and for the local environment, using MAMP. I've used the solution shared above that changed the localhost to 127.0.0.1, but it didn't work for me.

I use the default ports (8888/8889) of MAMP, not 80/3306.

Also, I've checked the mySQL sock file as per the second answer but got it correctly set up.

Finally, the solution that worked for me is the following:

I've replaced the localhost with 127.0.0.1:8889 in the wp-config.php file.

Thanks to Jeff for writing this article with the solution.

Newsreel answered 22/5, 2023 at 10:15 Comment(0)
R
-5

On the reported line in /wp-includes/wp-db.php (Line 1489 in WordPress v 4.5) the code reads:

mysqli_real_connect( $this->duh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );

The issue goes away by adding a @ in front. So the code should read:

@mysqli_real_connect( $this->duh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );

Add the missing @, save and upload the modified file to get the warning to disappear.

Radiance answered 16/4, 2016 at 6:55 Comment(3)
This is horrible advice. Suppressing an error does not fix the problem. That's just sweeping it under the rug. It's unfortunate to think there are developers out there who would actually do this.Galloromance
There was an error in the WP coding. This was fixed in later WordPress versions by adding the missing @ symbol as noted above. This can be seen on line 1531 in v4.7.3 So, it looks like WP developers did the needed sweeping to clean up the mistake.Radiance
It's line 1540. And that line suppresses connection errors when WP_DEBUG is set to false in wp-config.php as noted by the fact that it also appears unsuppressed in the same if block. It is not a solution for the OP's question.Galloromance

© 2022 - 2024 — McMap. All rights reserved.