Warning: A long semaphore wait

前端 未结 2 1058
说谎
说谎 2020-12-30 03:29

For the past 4 days I have had massive problems with my nightly updates, except for 1 night were it all went fine in between these 4 days.

During th

2条回答
  •  醉话见心
    2020-12-30 04:14

    Had such a problem too. The database broke without reason several times a day. I'm not sure if this helped me, but my solution is to optimize all tables. Three days so far, this problem no longer appears.

    There are many methods for optimizing all tables, but I'll give you an example of how to do this using PHP via the linux console

            #!/bin/php -n
            0;";
    $query="SHOW TABLE STATUS FROM $db_name";
    $tabbll=mysqli_query($con,$query);
    while ($row2 = mysqli_fetch_assoc($tabbll)) {
    $n++;
    $opt_table='`'.$db_name.'`.`'.$row2['Name'].'`';
    $query2="OPTIMIZE TABLE $opt_table";
    $time1 = time();
    mysqli_query($con,$query2);
    $time2 = time();
    $time3 = $time2-$time1;
    echo $n.' '.$time3.' '.$row2['Data_free'].' '.$opt_table.PHP_EOL;
    }}}
    mysqli_query($con,"SET GLOBAL innodb_buffer_pool_load_now = 1");
    mysqli_close($con);
    $timeend  = time();
    $time  = $timeend-$timestart;
    ?>
    

    Also part of the settings my.cnf

    innodb_thread_concurrency=0
    flush_time=0
    innodb_adaptive_hash_index=0
    innodb_adaptive_hash_index_parts=1
    innodb_purge_threads=1
    innodb_fatal_semaphore_wait_threshold=60
    

    UPDATE 17-07-2019

    I found the problem that caused me this error.

    The problem was that I had a table of 4,000 rows. This table received about 1000 updates every second. Also, from this table at the same time, there are about 500 selections every second. Usually, the time of selection is 0.006 seconds, but after several days the selection time becomes 5 seconds. After that, the moment that thousands of selects were gathered in the queue and there was a moment of error "A long semaphore wait".

    Possible solutions:

    1) Make another table structure, check the indixes, split the table into several tables.

    2 )Do every few hours optimize the table.

    3) Сome up with a caching system for this table

    Possible search problems:

    A useful script that will help you see what queries are gathered at the time of mysql crash. Run the script every minute through the cron.

    #!/bin/bash 
    USER=$( /media/bug/$(date +%Y%m%d%H%M%S)_$num.txt
    echo $num
    
    # Kill selections that can lead to "A long semaphore wait"
    # mysql --user=$USER --password=$PASSWORD -N -e "SELECT Id FROM information_schema.processlist where INFO like '%SELECT \`d_narfe\` FROM \`maitableep_com\`.\`5000_active\` WHERE%';" | while IFS= read -r loop
    # do
    #     echo "$loop"
    # mysqladmin --user=$USER --password=$PASSWORD  kill $loop
    # done 
    fi
    

提交回复
热议问题