Rewritten MySQL handling from scratch.