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

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

14
  • 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
    Commented Aug 9, 2016 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. Commented Aug 9, 2016 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. Commented Aug 9, 2016 at 23:05
  • Does the database exist? Are the DB credentials correct in your wp-config.php file?
    – Pat J
    Commented Aug 9, 2016 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. Commented Aug 9, 2016 at 23:18

3 Answers 3

5

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
1
  • Then, to load in the new PATH config, run: source ~/.zshrc !
    – Cush
    Commented Apr 11, 2020 at 16:36
4

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

define('DB_HOST', '127.0.0.1');

instead of:

define('DB_HOST', 'localhost');

2
  • This should be marked as the accepted answer. Thank you! Commented Sep 15, 2020 at 13:50
  • This also worked for me, the other answers did not
    – haveboard
    Commented Jun 3, 2021 at 16:57
2

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
  • 1
    Also don't forget to refresh your bash profile when finished with this command: source ~/.bash_profile
    – rhysclay
    Commented Nov 29, 2016 at 4:52

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.