WP CLI is a fantastic command-line tool for working with WordPress. You can set up new installs, install plugins, and update settings all from your terminal window. However, for MAMP users, it won’t work out of the box, so this is how you fix it:
Error Establishing A Database Connection
MAMP comes packaged with its own versions of MySQL & PHP, which we need to give WP CLI access to. Your default path to PHP is likely to be /usr/bin/php
, or /usr/local/bin/php
which is not those used by MAMP. You can find out what PHP you’re using by running the command which php
in your terminal window.
The exact path to MAMP’s PHP install varies depending on the version, but it will be something similar to /Applications/MAMP/bin/php/php5.5.14/bin
. You can replace the version number with your MAMP’s PHP version.
You’ll need to update the WP_CLI_PHP environment variable by adding the following code to your ~/.bash_profile or ~/.bashrc file (whichever you prefer). In the terminal, type:
nano ~/.bash_profile
Within this file, add the path to MAMP’s PHP. In my example, it would be like this:
export PATH="/Applications/MAMP/bin/php/php5.5.14/bin:$PATH"
Press CTRL + O
then CTRL + X
to save and exit. You’ll then need to either source .bash_profile
or re-open terminal.
Can’t Connect To Local MySQL Server Through Socket
You must have MAMP running for WP CLI to connect to the MySQL server. If you have MAMP running and you’re still receiving the “Can’t Connect” error, then you can add MAMP’s MySQL to your system $PATH
. This is almost the same process as above, so re-open the ~/.bash_profile
file in the terminal and type:
nano ~/.bash_profile
Within this file, add the following:
export PATH=$PATH:/Applications/MAMP/Library/bin
Press CTRL + O
then CTRL + X
to save and exit. You’ll then need to either source .bash_profile
reopen it or reopen it like before.
Vagrant
These are the two main errors when working with MAMP & WP CLI, but depending on your version of MAMP and your version of macOS, it’s possible that you will encounter other errors. For this reason (and many more), it’s worth investing the time to set up your development environments using VVV (a Vagrant configuration focused on WordPress development). Installing VVV is out of scope for this article, but you can find everything you need to get it up and running on their Github repo.