04.06.2021, Technical documentation & snippets

WP CLI Error: Error establishing a database connection.

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

Solution:

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

Background: "localhost" is not a fixed IP address. The system now has several options 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.

See also