Currently we are using the following commands in PHP to set the character set to UTF-8 in our application.
Since this is a bit of overhead, we'd like to set this as the default setting in MySQL. Can we do this in /etc/my.cnf or in another location?
SET NAMES 'utf8'
SET CHARACTER SET utf8
I've looked for a default charset in /etc/my.cnf, but there's nothing there about charsets.
At this point, I did the following to set the MySQL charset and collation variables to UTF-8:
skip-character-set-client-handshake
character_set_client=utf8
character_set_server=utf8
Is that a correct way to handle this?
MySQL v5.5.3 and greater:
Just add three lines only in the [mysqld] section:
Note: Including
skip-character-set-client-handshake
here obviates the need to include bothinit-connect
in[mysqld]
anddefault-character-set
in the[client]
and[mysql]
sections.The directive has changed to
character-set-system=utf8
http://dev.mysql.com/doc/refman/5.6/en/charset-configuration.html
You can do it the way it does, and if it doesn't work, you need to restart mysql.
This question already has a lot of answers, but Mathias Bynens mentioned that 'utf8mb4' should be used instead of 'utf8' in order to have better UTF-8 support ('utf8' does not support 4 byte characters, fields are truncated on insert). I consider this to be an important difference. So here is yet another answer on how to set the default character set and collation. One that'll allow you to insert a pile of poo (
On Fedora 21
Add follow:
Save and exit.
Final remember restart service mysqld with
service mysqld restart
.NijaCat was close, but specified overkill:
To set the default to UTF-8, you want to add the following to my.cnf
Then, to verify: