Closing Db Connections

My website has not that many visits (let’s say that at best I have 100 users connected at the same time) and when I watch the stats on my mysql server I can see that I have up to 70 simultaneous connections to the mysql server!

I think that the problem come from Yii that is not closing the connections, so they stay idle until the wait_timout value is reach and then mysql closes theses connections.

How can I force Yii to close the db connection when there are no more requests to perform?

it closes connections by default as it’s PDO

that’s on MySQL side to decide when to close temporary connections. what’s the status of command? sleep? if sleep then don’t worry or configure your MySQL to kill connections faster

on other hand,

If you want only 1 persistent connection to db and agree with all problem you may have to gain (decent) performance on connection bubbling, just add to config in db section

'db' => [

   'persistent' => true




but with 100 users you will not notice performance from persistent connection, only troubles

Hmm. Usually it’s GC job to free the resources. Maybe you have some kind of connection pooling?

Thank you both for your answers,

I don’t think I have any connection pooling and yes you are right, php gc should free the resources!

Right now i’m trying to see the running processes using

show processlist

But everything seems to be fine (no sleeping threads are present). So I really don’t understand how I could reach this many simultaneous connections!

What’s your server config? (apache/mod_php, php-fpm, something else?..)

How many webserver processes/threads are currently active?

Are there any cronjob tasks?

This is an apache webserver, I’m using PHP-FPM. No crons are defined during the day, and I only have two small ones at night.

But since it’s not a Yii problem I think I’ll try to find where the bottleneck comes from, but for that I need to wait that a lot of people are connected (in general between 6 and 10 pm)

Thanks again for your answers!

Quick question:

If I am constantly calling


in my code, in the same function or across several functions, will I be using the same connection or is it creating new connections everytime?

The same.

Creating new connections is very expensive.