MySQL dump error 'illegal mix of collations' happens when your database table encoding and connection encoding noes not match.
In this context, we shall look into methods to fix MySQL error.
Nature of MySQL dump error 'illegal mix of collations'
Typical error might look as shown below:
Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
This error generally occurs by comparing two strings of incompatible collations or by attempting to select data of different collations into a combined column.
Here, they are utf8_unicode_ci and utf8_general_ci.
Hence, we need to make the two columns collation match.
How to fix MySQL dump error 'illegal mix of collations' ?
1. Change the collation of one column (or string) to match the other collation.
Find the columns with inappropriate collation:
SHOW CREATE TABLE table_name;
So the chances are, you can find the column with a different collation or it hasn’t been specified at all.
Then, you can change the collation on each column:
ALTER TABLE table_name CHANGE col_name data_type CHARACTER SET charset_name COLLATE collation_name;
If you want to make a collation change table-wide:
ALTER TABLE table_name CONVERT TO CHARACTER SET charset_name COLLATE collation_name;
2. Add a COLLATE clause to specify the collation used in your query.
SELECT * FROM table _name ORDER BY col_name COLLATE collation_name;
3.Generate ALTER TABLE command into sql file and execute sql file on database. a.Generate ALTER TABLE command into sql file: SELECT CONCAT("alter table `", TABLE_SCHEMA,"`.`", TABLE_NAME, "` convert to character set utf8 collate utf8_general_ci;") as MySQLCMD
INTO OUTFILE "/var/tmp/test_schema_update.sql"
FROM information_schema.TABLES WHERE TABLE_SCHEMA = "test" The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax. file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed. As alternative if you are using Virtual machine or Docker container you could use client command such as mysql -e to generate file on local environment: mysql -h172.17.0.1 -uroot -e 'SELECT CONCAT("alter table `", TABLE_SCHEMA,"`.`", TABLE_NAME, "` convert to character set utf8 collate utf8_general_ci;") as MySQLCMD FROM information_schema.TABLES WHERE TABLE_SCHEMA = "test"' > /var/tmp/test_schema_update.sql b.
To execute generated file please use below command:
mysql -h172.17.0.1 -uroot dbname < /var/tmp/test_schema_update.sql
If you have got such error:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘MySQLCMD
alter table `dbname`.`tablename` convert to character set utf8 collate ‘ at line 1Just open test_schema_update.sql file and remove MySQLCMD string from the very beginning and save. After that try again to import file to your database.
How to Change the collation from command line ?
To change the collation from command line:
1. Log into MySQL with SSH:
mysql -u admin -p`cat /etc/psa/.psa.shadow`
2. Enter your database password when prompted.
3. Run the following command to change the character set and collation of your database:
ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;
4. Run the following command to change the character set and collation of your table:
ALTER TABLE tablename CHARACTER SET utf8 COLLATE utf8_general_ci;
For either of these examples, please replace the example character set and collation with your desired values.