Explain Codes LogoExplain Codes Logo

#1273 – Unknown collation: ‘utf8mb4_unicode_520_ci’

sql
collation
mysql-error
database-migration
Anton ShumikhinbyAnton Shumikhin·Oct 5, 2024
TLDR

Faced with the #1273 error featuring ‘utf8mb4_unicode_520_ci’? Your MySQL can't recognize this collation. Opt for the globally supported collation—utf8mb4_unicode_ci. For a swift solution, execute:

ALTER TABLE `your_table` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

This command adjusts your_table to use a compatible collation, effectively tackling the error. Always remember to create a backup prior to running the script.

Collation fundamentals in MySQL

"Collation" provides a set of rules for comparing characters within a character set. Problems often emerge during the migration or update process when the target MySQL server fails to recognize the source's collation. To circumvent the #1273 error, it's critical to align these collation settings.

Spotting the error source

  1. Version Mismatch: Collations like utf8mb4_unicode_520_ci are introduced in newer MySQL versions and might not be built into older versions.
  2. Server Incompatibility: The problem might arise when the incoming data is sourced from a server running a MySQL version that fails to recognize the newer collation.
  3. During CSV / SQL Export: The error arises if collation mismatches occur in exported databases.

Proactive resolutions

  • MySQL Version Update: Ensuring your MySQL server version is updated to at least 5.6 amplifies the support for utf8mb4_unicode_520_ci.
  • Collation Swap: A text editor can be used to Find and Replace instances of utf8mb4_unicode_520_ci with utf8mb4_unicode_ci or utf8mb4_general_ci inside your .sql dump file.
  • Character Set Tweak: In certain cases, altering the character set in your database export from utf8mb4 to utf8 can ensure compatibility with previous servers.

Never skip a backup

It's a golden rule in database operations — always export a copy of your database or the specific table before making any changes. It serves as a safety net for recovery if things don't go as planned.

Ensuring compatibility during migration

Avoiding errors like #1273 involves considering server collations extensively. Your database settings should reflect the target environment's collation setting — a universally accepted pick keeps you in safe waters.

Additional troubleshooting tricks

Post-collation-change woes

After updating the collation, you might encounter some additional issues due to charset inconsistencies or other mismatches.

  • Error Examination: Error messages hold clues for their resolution. Address these specifically.

  • DATABASE DEFAULTS: You can set a DEFAULT CHARSET and COLLATE at the database level to ensure that all new tables inherit compatible settings:

    -- Like setting the default ringtone for a new contact CREATE DATABASE your_db DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;
  • Consistency in Environment: Always keep your development and production databases alike to avoid any migration surprises.

What about older MySQL versions?

In scenarios where upgrading the MySQL version is off the table, and you are stuck with 5.5 or earlier versions:

  • Use the find-and-replace edits to switch to utf8mb4_unicode_ci or utf8_unicode_ci, both of which offer improved support on older servers.

Pre-deployment checks

Before your final roll-out:

  • Make sure all the collation references within views, stored procedures, and triggers have been updated.
  • Consider a small-scale import or migration on a development server as a trial run.