UTF8 / Unicode in MySQL

For some reason, the current version of MySQL defaults to using the latin1 encoding. I needed to get Unicode support with UTF-8 encoding working a little while ago, so I might as well write down what to do. It’s pretty simple.

Go edit your my.cnf file (mine was located in /etc/mysql/my.cnf).

At the end of the [client] section, add this :

default-character-set = utf8

At the end of the [mysqld] section, add this:

character-set-server = utf8

And then restart MySQL — e.g. sudo /etc/init.d/mysql restart

After this, any NEW MySQL tables you create should automatically default to using UTF-8 in queries.

MySQL Composite Indexing for Django

Posted my first Django snippet! This concerns a quick and dirty hack for getting composite indexing in MySQL. It’s also a simple example of how to use Django’s post_syncdb signal.

Django currently comes with a unique_together meta attribute you can use to specify unique combinations of fields. I think the backends create an index in the database from this. However, I couldn’t find anything for simply creating non-unique attributes, hence the hack you see in the snippet.

I don’t really have the patience to make it work fully for the other backends since I don’t use them currently (aside from SQLite3 for testing), but it’s GoodEnoughForMe.