Home About Me

MySQL utf8mb4: What Changes When You Pick a Collation

MySQL gives you two related choices when storing text: the character set and the collation. With utf8mb4, the first decision is usually straightforward. The second one decides how strings are compared and sorted, and it is where version differences and migration problems tend to show up.

utf8mb4 and the older utf8 name

utf8mb4 is a MySQL character set for Unicode data. It covers the usual letters and symbols as well as emoji and supplementary characters. MySQL has supported it since 5.5.3. In older versions, the character set called utf8 was effectively utf8mb3: it allowed UTF-8 encodings of up to three bytes. That limit means some characters, including certain emoji and auxiliary characters, cannot be stored or handled correctly. utf8mb4 raises the limit to four bytes and therefore covers a wider range.

Common utf8mb4 collations

utf8mb4_general_ci is the long-standing general-purpose collation. It is case-insensitive, so 'a' and 'A' compare as equal, and it takes multiple languages into account in a broad way.

utf8mb4_unicode_ci is based on the Unicode Collation Algorithm. It is also case-insensitive, but it is more precise than general_ci and can sort characters from different languages more correctly.

utf8mb4_bin compares by binary value. It is case-sensitive, and under this collation 'A' sorts before 'a'.

utf8mb4_0900_ai_ci arrived with MySQL 8.0.0. It uses UCA 9.0.0, is case-insensitive, and handles sorting and comparison of many characters more accurately. Before 8.0, utf8mb4 commonly used utf8mb4_general_ci, which could produce unexpected ordering or comparison results for some characters.

MySQL also ships other collations, such as utf8mb4_unicode_520_ci and utf8mb4_unicode_520_bin. The right choice depends on the comparison rules an application needs.

Defaults are version-dependent

If a table uses utf8mb4 as its default character set but does not name a collation, the server picks one. In MySQL 5.7 that default is utf8mb4_general_ci. In MySQL 8.0 it is utf8mb4_0900_ai_ci.

That difference matters during migrations. utf8mb4_0900_ai_ci exists only in MySQL 8.0, so a schema or dump that relies on it cannot be loaded into MySQL 5.7 or 5.6 without changes. The typical error is:

[Err] 1273 - Unknown collation: 'utf8mb4_0900_ai_ci'

One fix is to set the new database to a collation the older server understands. Another is to edit the SQL file and replace the collation references before import.

unicode_ci versus general_ci

The difference between these two collations is mainly accuracy versus sorting cost. utf8mb4_unicode_ci follows standard Unicode rules for comparison and sorting, so it can handle special characters and order text correctly across languages. utf8mb4_general_ci does not follow standard Unicode in the same way and cannot handle some special characters.

On performance, utf8mb4_general_ci has an edge in sorting because utf8mb4_unicode_ci implements more complex algorithms for special characters. In most workloads the gap is not noticeable, so the more precise behavior may be worth choosing when correctness across languages matters.

Server-level collation settings

MySQL 5.6 introduced the collation_server system variable. It sets the server-level default collation and defines the collation used by default when new tables are created. To inspect the current value:

SHOW VARIABLES LIKE 'collation_server';

The result set contains the variable name and its value. This is a server-level setting, normally placed in a configuration file such as my.cnf or my.ini and applied when the MySQL server starts.

Several defaults flow from that setting. If collation_database is not specified when the service starts, it inherits the value of collation_server. If a database is created without an explicit collation, it uses the value of collation_database.

There are version caveats here. character_set_database and collation_database were deprecated in MySQL 5.7 and are slated for removal. MySQL also added default_collation_for_utf8mb4, which controls the default collation when utf8mb4 is used and accepts either utf8mb4_0900_ai_ci or utf8mb4_general_ci. This setting applies when:

  • SHOW COLLATION and SHOW CHARACTER SET are run;
  • a database is created or altered with utf8mb4 but no collation;
  • a table is created or altered with utf8mb4 but no collation;
  • a column is added or modified with utf8mb4 but no collation;
  • utf8mb4 is used in other situations without an explicit collation.

Choosing utf8mb4 is only half the decision. The collation controls equality, ordering, and portability, and the safe choice depends on the MySQL version and the kind of text the application expects to compare.