Using version control (Git) on a MySQL database

You can backup your database within a git repository. Of course, if you place the data into git in a binary form, you will lose all of git’s ability to efficiently store the data using diffs (changes). So the number one best practice is this: store the data in a text serialised format.

mysqldump is a suitable program to help you do this. It isn’t perfect though. If anything disturbs the serialisation order of items (eg. as a result of creating new tables, etc.) then artificial breaks will enter into the diff. That will decrease the efficiency of storage. You could write a custom serialiser to serialise changes only — but then you are doing the hard work that git is already good at. Just use the sql dump.

That being said, what you are wanting to do isn’t what devs normally mean when they talk about putting the database in git. For instance, if you read the link posted by @eggyal (link to codinghorror) you will see that what is actually placed in git are the scripts needed to generate the initial database. There may be additional scripts, like those to populate the database data with a clean state, or to populate it with testing data. All such sql scripts are text files, and pretty much the same format as the sql dump you would get from mysqldump. So there’s no reason you can’t do it that way with your day-to-day data as well.

Leave a Comment