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

echo $WP_CLI_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

When I run wp <anything> in the command line, I get Error: Error establishing a database connection

  • 1
    As MAMP Pro is a commercial, paid solution I would recommend reaching out to their support staff directly instead of posting questions here. – EAMann Aug 9 '16 at 21:52
  • 1
    MAMP Pro is working fine, It's just the WP-CLI that's a problem. I can get to the locally hosted site without issue. – Graham Lutz Aug 9 '16 at 21:55
  • 1
    "unable to connect to database" is pretty vague -- a description of "I did X, and Y happened" that included specific error messages may help someone troubleshoot your issue. – Alan Storm Aug 9 '16 at 23:05
  • Does the database exist? Are the DB credentials correct in your wp-config.php file? – Pat J Aug 9 '16 at 23:17
  • You're using the php that came with the system and not the MAMP supplied PHP. Add the MAMP PHP path to your bash profile, or similar for your preferred shell, and you should be ok. – Andy Macaulay-Brook Aug 9 '16 at 23:18

I noticed a typo in the original answer, but it did work for me in my .zshrc file. The typo was the end of the last line, it was missing the final / between the php version and bin directory

#MAMP Madness
export PATH=/Applications/MAMP/Library/bin:$PATH
PHP_VERSION=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH
  • Then, to load in the new PATH config, run: source ~/.zshrc ! – James Cushing Apr 11 '20 at 16:36

Alright - so here's what I figured out.

~/.bash_profile should look like this:

#MAMP Madness
export PATH=/Applications/MAMP/Library/bin:$PATH
PHP_VERSION=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}bin:$PATH
  • 1
    Also don't forget to refresh your bash profile when finished with this command: source ~/.bash_profile – rhysclay Nov 29 '16 at 4:52

I've found it seems to work a lot better if, in wp-config.php you use:

define('DB_HOST', '');

instead of:

define('DB_HOST', 'localhost');

  • This should be marked as the accepted answer. Thank you! – Luca Reghellin Sep 15 '20 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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