You seem to have problems with MySQL; and your library sometimes becomes unavailable. This should be resolved first if possible.
Does it only freeze on Library Scans? Can you reliably reproduce this with a scan every time?
If there’s a bad file, it can sometimes cause Kodi to freeze. The solution is to split the library in to two directories and re-scan. If that succeeds, split the folder with the bad file again in to two folders until you eventually find the problematic file.