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

12 thoughts on “WP-CLI Error Establishing a Database Connection with MAMP”

  1. I followed the install instructions at wp-cli.org and am unable to connect to database. I am using a newly installed (this morning) version of MAMP PRO.

    “`which php
    /usr/bin/php

    echo $WP_CLI_PHP
    /Applications/MAMP/bin/php/php5.5.14/bin/php

    echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/Applications/MAMP/bin/php/php5.5.14/bin/php

    wp –info
    PHP binary:/usr/bin/php
    PHP version:5.5.14
    php.ini used:
    WP-CLI root dir:phar://wp-cli.phar
    WP-CLI packages dir:
    WP-CLI global config:
    WP-CLI project config:
    WP-CLI version: 0.24.1“`

    Then, I followed a few other tutorials/instructions:

    `sudo -s`
    `curl http://wp-cli.org/packages/phar/wp-cli.phar > /usr/share/wp-cli.phar`
    `echo ‘/Applications/MAMP/bin/php/php5.3.14/bin/php /usr/share/wp-cli.phar $@’ > /usr/bin/wp`
    `chmod +x /usr/bin/wp`

    and I am still getting `Error: Error establishing a database connection`

    Any ideas?

    1. Lauren Pittenger

      Hi, did you get this figured out? A few ideas:

      What is in your bash profile? See if you’re adding the path to MAMP’s version of PHP to your $PATH. It seems that you are, but just to be sure…

      Secondly, for some reason, I’m starting to have problems with my setup again. I’m having to source my bash profile for WP-CLI to change versions of PHP. Try running source ~/.bash_profile and then see if you’re still getting the database error.

  2. Unfortunately these steps didn’t help me. On my Mac running macOS High Sierra I had to:
    1. Edit my “.profile” file instead of “.bash_profile” for some reason.

    2. In my “.profile” file I added the line below:
    export PATH=/Applications/MAMP/bin/php/php7.2.1/bin:$PATH

    3. Then I typed in line below in my Terminal to reload Terminal to ensure the command worked:
    source ~/.profile

    I hope this helps someone. And Lauren keep up the good work!

Leave a Comment

Your email address will not be published.

GOT A PROJECT? LET’S TALK!

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

Scroll to Top
%d bloggers like this: