Peter Marklund

Peter Marklund's Home


Rails Deployment: Check MySQL Charset on Startup

The UTF-8 support got a lot better with the Rails 1.2 release but since Rails has evolved so fast many of the instructions you'll find on the web are dated. Rails will now default the Content-Type of its responses to UTF-8 so you don't need to set that yourself in an after filter.

When it comes to configuring MySQL to use UTF-8 there are at least three pieces to keep track of - the database encoding, the client encoding, and the connection encoding. For my project it was enough to set "default-character-set=utf8" in the MySQL my.cnf config file (takes care of the database part) and set "encoding: utf8" in database.yml (takes care of the connection and client).

A really great idea though is to check that the proper encoding is configured when your server starts up in production on your various servers and abort if it isn't. Credit goes to Mike Clark and the Pragmatic Programmers for sharing the following useful code snippet:

if RAILS_ENV == "production"
%w(character_set_database character_set_client character_set_connection).each do |v|
  ActiveRecord::Base.connection.execute("SHOW VARIABLES LIKE '#{v}'").each do |f|
    unless f[1] == "utf8"
      puts "ERROR: MySQL database isn't properly encoded!"
      puts "Kindly set your #{f[0]} variable to 'utf8'."
      RAILS_DEFAULT_LOGGER.error("MySQL database isn't properly encoded!")
      exit 1

This is yet another example of failing early, one of my favorite design principles, and it seems to have become a running theme in this blog...

1 comment(s)


Peter Marklund said 2008-02-21 10:37:

See the mysql_requirement plugin: