Answers.Body: <p>When you create a database in MySQL in Craft 2, you can set the <em>default</em> 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.</p>
  
  <p>Craft 2 will always explicitly set what to use based on what the <code>charset</code> and <code>collation</code> database config settings are set to in (by default <code>utf8</code> and <code>utf8_unicode_ci</code>).  Craft 3 does away with this and falls back on the database default.</p>
  
  <p>However, if you set those config settings to <code>utf8mb4</code> and <code>utf8mb4_unicode_ci</code> and try to install Craft 2 you'll run into errors:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>Because MySQL's <code>utf8</code> implementation uses 3 bytes per character and <code>utf8mb4</code> uses 4-bytes, that pushes many of Craft's indexes over InnoDB's <a href="https://stackoverflow.com/a/1814594/684">767 byte limitation</a>.</p>
  
  <p>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 <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/helpers/StringHelper.php#L474" rel="nofollow noreferrer">automatically HTML encode</a> them so they fit in MySQL's 3-byte <code>utf8</code> charset.</p>
  
  <p>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 <code>mb4</code> in the database directly.</p>
  
  <p>It's also worth noting that the upcoming Craft 3 supports PostgreSQL where none of this matters and it just works.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-14T20:18:20.433Z
Answers.LastActivityDate: 2017-11-05T02:14:09.413Z
Answers.LastEditDate: 2017-11-05T02:14:09.413Z
Answers.UserDisplayName: Brad Bell
Body: <p>The <em>Craft</em> docs call for using <code>utf8_unicode_ci</code> for MySQL collation. MySQL 5.5.3 introduced support for <code>utf8mb4_unicode_ci</code>, which, I believe, is required to support the storage of emojis.</p>
  
  <p>Does anyone have experience running <em>Craft</em> with the <code>utf8mb4_unicode_ci</code> collation? Any issues to watch out for?</p>
  
Tags: <mysql>
Title: MySQL Collation: utf8mb4_unicode_ci (emojis)
ThreadText: # MySQL Collation: utf8mb4_unicode_ci (emojis)
  
  (Asked by: username_0 on 2016-10-14)
  
  The *Craft* docs call for using `utf8_unicode_ci` for MySQL collation. MySQL 5.5.3 introduced support for `utf8mb4_unicode_ci`, which, I believe, is required to support the storage of emojis.
  
  Does anyone have experience running *Craft* with the `utf8mb4_unicode_ci` collation? Any issues to watch out for?
  
  **Tags:** mysql
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-10-14. Score: 7 | Accepted answer
  
  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](https://stackoverflow.com/a/1814594/684).
  
  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](https://github.com/pixelandtonic/Craft-Release/blob/master/app/helpers/StringHelper.php#L474) 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.
  
  
  ---
  
  