Skip to content

WordPress database charset and collation mismatch after migration

How to handle utf8 vs utf8mb4 charset and collation mismatches when migrating a WordPress database to cPanel — what breaks, how to detect it, and how to convert cleanly.

When a WordPress database moves from an older host to a current cPanel server, charset and collation often don’t match. Emojis stop rendering. Accented characters get replaced with question marks or ?s. Search returns no results for queries that worked yesterday. The cause is utf8 (which in MySQL means 3-byte UTF-8, not real UTF-8) vs utf8mb4 (4-byte, actual UTF-8 including emoji).

What’s happening

MySQL’s utf8 predates the rest of the world adopting full UTF-8. It supports characters in the Basic Multilingual Plane only — three bytes per character maximum. Emojis, some Asian scripts, and some mathematical symbols need four bytes; utf8 can’t store them. MySQL 5.5+ added utf8mb4, which is real UTF-8. WordPress 4.2+ defaults to utf8mb4. A site that originally lived on a MySQL 5.1 host with utf8 tables and then got migrated retains the original charset until you convert it.

How to verify

Check the database, server, and connection charsets:

wp db query "SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = DATABASE()"
wp db query "SHOW TABLE STATUS" | grep -i collation
wp db query "SHOW VARIABLES LIKE 'character_set_%'"

For WordPress’s view:

wp config get DB_CHARSET
wp config get DB_COLLATE

If the database charset is utf8 but DB_CHARSET says utf8mb4, WordPress is asking for 4-byte UTF-8 from a 3-byte UTF-8 database — and writes silently truncate at the first 4-byte character.

The fix

  1. Decide the target. Modern WordPress on modern MySQL: utf8mb4 with utf8mb4_unicode_520_ci (or utf8mb4_0900_ai_ci on MySQL 8.0+). Don’t go to utf8mb4_general_ci — its sorting is wrong for several languages.

  2. Take a backup first. Always, but especially before a charset conversion that touches every row:

    wp db export ~/pre-charset-backup-$(date +%F).sql
  3. Update wp-config.php:

    define( 'DB_CHARSET', 'utf8mb4' );
    define( 'DB_COLLATE', '' );

    An empty DB_COLLATE lets WordPress use the database default, which is what you want.

  4. Convert each table. The script:

    for table in $(wp db tables --format=csv | tr ',' '\n'); do
      wp db query "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci"
    done

    On large tables this rebuilds the table — plan for a maintenance window proportional to the table size. The wp_postmeta table on a busy WooCommerce site can take an hour.

  5. Some indexes hit MySQL’s 767-byte limit when converting from utf8 to utf8mb4 because the same character count now requires more bytes. Symptom: ALTER TABLE fails with “Specified key was too long”. Fix by enabling innodb_large_prefix (default on MySQL 5.7.7+) and innodb_file_format=Barracuda, then retry. Older WordPress versions had a DROP INDEX step they recommended; on recent MySQL/MariaDB the index limit is rarely hit.

  6. After conversion, verify:

    wp db query "SELECT TABLE_NAME, TABLE_COLLATION FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE()" | head

    Every table should now show the new collation. Try writing an emoji to a post, save, refresh — should display correctly.

  7. If converting an old utf8 database where mixed encoding rot has already happened (some rows have correct UTF-8, some have double-encoded characters from past migrations), straight conversion preserves the rot. In that case you need to dump, run a re-encoding script (iconv or a one-off PHP), then import. That’s the more complex path; do it only after you’ve confirmed the data is actually mis-encoded by inspecting raw bytes:

    wp db query "SELECT HEX(post_title) FROM wp_posts WHERE post_title LIKE '%é%' LIMIT 5"

For environments where this is part of a migration workflow, Stack Harbor handles charset conversion as part of migration.

Stack Harbor operates production WordPress at scale — see Enterprise WordPress for the managed model, or book a WordPress consult for a written read of your current shape.