Handle accented characters using utf8 encoding in MySQL

by Federica Della Monica last modified Feb 03, 2010 04:00 PM
The goal of this tutorial is to show how to solve the problem of the correct display of the accented characters in a MySQL table using utf8 encoding.

This how-to assumes that you have already installed both Python MySQL and ZMySQLDA packages and have a MySQL table.
When you insert accented characters in a field of a MySQL table and you don't have configured the system correctly, occur that you can find strange characters such as ì or è etc..

To solve this annoying problem you must do this:

1. Add a few lines of code in the file called my.cnf (generally under /etc/mysql) under the [mysqld] section:

collation_server=utf8_unicode_ci
character_set_server=utf8

Optionally you can also add the line:

skip-character-set-client-handshake

to enforce using of utf8 encoding in db.

2. Restart mysql.

3. Test the values of the character set system variables by digit from shell (terminal):

mysql> SHOW VARIABLES LIKE 'character_set%';

 Do this statement, should return this:

| 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)

Now you have correctly set MySQL character set!