fbpx

WP-CLI Error Establishing a Database Connection with MAMP

Last weekend while at WordCamp NYC, I saw a presentation by Shawn Hooper  called WP-CLI – Save Time By Managing WordPress from the Command Line which covers a lot of great things that WP-CLI is capable of. I already had WP-CLI installed on my computer but I’ve had an issue getting it to work with my MAMP setup. After a bit of research, I found that there’s some additional setup required.

Here’s how I got it working.

  1. Follow the install & upgrade instructions at http://wp-cli.org/
  2. After running $ wp --info, I could see that WP-CLI was using a version of PHP that came with my computer instead of MAMP’s version.
    WP-CLI Using Wrong Version of PHP
  3. When I tried to run $ wp help from within a local install of WordPress, I got a message starting with the following:
    Error:
    <h1>Error establishing a database connection</h1>WP-CLI Error Establishing a Database Connection
  4. We need to change the version of PHP that WP-CLI uses. To do that, we need to edit our bash profile. The file can be opened with the following command:
    $ open ~/.bash_profile
  5. Add the following to your bash profile to switch the version of PHP to the one MAMP uses. Make sure to check the PHP version number that you’re using with MAMP, which can be done by looking in MAMP’s preferences. If you’re not using 5.5.10, you’ll need to change that in the following lines.
    ##
    # WP-CLI / MAMP compat
    ##
    export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.10/bin
    export PATH="$MAMP_PHP:$PATH:/Applications/MAMP/Library/bin"
  6. Run the following command or close terminal and reopen it
    $ source ~/.bash_profile
  7. Check all the things to make sure we’re using the right PHP
    $ wp --info should return something like the following:
    Screen Shot 2015-11-08 at 7.06.24 PM
    Also try $ echo $PATH. You should see the path to MAMP’s version of PHP.
    Screen Shot 2015-11-08 at 7.07.56 PM
    Also try $ which php, which should also return the path to MAMP’s version of PHP.
    Screen Shot 2015-11-08 at 7.09.41 PM
  8. If you’re still getting an error about connecting to the database, make sure MAMP is running and run the command $ source ~/.bash_profile

Thanks Rami, this postthis post, this stackoverflow post, and this video

GOT A PROJECT? LET’S TALK!

Are you ready to get your work out to the world?

Scroll to Top
%d bloggers like this: