I get this error when I try to create something with WP CLI:

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.

But I can open the site with the link: http://localhost:8888/projectname

Any idea?

Go into your wp-config.php and change your DB_HOST to 127.0.0.1 instead of localhost.

Credit goes to Craig Wayne above in the comments.

  • God bless You & Craig Wayne – Mirko Oct 10 at 11:20
  • In MAMP Pro I also had to check "Allow network access to MySQL" for this to work. – Nick M Oct 22 at 6:16
  • I tried this but no help. I'm using Bedrock boilerplate. I went thru wp-config and .env file over and over again, went thru MAMPs configs over and over again. Debugged the wb-db.php file. Then I removed the # from the DB_HOST row in .env, and all is good. – Tom Nov 20 at 9:45

Make sure to use the MAMP PHP binary. You can check the which PHP Version WP CLI is running with

php wp-cli.phar --info

To use the latest MAMP PHP you need to modify your bash or zsh profile:

PHP_VERSION=$(ls /Applications/MAMP/bin/php/ | sort -n | tail -1)
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH

Make sure to reload the profile:

source ~/.bash_profile

Make sure that the changes are applied correctly:

  php wp-cli.phar --info
  • That first command just gives the error Could not open input file: wp-cli.phar – Felix Eve Sep 18 at 8:19
  • Did you run the command in the wordpress root folder? – eknows Sep 18 at 10:16
  • yes, but there is no wp-cli.phar file in the root of the WP directory. – Felix Eve Sep 18 at 10:58
  • Here you can find more informations how to install the wp-cli.phar: (wp-cli.org) – eknows Sep 20 at 5:53

Step1: check if your mysql server is running Step2: if yes then you can log in to mysql using

mysql -u root -p

then enter your password: ( you must use this command from terminal ) then use following command to ensure that database exists:

show databases;
grant all privileges on database_name.* to 'root'@'localhost' identified by 'password';
flush privileges;
exit;

now edit the wp-config.php file and search for

define('DB_NAME', 'db_name');
define('DB_USER', 'root');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');

Now restart the server and try to login to your wordpress dashboard. I hope it helps.

  • "mysql -u root -p" gives me "Access denied for user 'root'@'localhost' (using password: YES)" – Klevis Miho Jan 12 at 11:14
  • when you set up your mysql database, you must have entered a password. Use that password. If you didn't configure password at the time of mysql database set up, just hit enter instead of any password. – maverick Jan 12 at 11:29
  • I did what you wrote. Can connect to the database with "mysql -u root -p". Also the privileges are granted. But the error is still there. – Klevis Miho Jan 15 at 9:42
  • 1
    I've found that if i change localhost to 127.0.0.1 in my wp-config.php, everything works fine... i feel like theres a bigger problem here – Craig Wayne Jun 1 at 7:03
  • 1
    With a clean install of WordPress v3.6.9 and WP-CLI v1.5.1 @CraigWayne 's solution worked perfectly. – betweenbrain Jun 26 at 15:30

In my case, besides the aforementioned error message, I also got the warnings below:

PHP Warning:  mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
Warning: mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
PHP Warning:  mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
Warning: mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
PHP Deprecated:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/wp-includes/wp-db.php on line 1562
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/wp-includes/wp-db.php on line 1562
PHP Warning:  mysql_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
Warning: mysql_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
PHP Warning:  mysql_connect(): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
Warning: mysql_connect(): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562

According to this question, this issue happens because the default charset for MySQL 8.0 is utfmb4.

In fact, I could replicate the error by upgrading from MySQL 5.7 to MySQL 8.0

I solved the issue by exporting the database, downgrading to MySQL 5.7 and re-importing the data. I also ran tests on MariaDB 10.3 and it works fine.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.