MySQL, UTF-8 and Emoji

In a MySQL-based project we had a series of UncategorizedSQLExceptions in the log:

org.springframework.jdbc.UncategorizedSQLException:
### Error updating database.  
Cause: java.sql.SQLException: 
Incorrect string value: '\xF0\x9F\x98\x98' for column 'note' at row 1

These exceptions had occured because some users entered Emoji characters into a free text field. Emojis are encoded as UTF-8 character. Normally this isn’t a problem, but the MySQL “utf8” character set only supports UTF-8 characters with 3 bytes(!). So Emoji cannot be stored into a “utf8”-encoded MySQL database. 👎

The solution

MySQL 5.5 introduced an “utf8mb4” character set [1]. If you create new MySQL databases you should use this encoding right from the beginning. Otherwise you have to perform a migration afterwards which might take some time. ⌚ [2][3].

[1] http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
[2] http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-upgrading.html
[3] https://mathiasbynens.be/notes/mysql-utf8mb4