I am new to wordpress but not to programming to please bear with me. I am trying to get a new wordpress project up and running locally using the wp-cli.

But when I run the following command:

wp core install --url=example.com --title=WP-EXAMPLE --admin_user=******* --admin_password=****** --admin_email=******

I get the following error:

Error: 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.

I was able to run the wp config create and wp db create commands without issue and a database was create so I know that the database username and password are correct. Is there a step I am missing?

The output of wp --info is as follows:

OS: Darwin 17.6.0 Darwin Kernel Version 17.6.0: Tue May  8 15:22:16 PDT 2018; root:xnu-4570.61.1~1/RELEASE_X86_64 x86_64
Shell:  /bin/zsh
PHP binary: /usr/bin/php
PHP version:    7.1.16
php.ini used:
WP-CLI root dir:    phar://wp-cli.phar
WP-CLI vendor dir:  phar://wp-cli.phar/vendor
WP_CLI phar path:   /Users/admin/code/wp-example
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.5.1

Since there are so many blank items I feel as though I am missing something although I have following all of the steps that I have found in quick start guides for setting up wordpress from the CLI.

Thanks in advance.

1 Answer 1


First ensure the normal browser install works. If not, here's a really good article on that: How to Fix the Error Establishing a Database Connection in WordPress.

Next thing to note is that your database may not be hosted on localhost. Then try to create the wp-config.php using or (for MAMP for example) or something similar. On an existing installation this can be fixed by replacing the DB_HOST with define('DB_HOST', ''); in your wp-config.php.

For an existing site you could also try it with define('WP_ALLOW_REPAIR', true); in your wp-config.php and then access the site in a browser to let WordPress fix some common database issues for you.

The article also suggest to create a raw testconnection.php file in your WordPress root (replaced the database credentials with yours) and call that in a browser http://example.com/testconnection.php to see what will be returned:

$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
  die('Could not connect: ' . mysqli_error());
echo 'Connected successfully';
  • 1
    Only setting did the trick for me. Thank you Commented Apr 30, 2019 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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