DB insertion PHP script freezing randomly, no error log

♀尐吖头ヾ 提交于 2020-04-30 06:28:41

问题


I have a script that I run as a cron job every X minutes.

It is being run on VPS using Apache, Centos, Maria DB 10.2, 6 Cores and 8GB of RAM.

I applied a mechanism to prevent the script from running again if the current run didn't finish yet.

It seems to run fine about 99% of the time.

However, in a random manner, sometimes the script would just "freeze". The log file that the script is creating will either stop at one point before the EOE, or a 0 bytes log file will be created without getting populated. There are no error logs and no MySQL errors logged.

Today I enabled mysqlbinlog log, and saw that at the exact second in which the progress log of the script has halted, the script was making about ~290 Inserts into two DB tables, one table has ~40 columns and another have ~85 columns.

But this is normal behavior for the scripts.

I have consulted few developers and none of them had a clear idea of what can be happening. I was simply suggested that maybe the RAM is not enough for the script. However, I don't know if this makes sense since most of them time the script does finish successfully, and it doesn't report a memory problem, it simply freezes.

Can you suggest a way to debug this?


回答1:


Use bulk insert; that will be about 10 times as fast, thereby at least decreasing the frequency of conflict.

INSERT INTO tbl (a,b) VALUES
    (1,2),
    (33,44),
    ...

Do you check for errors? SHOW ENGINE INNODB STATUS;. An un-detected deadlock could lead to missing data, but not a hang.

A hang could come from an action blocking the cron job. This might time out after 50 seconds; did you wait that long? Be sure to check for errors and log them.

0.5G -- what if several cron jobs pile up?? Is PHP running on the same machine as MySQL? Could they be fighting for RAM?

(I suspect your anti-collision code has a bug.)



来源:https://stackoverflow.com/questions/61165196/db-insertion-php-script-freezing-randomly-no-error-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!