Killing long running process in MYSQL

Discussion in 'MySQL' started by realinfo, Jul 19, 2013.

  1. #1
    Sometimes due to more traffic mysql got hang, when i check it thereare to many tables running, open and locked, to slove this issue i want to try this code

    <?php
    
    $con = mysql_connect( "localhost", "user", "pass" ) or die( "can not connect" );
    if( $con ) echo "Connected<br>";
    
    $result = mysql_query( "SHOW FULL PROCESSLIST", $con );
    while( $row = mysql_fetch_array( $result, $con ) )
    {
    
        $process_id = $row["id"];
        if ($row["Time"] > 100 )
        {
            $sql = "KILL $process_id";
            $res = mysql_query( "$sql", $con );
            if( $res )
            {
                echo "Mysql Process ID $process_id has been killed<br>";
            }
        }
        else echo "Row not found?<br>";
    }
    ?>
    
    Code (markup):
    I tried this code for cron job to kill slow query

    but when i run this cron.php then i got this error

    Connected

    Warning: mysql_fetch_array() expects parameter 2 to be long, resource given in /home/r/public_html/da/cron.php on line 7

    Help me

    I have SMF 2.0.4 default theme
     
    realinfo, Jul 19, 2013 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Means the query is failing.

    Try replacing:

    $result = mysql_query( "SHOW FULL PROCESSLIST", $con );

    with:

    if(!$result = mysql_query( "SHOW FULL PROCESSLIST", $con )) {
    die(mysql_error());
    }

    May just be a permission problem.
     
    jestep, Jul 25, 2013 IP
  3. realinfo

    realinfo Greenhorn

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Connected

    Warning: mysql_fetch_array() expects parameter 2 to be long, resource given in /home/r/public_html/da/cron.php on line 9

    Still same error
     
    realinfo, Jul 25, 2013 IP