Connecting to the wp-env MySQL database

The intent of this post is to be my own bookmark for how to quickly infer the connection info of your wp-env myqsl container, based on advice I got from Kira during the WordCamp Kansai contributor day when I realized I didn’t know how to do this 🙂

WordPress core development these days uses wp-env, a Docker-based tool that can be easily run from the root of the WordPress Develop repository with simple commands like npm run env:start and npm run env:stop. It’s a powerful tool for quickly spinning up a local development environment, but unfortunately I have found the documentation for wp-env can be confusing due to small differences in the commands you would run in the core project versus the block editor. (As an example, you run npm run env:start for Core, but npm run wp-env start in the block editor—they do the same thing, but at contributor days I’ve heard several people ask whether these are unrelated tools because of the slightly different naming.)

All that is basically to say that these instructions connecting to the DB when using wp-env can basically be used as-is for the npm run env:start command in Core:

  • Host: 127.0.0.1 (localhost)
  • Username: root (from WP Develop wp-config file)
  • Password: password (from WP Develop wp-config file)
  • Database: wordpress (from WP Develop wp-config file)
  • Port: {MYSQL_PORT_NUMBER}

The only tricky part is figuring out that MYSQL_PORT_NUMBER. At least on my machine, it doesn’t get displayed when turning on the Docker environment; and I’ve found that the number will change periodically while the site is running.

You can look at the WP environment’s MySQL container (which for me is git-mysql-1) in Docker to get the host URL, or you can use jq on the command line:

docker inspect git-mysql-1 | jq '.[].NetworkSettings.Ports."3306/tcp"[].HostPort'

If this outputs "49546", use that number as your port.

With this combined information, you can connect to your WP environment database with TablePlus, the mysql command line, or any other tool on your host machine.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.