WP CLI error “No such file or directory”

A while back, wordpress announced their command line tool – WP CLI. The CLI makes WordPress maintenance quick and easy via terminal commands. WP CLI made few mundane and boring tasks somewhat less boring.

Operations like

  • Setting up new WordPress installation on local machine before development,
  • Downloading a fresh copy of WordPress
  • Updating plugins, themes, WordPress core
  • Transients housekeeping etc

are now somewhat more interesting and less boring than earlier.

Whats more is that once a WP CLI downloads a copy of WordPress, it caches this download and serves further download requests from this cached copy. Moreover, the WP CLI also checks this cache periodically for newer versions. This ensures that users get optimum experience while using WP CLI.

So far, so good. But recently I started encountering a pretty annoying issue while using WP CLI at my localhost setup. When ever I used to run a CLI command, it gave following error for no obvious reasons

Error: `No such file or directory`
Error establishing a database connection
This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `localhost`. This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?

If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums. `No such file or directory` 

So, I did what I usually do, copy the error message, prefix with WP CLI and hit a google search. Majority of the search results were related to incorrect entries in wp-config.php like wrong database credentials. I double checked my config file but to no avail. Luckily after couple of hit and trials, I was able to get the CLI working again

TLDR:

This issue can sometimes occur due to the value to DB_HOST defined in wp-config.php. To fix this just change the value of DB_HOST from localhost to 127.0.0.1 and voila, WP CLI is working again.