Let me briefly explain you my problem.
The architecture(for the problem) is as follow:
- 1 Ubuntu 16.04 with Apache 2.4 server; php-fpm 7.2 and Laravel
- 1 Ubuntu 16.04 with MariaDB v.15.2 distrib 10.2.14 server
The problem that started from couple of days is that web server is throwing this error:
SQLSTATE[HY000] [2002] Cannot assign requested address
What I've found so far:
On MariaDB
- ~30-40 concurrent connections
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n
- shows average ~800-900 connections in TIME_WAIT status
So far everything seems legit. But on Apache server the findings are as follow:
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n
- ~10-12k connections in TIME_WAIT status
When they get > 28k I receive the error mentioned above. That's because the server has ~28k free ports. (range 32768 - 61000). I've changed the range to 10000 - 63000 yesterday and I've changed waiting in TIME_WAIT status to 5 seconds,so far the error stopped. But this is not real solution of the problem.
Questions that I have in mind are:
Does php PDO start DB connection for every user and open TCP connection for every query to DB and reuse created DB connection? If this is true, is it possible to fix this without additional web server ?
Why and how there is such big difference between the connections in TIME_WAIT status on Apache and MariaDB ? Is it a PDO problem that is not closing connections properly ?
Thanks in advance.
P.S.: The connections to MariaDB are not persistent