Getting WP CLI Working With MAMP
WP CLI is a fantastic command line tool for working with WordPress. You can setup new installs, install plugins, 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 it’s 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 MAMPs 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 MAMPs PHP version.
You’ll need to update the WP_CLI_PHP environment variable by add the following code to your ~/.bash_profile
or ~/.bashrc
file (whichever you prefer). In terminal, type:
nano ~/.bash_profile
Within this file, add the path to MAMPs 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 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
or re-open terminal like before.
Vagrant
These are the 2 main errors when working with MAMP & WP CLI, but depending on your version of MAMP and version of OSX, it’s possible you will encounter other errors. For this reason (and many more), it’s worth investing the time to setup 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.
Leave A Comment
Hey Christopher – thanks loads for this. I’m using MAMP Pro and getting this error – really don’t seem to be able to solve it. I’m terrible at my unix knowledge which doesn’t help, but any other ideas would be appreciated. The weird thing is I get the error using WPCLI but if I go to the WP folder in a browser it all works fine?
Thanks in advance
Mike
Hi Mike,
Which of the two errors are you seeing? You can check your $PATH variable by doing
echo $PATH
in terminal, you should be able to see your MAMP PHP path in this.You could also try to add the following to your
~/.bash_profile
instead, as this is another way to get WP CLI to recognise MAMPs version of PHP (switch the version if you need to):If you’re still struggling, you can shoot me a message on twitter. @crgeary
Hi !
Thanks for the Tips !
I’ve updated my PATH variable
but I still have an error (in french)
“Error: Erreur de connexion à la base de données (–>Can not connect to DDB)”
A really strange thing, because I can create the DDB throught Wp-Cli “wp db create”
Any idea ?
Thanks !
Sorry, not too sure. Here are a couple of things to keep in mind though:
1. Anytime you edit .bash_profile you will need to re-source the file using the
`source`
command. Or alternatively you can close terminal and re-open it.2. Is MAMP running? You get the same error if you try to run the
`wp`
command when MAMP isn’t running (because the mysql server isn’t running).3. You could try and run `wp cli info` which will tell you which PHP version wp cli is trying to use. You basically need to get this to be your MAMP version. I see you have tried to set it to
`/usr/bin/php`
which will not work as MAMP doesn’t touch this file at all. I’ve included what I get when i run this command at the end of the email.4. It’s possible that MAMP is not using version 5.5.10 of php (at least I cannot tell from my desk), just double check what version of PHP MAMP is using (you can do this from the MAMP Interface).
It’s hard to diagnose from this end, so if you cannot figure it out from the above, then i’m afraid I cannot help. It’s worth getting it working though, I use WP CLI to install & setup out base install of WordPress and it saves me 45 minutes each install. Here is a link if you’re interested.
Good luck with this 🙂
Here’s the result of `wp cli info` when I run it on my Mac 🙂
Hey Christopher,
I appreciate this post and I’m stoked to adapt your WP bash script for new installs. I downloaded WP-CLI and made the changes to the bash profile but something strange (and possibly unrelated) is happening. When I go to run “wp download core” in a new directory in the htdocs folder, it says “Downloading WordPress 4.4.1 (en_US)…” like you’d expect. But then it just hangs for 20 seconds or so and before going back to the terminal prompt for the directory. No files downloaded. Not even any errors. Any ideas?
Hi Justin,
I’ve not encountered this before, but sounds like it might be something to do with permissions in your directory, or perhaps there’s an issue with the download to your computer.. You can try running this again with debug mode and see what you get in the output:
Hey Christopher – hope you could help me.
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“`
Then, I followed your instructions above to no avail. Afterwards I follow the directions below from another source:
`sudo -s`
`curl wp-cli.org/packages/phar/wp-cli.phar > /usr/share/wp-cli.phar`
`echo ‘/Applications/MAMP/bin/php/php5.3.14/bin/php /usr/share/wp-cli.phar $@’ > /usr/bin/wp`
`chmod +x /usr/bin/wp`
and I am still getting `Error: Error establishing a database connection`
Hello,
For anyone reading this and still getting an error establishing database connection, this is what fixed it for me:
When appending MAMP’s php to your path, be sure to leave off the php directory. So from the example, it would look like this:
export PATH="/Applications/MAMP/bin/php/php5.5.14/bin:$PATH"
Thanks Gary,
I’ve updated the article to reflect this. This was a typo, thanks for spotting it 😉
Hi Christopher! Very helpful 👍 – thanks for sharing!
Worked a treat, thank you!
If someone have MySQL 8 it could be conflict between wp-cli and MySQL version