MySQL & MariaDB error 1267: Illegal mix of collations
Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '=' (5.6.51) Two strings with different collations (or character sets) are compared, and the server can't decide which rules to use. Common after upgrades, because MySQL 8.0's default collation changed to utf8mb4_0900_ai_ci.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_CANT_AGGREGATE_2COLLATIONS | ER_CANT_AGGREGATE_2COLLATIONS |
| SQLSTATE | HY000 | HY000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='. A collation defines how strings compare (case, accents, sort order). When both sides of a comparison, JOIN or UNION have equal priority but different collations, the server refuses to pick one.
Why it happens
- Tables created at different times: older ones with
utf8mb4_general_ciorutf8mb4_unicode_ci, newer ones with MySQL 8.0’s defaultutf8mb4_0900_ai_ci(or MariaDB 11.4+‘sutf8mb4_uca1400_ai_ci). - A stored procedure or function whose parameters took the database default collation when it was created, compared with a column that has another.
- The connection collation (
collation_connection) differs from the column’s when comparing to string literals or user variables. - Mixing character sets, such as a latin1 column and a utf8mb4 column.
How to fix it
- Quick fix in the query:
… ON a.s = b.s COLLATE utf8mb4_general_ci. This prevents index use on the converted side. - Proper fix: give the columns the same collation:
ALTER TABLE b CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;(or the reverse). Check the whole schema withSELECT table_name, column_name, collation_name FROM information_schema.COLUMNS WHERE table_schema = DATABASE() AND collation_name IS NOT NULL; - Set a consistent default for new objects:
ALTER DATABASE … COLLATE …and in the server config.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE a (s VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci); CREATE TABLE b (s VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci); INSERT INTO a VALUES ('x'); INSERT INTO b VALUES ('x');Run:
SELECT * FROM a JOIN b ON a.s = b.s;
You get: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='(11.8.9)
Verified fixes
Force one collation in the comparison (quick fix)
SELECT a.s FROM a JOIN b ON a.s = b.s COLLATE utf8mb4_general_ci;Make the columns use the same collation (proper fix)
ALTER TABLE b MODIFY s VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; SELECT a.s FROM a JOIN b ON a.s = b.s;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Compare two columns with different collations repro | ✓ 1267 | ✓ 1267 | ✓ 1267 | ✓ 1267 | ✓ 1267 | ✓ 1267 | ✓ 1267 | ✓ 1267 | ✓ 1267 |
| Force one collation in the comparison (quick fix) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Make the columns use the same collation (proper fix) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |