I am running the most recent version of WP-CLI on my mac. I do all WordPress development within Vagrant, but would like to run WP-CLI commands in the host machine. I have a domain alias from “dev.mysite.com” to the IP of my server so it can be accessed through my web browser through an HTTP request.
When trying to run a WP-CLI command, I get an “Error Establishing a Databse Connection” error mesage because it is trying to connect via “localhost” when it really should be “dev.mystie.com” or the Vagrant VM’s IP Address. Is there any way I can access my site through WP-CLI without having to SSH into the guest?
Since the issue is probably DB configuration you could try adjusting it conditionally in
wp-config.php
:Have you tried updating wp-config.php to change the database host?
Alternatively why not use command + t in terminal to open a new tab then vagrant ssh in there and use that for WP-CLI?
You could take a look at wp-cli-ssh
I know you don’t want to SSH to the guest, but this at least, supposedly, removes the need to do it manually.
I figured out how to set it up.
Follow these steps
Create or edit
config.yml
in the same folder and add:Install the package with:
php composer.phar require x-team/wp-cli-ssh=dev-master
Then just running
wp
should display a list of available subcommands, and you should havessh
added to that list.Then we configure the plugin in
wp-cli.local.yml
orwp-cli.yml
:And run it all with
wp ssh plugin status --host=vagrant
… It’s just to bad that this plugin assumes Varying-Vagrant-Vagrants. Since my vagrant environment hosts a lot of sites the Vagrantfile isn’t in the WordPress directory, breaking the whole thing…
I’ve been able to utilize @aliases to trigger WP-CLI commands on the
basic-wordpress-vagrant
box from my host machine. Maybe this could help you.