Back to Blog
Dotkernel

Forcing UTF8 connections and character set in MySQL

In some situations, it may be neccesar to force MySQL server collation and character set to UTF8. As you can't control all scripts that are connecting to your database( for instance: mysql command line, or mysqldump) For that , open the my.cnf file and add the below lines:
character_set_server=utf8
skip-character-set-client-handshake
If you are interested in better performance, add the below line:
collation_server=utf8_general_ci
If you are interested in better sorting order, add the below line instead:
collation_server=utf8_unicode_ci

Frequently Asked Questions

Why force UTF8 at the MySQL server level instead of relying on each client? +

Because you can't control all the scripts that connect to your database (for instance the mysql command line or mysqldump), so forcing the server's collation and character set to UTF8 in my.cnf guarantees it regardless of the connecting client.

What two lines enable UTF8 for all connections in my.cnf? +

character_set_server=utf8 and skip-character-set-client-handshake.

What's the difference between the two suggested collation settings? +

collation_server=utf8_general_ci is recommended for better performance, while collation_server=utf8_unicode_ci is recommended instead if better sorting order matters more.