Move a bunch of MySQL related logic to separate module.