A customer reports that emoji in WordPress comments show as ???. You check wp-config.php — DB_CHARSET = utf8mb4. You check the table — DEFAULT CHARSET=utf8mb4. You check phpMyAdmin — connection collation is utf8mb4_unicode_ci. Everything looks right. The problem is that one specific column was created back when the schema was utf8 and never migrated, or the index length is too short for utf8mb4, or a JOIN crosses a column that’s still utf8 against one that’s utf8mb4 and MySQL silently drops down to the lowest common denominator. The migration is more than ALTER TABLE ... CONVERT TO.
What’s happening
MySQL’s “utf8” is a historical mistake — it’s a three-byte subset that cannot represent any character outside the Basic Multilingual Plane. Emoji, some CJK ideographs, mathematical symbols — all four bytes, all dropped or replaced. utf8mb4 is the actual variable-width UTF-8. The two charsets cannot interoperate cleanly in joins or comparisons; MySQL converts one side, often to the narrower one, and you lose data.
The conversion has gotchas:
- Index byte limit: utf8 indexes are sized for 3-byte chars. A
VARCHAR(255)index in utf8 is 765 bytes — fine. In utf8mb4 it’s 1020 bytes — fails on InnoDB unlessinnodb_large_prefixis enabled (default in 5.7+) and the table uses DYNAMIC or COMPRESSED row format. - Server default vs table default vs column default: each level has its own charset/collation. Setting the database default doesn’t change tables. Setting the table default doesn’t change columns.
- Collation choice matters:
utf8mb4_unicode_civsutf8mb4_0900_ai_civsutf8mb4_general_ciproduce different sort orders. Mixing collations in a JOIN fails with “illegal mix of collations.”
How to verify
mysql -e "SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COLLATION FROM information_schema.TABLES WHERE TABLE_SCHEMA='acmecorp_wp'"
mysql -e "SELECT TABLE_NAME, COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='acmecorp_wp' AND CHARACTER_SET_NAME IS NOT NULL"
mysql -e "SHOW VARIABLES LIKE 'character_set_server'"
mysql -e "SHOW VARIABLES LIKE 'innodb_large_prefix'"
mysql -e "SELECT NAME, ROW_FORMAT FROM information_schema.INNODB_TABLES WHERE NAME LIKE 'acmecorp_wp/%'"
The first two find any table or column not in utf8mb4. The third confirms the server default. The last two confirm InnoDB can hold the longer indexes you’re about to create.
The fix
- Back up first:
mysqldump --single-transaction --default-character-set=utf8mb4 acmecorp_wp > /home/acmecorp/backups/pre-utf8mb4.sql. Test the backup restores into a scratch database before you touch the live one. - Set the server default in
/etc/my.cnf.d/cpanel.cnf:
Restart MySQL. New databases will default to utf8mb4. Existing ones won’t change yet.[mysqld] character_set_server = utf8mb4 collation_server = utf8mb4_unicode_ci innodb_large_prefix = 1 innodb_file_format = Barracuda innodb_default_row_format = DYNAMIC - Convert the database default:
This affects new tables only.ALTER DATABASE acmecorp_wp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - Convert each table:
For a database with 50 tables, generate the statements with a query:ALTER TABLE acmecorp_wp.wp_posts CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Save the output, run the statements during a maintenance window. CONVERT TO rebuilds the table — on a 50GB table it takes hours and holds locks. For very large tables, useSELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;') FROM information_schema.TABLES WHERE TABLE_SCHEMA='acmecorp_wp';pt-online-schema-changefrom Percona Toolkit. - Check for index byte limit failures in the ALTER output. Any table with a 255-char index that fails needs the index shortened (
KEY my_key (col(191))) — 191 is the safe utf8mb4 length for the legacy 767-byte limit; ifinnodb_large_prefixis on, you can leave indexes at 255 chars. - Update application config:
DB_CHARSET = utf8mb4andDB_COLLATE = utf8mb4_unicode_ciinwp-config.phpand equivalent for other apps. Without this, the connection negotiates back to utf8 and your work is wasted. - Spot-check a known multibyte row:
If the rocket emoji comes back intact, the migration is complete. If it comes back asINSERT INTO acmecorp_wp.wp_options (option_name, option_value) VALUES ('utf8mb4_test', 'test rocket: 🚀'); SELECT option_value FROM acmecorp_wp.wp_options WHERE option_name='utf8mb4_test';?, one of the layers is still utf8.
For environments running many cPanel WordPress accounts where the utf8mb4 migration was never completed, Stack Harbor’s environment management team batches the conversion with proper locking windows and verifies each account post-migration.