WP-CLI Cannot Connect to Database due to Vagrant

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?

Related posts

Leave a Reply

4 comments

  1. Since the issue is probably DB configuration you could try adjusting it conditionally in wp-config.php:

    if ( defined( 'WP_CLI' ) ) {
        define( 'DB_HOST', 'example.com' );
    }
    else {
        define( 'DB_HOST', 'localhost' );
    }
    
  2. 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?

  3. You could take a look at wp-cli-ssh

    Seamlessly run WP-CLI commands on a remote server via 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

    mkdir -p ~/.wp-cli
    cd ~/.wp-cli
    touch composer.json
    php composer.phar init --stability dev --no-interaction
    php composer.phar config bin-dir bin
    php composer.phar config vendor-dir vendor
    php composer.phar config repositories.wp-cli composer 'http://wp-cli.org/package-index/'
    

    Create or edit config.ymlin the same folder and add:

    require:
      - vendor/autoload.php
    

    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 have ssh added to that list.

    Then we configure the plugin in wp-cli.local.yml or wp-cli.yml:

    ssh:
      vagrant:
        # The %pseudotty% placeholder gets replaced with -t or -T depending on whether you're piping output
        # The %cmd% placeholder is replaced with the originally-invoked WP-CLI command
        cmd: vagrant ssh-config > /tmp/vagrant_ssh_config && ssh -q %pseudotty% -F /tmp/vagrant_ssh_config default %cmd%
        # Passed to WP-CLI on the remote server via --url
        url: local-www.yourawesomewebsite.com
        # We cd to this path on the remote server before running WP-CLI
        path: /your/vvv/path/
    

    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…

  4. 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.

    ~/.wp-cli/config.yml

    @basic:
        ssh: basic.dev/var/www/wordpress/
    

    ~/.ssh/config

    Host basic.dev
        HostName basic.dev
        User vagrant
        IdentityFile ~/sites/basic.dev/.vagrant/machines/default/virtualbox/private_key
    

    $ wp @basic theme list