MySql Proccesslist filled with “Sleep” Entries leading to “Too many Connections”?

后端 未结 6 1406
礼貌的吻别
礼貌的吻别 2020-12-12 12:41

I\'d like to ask your help on a longstanding issue with php/mysql connections.

Every time I execute a \"SHOW PROCESSLIST\" command it shows me about 400 idle (Status

相关标签:
6条回答
  • 2020-12-12 13:15

    Basically, you get connections in the Sleep state when :

    • a PHP script connects to MySQL
    • some queries are executed
    • then, the PHP script does some stuff that takes time
      • without disconnecting from the DB
    • and, finally, the PHP script ends
      • which means it disconnects from the MySQL server

    So, you generally end up with many processes in a Sleep state when you have a lot of PHP processes that stay connected, without actually doing anything on the database-side.

    A basic idea, so : make sure you don't have PHP processes that run for too long -- or force them to disconnect as soon as they don't need to access the database anymore.


    Another thing, that I often see when there is some load on the server :

    • There are more and more requests coming to Apache
      • which means many pages to generate
    • Each PHP script, in order to generate a page, connects to the DB and does some queries
    • These queries take more and more time, as the load on the DB server increases
    • Which means more processes keep stacking up

    A solution that can help is to reduce the time your queries take -- optimizing the longest ones.

    0 讨论(0)
  • 2020-12-12 13:16

    Before increasing the max_connections variable, you have to check how many non-interactive connection you have by running show processlist command.

    If you have many sleep connection, you have to decrease the value of the "wait_timeout" variable to close non-interactive connection after waiting some times.

    • To show the wait_timeout value:

    SHOW SESSION VARIABLES LIKE 'wait_timeout';

    +---------------+-------+

    | Variable_name | Value |

    +---------------+-------+

    | wait_timeout | 28800 |

    +---------------+-------+

    the value is in second, it means that non-interactive connection still up to 8 hours.

    • To change the value of "wait_timeout" variable:

    SET session wait_timeout=600; Query OK, 0 rows affected (0.00 sec)

    After 10 minutes if the sleep connection still sleeping the mysql or MariaDB drop that connection.

    0 讨论(0)
  • 2020-12-12 13:16

    So I was running 300 PHP processes simulatenously and was getting a rate of between 60 - 90 per second (my process involves 3x queries). I upped it to 400 and this fell to about 40-50 per second. I dropped it to 200 and am back to between 60 and 90!

    So my advice to anyone with this problem is experiment with running less than more and see if it improves. There will be less memory and CPU being used so the processes that do run will have greater ability and the speed may improve.

    0 讨论(0)
  • 2020-12-12 13:17

    The above solutions like run a query

    SET session wait_timeout=600;
    

    Will only work until mysql is restarted. For a persistant solution, edit mysql.conf and add after [mysqld]:

    wait_timeout=300
    interactive_timeout = 300
    

    Where 300 is the number of seconds you want.

    0 讨论(0)
  • 2020-12-12 13:22

    Alright so after trying every solution out there to solve this exact issues on a wordpress blog, I might have done something either really stupid or genius... With no idea why there's an increase in Mysql connections, I used the php script below in my header to kill all sleeping processes..

    So every visitor to my site helps in killing the sleeping processes..

    <?php
    $result = mysql_query("SHOW processlist");
    while ($myrow = mysql_fetch_assoc($result)) {
    if ($myrow['Command'] == "Sleep") {
    mysql_query("KILL {$myrow['Id']}");}
    }
    ?>
    
    0 讨论(0)
  • 2020-12-12 13:26

    Increasing number of max-connections will not solve the problem.

    We were experiencing the same situation on our servers. This is what happens

    User open a page/view, that connect to the database, query the database, still query(queries) were not finished and user leave the page or move to some other page. So the connection that was open, will remains open, and keep increasing number of connections, if there are more users connecting with the db and doing something similar.

    You can set interactive_timeout MySQL, bydefault it is 28800 (8hours) to 1 hour

    SET interactive_timeout=3600
    
    0 讨论(0)
提交回复
热议问题