WP CLI Error: Error establishing a database connection.

Table of contents

Appearance: Issue commands via WP CLI, but WordPress in the browser works. This error often occurs when the WP CL interpreter cannot correctly interpret the database connection in wp-config.php.


In 99% of all cases, change the value of the constant DB_HOST from "localhost" to "".

Background: "localhost" is not a fixed IP address. The system now has several possibilities for interpretation. In some cases, "localhost" is interpreted as an alias for a socket connection instead of the TCP network protocol. The socket connection does not work via the command line.