When you create a database in MySQL in Craft 2, you can set the default charset and collation, so that any SQL statements that get executed that don't explicitly specify what charset/collation to use, MySQL will fall back on those.
Craft 2 will always explicitly set what to use based on what the charset
and collation
database config settings are set to in (by default utf8
and utf8_unicode_ci
). Craft 3 does away with this and falls back on the database default.
However, if you set those config settings to utf8mb4
and utf8mb4_unicode_ci
and try to install Craft 2 you'll run into errors:
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes.
Because MySQL's utf8
implementation uses 3 bytes per character and utf8mb4
uses 4-bytes, that pushes many of Craft's indexes over InnoDB's 767 byte limitation.
It's worth noting that if the only place you think you'll need 4-byte support for emojis is in a Rich Text field, then Craft will automatically HTML encode them so they fit in MySQL's 3-byte utf8
charset.
If you need them outside the context of a Rich Text field, then your best bet would be to manually set just that column's charset and encoding to use mb4
in the database directly.
It's also worth noting that the upcoming Craft 3 supports PostgreSQL where none of this matters and it just works.