wp db cli in the public folder doesn’t work without editing WP-config
Other wp db have similar problems but that doesn’t really matter for the problem itself.
If this is the expected behaviour, then of course this whole post can be dismissed.
Could also be I missed some mandatory setup or it’s because of my environment, if so, apologies.
What steps can be taken to replicate the issue?
Create new site (called site in this example)
PHP version 8.2.23/Nginx 1.26.1/MySQL 8.0.35 (The basic setup)
Open Site shell
Navigate to the /public/ folder
Run wp db cli
F:\dev\site\app\public>wp db cli
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)
If I edit the DB_HOST in public/wp-config.php to use the port that’s shown in the database tab
Thank you for spinning up this bug @jimihenrik! It looks like we’ve had some similar reports of this before and the Local team is taking a deeper look.
I ended up on this page since I am having the same issue. The work-around with the port in the wp-config.php still works but that will have to change again the next time I restart the site, I presume? So not really the best solution I suppose.
Is there a better approach for this? I like my Claude Code to do a bunch of queries via wp-cli, which otherwise works well, except for this port issue.
No no, the port only changes if you do something major like turn on Xdebug. I ended up using .env instead of actually changing wp-config, but I’m using a bedrock setup so that might not apply to you.
Anyway, the port does not constantly change so you can easily get going with that, even if it’s annoying to possibly keep an eye on it every now and then.
Well, I guess it would be easy to notice if stuff stops working, just remember to check the port at that point.
Hi Jimihenrik, thanks for your reply. your .env solution is interesting to me too, can you share what you add to the .env? And will that persist even with turning xdebug on/off etc?
Well there wasn’t really a reason for me to mention the .env as it’s all Bedrock. On that setup (WP+Composer) you just set all the environment variables in the .env file. See an example .env. On that I just haveit like DB_HOST='localhost:10053' with the site’s port for any given site. But if you’re not using Bedrock this doesn’t concern you.
Anyway, I haven’t seen the port change every except when I did turn the Xdebug on from the Local admin out of interest, and then it took me a long while to realize as of why everything was broken…
So even if it’s weird and annoying that the wp-cli didn’t work properly without setting the port, it hasn’t really been an issue for me. It’s not like you need to check that every time you boot your computer. Or Local. Or the site.
I ended up fixing my Powershell launch script to get the port from the sites.json file from LocalWP and adding it to the wp-config.php file every time, so it won’t even be an issue and wp-cli queries work fine too. Now Claude Code can use it too
WP-CLI db commands and Local by Flywheel port issues on Windows 11
When using wp db query (or any wp db command) with a Local by Flywheel site,
you might hit this error even though your site is running fine:
Error: Failed to get current SQL modes. Reason: ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)
Local assigns a random high port (like 10244) to each site’s MySQL instance.
The fix is not obvious: defining DB_PORT as a separate constant in wp-config.php
is silently ignored by WP-CLI’s db commands. The port must be embedded in DB_HOST
directly as localhost:PORT.
Local stores the live port in %APPDATA%\Local\sites.json under services.mysql.ports.MYSQL — more reliable than hardcoding since the port
can change when you restart Local or toggle Xdebug.
This PowerShell snippet reads the live port and patches wp-config.php automatically: