PHPUnit Testing: WordPress Unable to Connect to MySQL Database

I decided to use Semaphore CI to run automated test for a small WordPress plugin I made over the weekend.

I ran into a problem where WordPress was unable to connect to the database each time phpunit command is ran.

PHP Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /tmp/wordpress/wp-includes/wp-db.php on line 1538 

PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /tmp/wordpress/wp-includes/wp-db.php on line 1568 

PHP Warning: mysql_connect(): No such file or directory in /tmp/wordpress/wp-includes/wp-db.php on line 1568

Error establishing a database connection

So i double checked to ensure the MySQL user and password credentials were correct and indeed they were. What then could be the problem?

After a lot of head banging, the solution was replacing localhost with 127.0.0.1 as the database host (DB_HOST).

Don’t miss out!
Subscribe to My Newsletter
Invalid email address