I provided solution to similar problems with the one I met recently:
I have Telegram (messenger) bot with MySQL user DB. There is connection to it in start of code causing connection was going away later about 10 hours. Therefore bot was returning errors because couldn`t get information about user.
I use mysql-connector-python
framework. Solution is to use class for DB queries. You can see it and examples of using below.
The class:
Table example:
Table example
Query Structure:
Examples of using:
If you have some optimisation offers, write its!