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 swapping localhost
and 127.0.0.1
).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With