It's unusual to have connection issues on localhost. When this happens, check your computer's memory and processor usage. If they're too high, it might cause timeouts. Also, look at your MySQL settings, especially max_allowed_packet and wait_timeout. If they're too low, increase them. Did you previously change any settings?
Poorly written queries can also cause connection issues. You can perform a Java Flight Recording: https://maxcheaters.com/topic/248080-ubuntu-server-2204-low-latency-and-high-performance/
Recommended Posts