This is a PowerShell script ddev_forward_dynamic_mysql_host_port_port.ps1. This gets the dynamic port from the ddev describe json output and updates port forwarding using the netsh interface portproxy command.
Why do we want this? We can now connect to port 3306 on our host and reach the MySQL database in the container. The port generated by ddev and accessible by the host, on the other hand, is regenerated every time the container is restarted and changes randomly.
- get jq from https://stedolan.github.io/jq/download/ (it is needed to read the json output).
- put the ddev_forward_dynamic_mysql_host_port.ps1 script in your project folder.
- run the script as administrator.
This script uses the following open source components:
- jq is a lightweight and flexible command-line JSON processor. https://stedolan.github.io/jq
- https://woshub.com/port-forwarding-in-windows/