Dave Perrett

Force MySQL Encoding to UTF8

database, linux, mysql, programming, web

Often MySQL will need to be forced to return results in UTF8 encoding. There are two ways to do this:

  1. Run a query “SET NAMES ‘utf8’” at the start of each connection - this will force MySQL to use UTF8 for the lifetime of that connection. This is a good option if you don’t have access to the my.cnf file.

  2. A better option - find and edit the file my.cnf on your database server. Add the following lines to the mysqld section :

1
2
3
[mysqld]
default-character-set=utf8
skip-character-set-client-handshake

Now if you restart MySQL, the encoding should default to UTF8.