0

I'm running macOS Catalina and am trying to use WP CLI with MAMP, with iTerm2 and Oh My Zsh as my command line.

However in my localhost directory when I enter wp in the command line, and then press q to exit, I get the following errors:

PHP Notice:  Undefined index: SERVER_NAME in phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1197) : eval()'d code on line 5  
Notice: Undefined index: SERVER_NAME in phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1197) : eval()'d code on line 5
PHP Warning:  mysqli_real_connect(): (HY000/2002): No such file or directory in /Users/mattpjennings/Desktop/projects/mj/build/mj.net/wp-includes/wp-db.php on line 1626
Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /Users/mattpjennings/Desktop/projects/mj/build/mj.net/wp-includes/wp-db.php on line 1626

Then when I enter wp plugin list I also get the error below:

Error: Error establishing a database connection.

When I enter wp --info I get:

PHP binary: /usr/local/Cellar/php/7.4.6/bin/php
php.ini used:   /usr/local/etc/php/7.4/php.ini

I already tried adding the line below to ~/.oh-my-zsh/custom/aliases.zsh to change the php or php.ini path when I enter wp --info:

#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

I also tried changing the line below on the wp-config.php file, which didn't work:

define('DB_HOST', '127.0.0.1:8889');

Any help would be appreciated. I really would like to use WP-CLI locally on my Mac.

1 Answer 1

1

Generally, MAMP is not installed in a place that wp-cli expect it to be installed. And the MySQL socket file is needed to by the application.

Creating a symbolic link would help.

  1. Locate the socket file location either in tmp or var

    netstat -a | grep mysql
    

    In the case of MAC Sierra and MAMP PRO, you have /Applications/MAMP/tmp/mysql/mysql.sock

  2. Create a symbolic link to this file

    cd /var 
    sudo mkdir mysql
    sudo chmod 755 mysql
    cd mysql
    sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
    

If this did not work you can also add a symbolic to tmp folder.

cd tmp
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock

This is a tweak to the answer by @Brain Lowe here

5
  • I just used Local from Flywheel (localwp.com) instead where I can easily use the WP CLI. Commented Sep 23, 2020 at 16:29
  • My wp command complained about there being no /tmp/mysql.sock, and the second symbolic link in /tmp worked perfectly, thanks! Commented Jan 5, 2022 at 17:14
  • @risingPhoenix1979 I used localwp and it works really well so far. You can also use the addon for xdebug. That is sweet!
    – pensebien
    Commented Feb 13, 2022 at 13:42
  • Can anyone confirm that the answer from @pensebien works on MAMP? I've given up using MAMP with the WP-CLI and currently use Local (localwp.com) as the WP-CLI works on my Mac out of the box with no configuration. Commented Mar 11, 2022 at 23:24
  • 1
    @risingPhoenix1979 I did that and it worked. LocalWP does not work well with MAMP if you have the both installed on your system.
    – pensebien
    Commented Jul 2, 2022 at 21:44

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.