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.

0

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

| improve this answer | |

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.