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.
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 swappinglocalhost
and127.0.0.1
).First, be sure that MySql is actually running. It won’t create a socket file if the process hasn’t started.
or
If MySql is running, changing your database host from
localhost
to127.0.0.1
in wp-config.php works, but it’s only a workaround.When you specify
localhost
, themysqli_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 specify127.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 runninglocate mysql.sock
. Here’s the settings that work for me on CentOS 6.8.php.ini
Other systems commonly use
/tmp/mysqld.sock
. You can verify the configured location by checking the MySql configuration filemy.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.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
It made me confused but I solved eventually.
My MySQL port is 3308, so I change “127.0.0.1” to “localhost:3308” in
It works!
In addition, I set
And…
But I think the most key is port changed.
Creating symbolic for
wp-cli
to find is more efficient and remove the issue of editing many files and keeping track with yourwp-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
Create file in
var
folder.wp plugin list from your WordPress installed folder works
I’m using Mac, and for the local environment, using MAMP. I’ve used the solution shared above that changed the
localhost
to127.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
with127.0.0.1:8889
in the wp-config.php file.Thanks to Jeff for writing this article with the solution.
On the reported line in /wp-includes/wp-db.php (Line 1489 in WordPress v 4.5) the code reads:
The issue goes away by adding a
@
in front. So the code should read:Add the missing
@
, save and upload the modified file to get the warning to disappear.