Db_host configuration

Hi there.

After installing WP-CLI, running any commands was met with Error: Error establishing a database connection.

In wp-config.php I changed define( 'DB_HOST', 'devkinsta_db' ); to define( 'DB_HOST', '127.0.0.1:<PORT NUMBER>' );. This fixed the problem with WP-CLI and I was able to successfully run commands, however, it then broke the DB connection when I try to access content through the WP Dashboard.

What is the correct configuration to permit both WP-CLI and regular database functionality to both work?

Many thanks for your help.

Hi @ms-bris, thanks for reaching out! Can you detail how you installed WP-CLI and where? I will try to reproduce your process/issue. It sounds like a host routing issue.

By the way, the devkinsta_fpm container already has WP-CLI installed, although you would have to run it as root form there which may not be ideal.

Thanks @Kevin.

We’re developing using the Sage 10 starter theme with a WordPress instance spun up with DevKinsta. There is some reference in the Sage 10 documentation to using WP-CLI and we thought we would try and integrate it’s use on this project and WP projects more generally.

I’m in an OS X environment with PHP 8.0 and WordPress 6.0. I followed the install steps on wp-cli.org for a global install, specifically using the following commands to complete the install -

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp cli update

I’m not sure if it’s relevant but there is some dev URL config in theme’s bud.config.js file

Thanks again Kevin and let me know if you require any further info.

So you have it installed globally right in Terminal? I’ve looked it over again and I think the only convenient way will be fore you to run the command in the FPM container. So from terminal run this command:

docker exec -u www-data -it devkinsta_fpm bash

Then you just cd to your desited site’s home directory and test any wp-cli command. It should route the db_host correctly from within the container if you leave it as “devkinsta_db.”

Thanks Kevin.

That’s correct - it was globally installed in Terminal.

I ran that command from terminal and cd’d in the home directory. I ran the same WP command I used when I had set the DB_host as DB_HOST', '127.0.0.1:<PORT NUMBER> and successfully ran the command earlier. What I got back this time was -

PHP Fatal error:  Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.2". You are running 7.4.29. in /www/kinsta/public/<theme-name>/wp-content/plugins/acorn/vendor/composer/platform_check.php on line 24
Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.2". You are running 7.4.29. in /www/kinsta/public/<theme-name>/wp-content/plugins/acorn/vendor/composer/platform_check.php on line 24

DB_HOST has been left as devkinsta_db. A check of PHP versions says I’m running 8.0.18.

Any thoughts?

Thanks again for your help.

That’s odd. Can you check the PHP version with php -v ?
Is that what’s returning 8.0.18? I believe that container uses php 7.4.29 by default. Can you share the command that you are trying to run? Feel free to public message it to me as well if it has any private information.

And just to confirm, does a basic wp-cli command also return that error? wp help

Thanks @Kevin. Making sense now I think.

  • The container instance is running PHP 7.4.29;
  • The DevKinsta instance install was with PHP 8.0;
  • The command used is wp acorn make:composer ExampleComposer;
  • The command wp help returns the same error;

The Acorn install I used was to run on PHP version 8.0 (from here).

Okay thanks for that @ms-bris
This is an interesting puzzle. I’m going to try to try to reproduce your setup to see what’s possible with DevKinsta as-is. I know there’s a solution somewhere. Usually wp cli works by default in that container. I’m not sure why it isn’t working for you/why you have a php version requirement.

I’m going to do some testing tomorrow and consult with our Devs then get back to you!

Above and beyond @Kevin - thankyou!

I think I’ve figured this out @ms-bris. So this is going back to your original issue with installing wp-cli globally and getting the database connection error:

  1. In terminal, use the command hostname -I to get your IP address.
  2. In wp-config.php, set your DB_HOST to ipaddress:dbport (123.1.2.3:15101)

The main idea is to not use localhost, 127.0.0.1 or devkinsta_nginx as the db_host. The first two won’t work because of the loopback in /etc/hosts on the Docker containers and devkinsta_nginx will only get routed correctly as a host when used within the Docker containers (that’s why it wasn’t working with global wp-cli).

Your machine’s IP address/port is the most direct way to connect to the database and won’t run into any hosts file conflicts. Please let me know if this doesn’t work for you, though. I was only able to test with Windows/Linux but the same idea should apply to Mac.

@Kevin it looks to be working! How wonderful.

I tripped my self up because I was using my external IP. Once the internal IP was used it worked. Also, hostname -I doesn’t work for some reason (might be a Mac thing) but there are other commands / avenues to get the details.

Thanks for your patience and direction! Very much appreciated.

1 Like