Have you ever got a timeout while trying to connect to a local mysql server from php code? Have you teared your nails trying to figure out cause and solution?
I unfortunately spent 3 hours trying to solve the issue.
Solution is again simple and absurd. The workaround is whether you hardcode the connection or use a framework such as Symfony, the same. If you write server url as "localhost" on some conditions the local server timeouts. The issue does not seem very well documented. Nevertheless, the solution is to make server "127.0.0.1" instead of "localhost".
Happy connections, healthy nails!