modify db.py and rest.py to fix MySQL has gone away issue