HA keeps loosing connection to external database- needs a restart

Hi everyone

I have started using external MySQL database for recorder for a few months and I really like as it keeps my HA responsive and my backup very small.

However for the last month or so, every two or 3 days HA starts to fail to write to the database. The way to fix this is to reboot HA. Another symptom when this happens is the RPI Ram used values started to go up ( I have a rule set up now that notifies me when memory exceeds 550mb and when it reaches 600 mb executes a restart of HA. Under normal circumstances when the external data base connection is working it seems stays below 500mb)

Any one have any idea why this would be happening?

It seems the issue is triggered at ~4.12 am which is around when “cannot delete or update a parent row: foreign key constraint fails” and “error purging history” errors get logged.

HA logs related to external database errors attached