10

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.

Nilpo
  • 4,675
  • 1
  • 25
  • 39
SomeNorwegianGuy
  • 1,494
  • 4
  • 17
  • 43
  • 1
    Possibly a duplicate of http://stackoverflow.com/q/4219970/1924128. See especially http://stackoverflow.com/a/32575869/1924128. – J.D. Feb 06 '16 at 15:21
  • Possible duplicate of [Warning: mysql\_connect(): \[2002\] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in](http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi) – Nilpo Mar 18 '17 at 01:44
  • In my case It was Azure issue with connection string https://stackoverflow.com/questions/56007265/how-to-use-mysql-connection-string-inside-php-code-which-is-served-by-azure-luni/56007385#56007385 – Shady Mohamed Sherif May 06 '19 at 14:40

7 Answers7

25

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

Doug Thompson
  • 595
  • 5
  • 15
14

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.

Nilpo
  • 4,675
  • 1
  • 25
  • 39
  • 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 Docker – Marecky Jan 27 '18 at 18:14
  • 1
    @Marecky 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. – Nilpo Jan 28 '18 at 03:37
  • 1
    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. – Marecky Jan 28 '18 at 21:13
  • The user is using a MAC so the `netstat` answer does not work. Consider using a different set of `options`. – pensebien Sep 07 '20 at 12:25
  • 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? – Arpita Hunka Sep 14 '20 at 16:28
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

  • 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. – risingPhoenix1979 Aug 16 '21 at 15:39
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.

Syscall
  • 19,327
  • 10
  • 37
  • 52
Vicar
  • 1
  • 1
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   |
pensebien
  • 506
  • 4
  • 16
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.

Showhan Ahmed
  • 109
  • 2
  • 9
-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.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • 6
    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. – Nilpo Mar 18 '17 at 01:23
  • 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. – MissionNext Mar 19 '17 at 04:01
  • 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. – Nilpo Mar 19 '17 at 04:37