How to Fix the Illegal Mix of Collations Error in MySQL

This guide covers how to fix the “illegal mix of collations” error in MySQL.

What is the Illegal Mix of Collations Error?

The "illegal mix of collations" error in MySQL, identified by error code 1267, occurs when you try to compare or combine text columns with incompatible collations (collations are a set of rules for comparing characters in a character set). This error is common when performing operations like JOIN, WHERE, or ORDER BY on columns with different collations.

Identifying the Error

You can identify this error when MySQL returns the message: "Illegal mix of collations (collation1) and (collation2) for operation 'operation_name'". The error code associated with this issue is 1267.

Diagnosing Collation Settings

To diagnose the problem, check the collation of the involved columns, tables, or databases. Use the following SQL commands:

SELECT COLLATION_NAME FROM information_schema.columns WHERE table_schema = 'your_database' AND table_name = 'your_table'; SHOW TABLE STATUS LIKE 'your_table'; SHOW CREATE DATABASE your_database;

Aligning Collations

Once you've identified the collation differences, align them. You can do this at the column, table, or database level. Here’s how to update the collation:

Updating Column Collation

ALTER TABLE your_table MODIFY your_column VARCHAR(255) COLLATE desired_collation;

Updating Table Collation

ALTER TABLE your_table CONVERT TO CHARACTER SET charset_name COLLATE desired_collation;

Updating Database Collation

ALTER DATABASE your_database CHARACTER SET charset_name COLLATE desired_collation;

Default Collation Setting

To prevent future errors, set a default collation at the server or database level:

Server-Level Setting

[mysqld] character-set-server = charset_name collation-server = collation_name

Database-Level Setting

CREATE DATABASE your_database CHARACTER SET charset_name COLLATE collation_name;

Handle Collations in Application Code

If for some reasons you can’t modify database collations, you can handle collation differences in your application code. Just convert strings to a consistent collation before sending them to the database.

Check out Basedash

If you want to make database management easier, check out Basedash. Basedash is the best admin interface for your MySQL database, and you can get set up in just a couple clicks. You can share queries, run CRUD operations, build collaborative dashboards and let your team read and edit data.

Invite only

We're building the next generation of data visualization.