Why Emojis Break MySQL: Fix utf8 vs utf8mb4 Collation
Fix truncated text and Error 1366 in MySQL. Discover why original MySQL utf8 only stores 3 bytes and how to safely migrate tables to utf8mb4.
MySQL throws error 1366: Incorrect string value or silently truncates strings at the first emoji because its legacy character set named utf8 is an incomplete 3-byte implementation of the Unicode standard. To store emojis, mathematical symbols, and historic scripts without data loss, you must migrate your database character set to utf8mb4 and update collation to utf8mb4_unicode_ci or utf8mb4_0900_ai_ci.
The historical flaw: Why MySQL utf8 is not real UTF-8
UTF-8, as defined in RFC 3629 and the Unicode Standard, encodes each character in 1 to 4 bytes:
- Standard ASCII characters (A-Z, 0-9): 1 byte
- Latin accents, Greek, Cyrillic, Hebrew: 2 bytes
- Asian characters, symbols, currency signs: 3 bytes
- Most emojis, mathematical alphanumeric symbols, historic scripts, and rare CJK characters (supplementary planes): 4 bytes
When MySQL added Unicode support in version 4.1 (released in 2004), its utf8 character set was limited to a maximum of 3 bytes per character, which covers only the Basic Multilingual Plane (BMP, U+0000 to U+FFFF). Characters outside the BMP had already been assigned since Unicode 3.1 (2001), but they were rarely used at the time.
When emojis arrived in Unicode 6.0 (2010), mostly outside the BMP, databases using the 3-byte utf8 could not store them. MySQL 5.5.3 (2010) introduced utf8mb4, but kept utf8 as an alias for the 3-byte version (utf8mb3) for backward compatibility. utf8mb3 is deprecated in MySQL 8.0 and later.
Differences between utf8 (utf8mb3) and utf8mb4
| Characteristic | Legacy MySQL utf8 (utf8mb3) | Modern MySQL utf8mb4 |
|---|---|---|
| Max Bytes Per Character | 3 bytes | 4 bytes |
| Unicode Plane Support | Plane 0 (BMP only, up to U+FFFF) | Planes 0 through 16 (Full Unicode) |
| Emoji Storage | Throws Error 1366 or truncates text | Native storage and indexing |
| Default in MySQL 8.0+ | Deprecated | Default character set |
| Default Collation | utf8_general_ci | utf8mb4_0900_ai_ci |
| Max VARCHAR(255) Index Size | 255 * 3 = 765 bytes | 255 * 4 = 1,020 bytes |
Step-by-step SQL migration from utf8 to utf8mb4
Migrating an existing database requires altering database defaults, individual table schemas, and client connection parameters.
1. Alter database defaults
Set default character set and collation for newly created tables:
ALTER DATABASE production_db
CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
2. Convert existing tables and columns
Convert each table and its textual columns (VARCHAR, TEXT, MEDIUMTEXT):
-- Convert table encoding and all string columns in one operation
ALTER TABLE comments
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
ALTER TABLE users
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
3. Update application connection parameters
Ensure your web application requests the 4-byte charset when establishing database connections:
-- Execute immediately after connecting (or set in database pool config)
SET NAMES utf8mb4;
In PHP PDO, configure your DSN string as mysql:host=localhost;dbname=production_db;charset=utf8mb4. In Node.js (mysql2), set { charset: 'UTF8MB4_UNICODE_CI' }.
Production Pro-Tip: The 767-byte index prefix trap
A frequent failure encountered during utf8mb4 migration is error 1071: Specified key was too long; max key length is 767 bytes.
In older MySQL storage engine configurations (InnoDB with the Antelope row format):
- Maximum index key prefix length was capped at 767 bytes.
- Under 3-byte
utf8, aVARCHAR(255)index required255 * 3 = 765 bytes, fitting just under the 767-byte ceiling. - Under 4-byte
utf8mb4, that sameVARCHAR(255)requires255 * 4 = 1,020 bytes, exceeding the 767-byte index limit and throwing Error 1071 duringALTER TABLE.
To resolve this issue:
- Use the
DYNAMICorCOMPRESSEDrow format, which allows index key prefixes up to 3,072 bytes. In MySQL 5.7.7 and laterinnodb_large_prefixis enabled by default (the option was removed in 8.0, where large prefixes are always available), andDYNAMICis the default row format since 5.7.9. - Set table row format explicitly:
ALTER TABLE table_name ROW_FORMAT=DYNAMIC;. - If using legacy MySQL versions, reduce column index length to
VARCHAR(191)(191 * 4 = 764 bytes).
When preparing queries or importing large text datasets containing multibyte emojis and international scripts, test your strings with an in-browser character and byte counter to inspect exact UTF-8 byte weights. To format and sanitize bulk arrays of strings into valid SQL clauses before database execution, use a client-side SQL list formatter.
Frequently Asked Questions (FAQ)
Does converting from utf8 to utf8mb4 double storage requirements?
No. UTF-8 is a variable-length encoding. Standard ASCII characters (such as English letters and numbers) still consume exactly 1 byte. Only characters requiring 4 bytes (such as emojis) consume additional disk space. Unused bytes are not padded in storage.
What is the difference between utf8mb4_general_ci and utf8mb4_unicode_ci?
utf8mb4_general_ci is a simplified collation that compares characters one by one and does not support expansions, so for example it treats ß as equal to s, while UCA-based collations treat it as ss. utf8mb4_unicode_ci (UCA 4.0.0) and the MySQL 8.0 default utf8mb4_0900_ai_ci (UCA 9.0.0) follow the Unicode Collation Algorithm and give more linguistically correct sorting and comparison.
What happens if strict mode is disabled and an emoji is inserted into a utf8 column?
In legacy MySQL setups with strict SQL mode disabled (STRICT_TRANS_TABLES off), MySQL does not throw an error. Instead, it issues a warning and truncates the value at the first character it cannot store, discarding the emoji and everything after it.