Sunday, April 18, 2010

UTF-8 setting in Grails, mysql

Client UTF8----(A)---->
Server UTF8 ----(B)---->
Database UTF8 --- (C)---->
Table UTF8 ---(D)----> Row UTF8

1: Client UTF8
    <~meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

2: Server UTF8

In DataSource.groovy
url = "jdbc:mysql://localhost:3306/home?tcpKeepAlive=true&useUnicode=true&characterEncoding=UTF8"
This forces the driver to recognize that all text being sent to the driver is in UTF8. This should conform directly with Java's default UTF8 encoding without any translations occurring.

3: Database UTF8
mysql> show variables like '%character%';

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> set global character_set_server=utf8;
be sure to include the option in your my.cnf to ensure this option is persisted for a mysqld restart.

/etc/mysql/my.conf - default-character-set = utf8

No comments: