title: What is killing my PHP process, and leaving so many sleeping mysql connections?
body: <p>I'm having trouble investigating an issue with many sleeping MySQL connections. </p>
  
  <blockquote>
    <p>Once every one or two days I notice that all (151) MySQL connections
    are taken, and all of them seem to be sleeping.</p>
  </blockquote>
  
  <p>I investigated this, and one of the most reasonable explanations is that the PHP script was just killed, leaving a MySQL connection behind. We log visits at the beginning of the request, and update that log when the request finishes, so we can tell that indeed some requests do start, but don't finish, which indicates that the script was indeed killed somehow. </p>
  
  <p>Now, the worrying thing is, <strong>that this only happens for 1 specific user</strong>, and only on 1 specific page. The page works for everyone else, and when I log in as this user on the Production environment, and perform the exact same action, everything works fine. </p>
  
  <p>Now, I have two questions:</p>
  
  <ol>
  <li><p><strong>I'd like to find out why the PHP script is killed</strong>. Could this possibly have anything to do with the client? Can a client do 'something' to end the request and kill the php script? If so, why don't I see any evidence of that in the Apache logs? Or maybe I don't know what to look for? How do I find out if the script was indeed killed or what caused it?</p></li>
  <li><p><strong>how do I prevent this?</strong> Can I somehow set a limit the to number of mysql connections per PHP session? Or can I somehow detect long-running and sleeping mysql connections and kill them? It isn't an option for me to set the connection-timeout to a shorter time, because there are processes which run considerably longer, and the 151 connetions are used up in less than 2 minutes. Also increasing the number of connections is no solution. So, basically.. how do I kill processes which are sleeping for more than say 1 minute? </p></li>
  </ol>
  
  <p>Best solution would be that I find out why the request of 1 user can eat up all the database connections and basically bring down the whole application. And how to prevent this. </p>
  
  <p>Any help greatly appreciated. </p>