Wondering how to resolve MySQL dump error ‘illegal mix of collations’? We can help you.
As part of our Server Management Services, we assist our customers with several MySQL queries.
Today, let us see how our Support Techs fix this error.
What causes 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.
In this case, they are utf8_unicode_ci and utf8_general_ci.
Hence, we need to make the two columns collation match.
How to resolve it?
Let us see some of the steps followed by our Support Techs to resolve the issue.
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.
Change the collation from command line.
Let us go through the steps followed by our Support techs to change the collation from command line.
- Log into MySQL with SSH:
mysql -u admin -p`cat /etc/psa/.psa.shadow`
- Enter your database password when prompted.
- Run the following command to change the character set and collation of your database:
ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;
- 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.
[Finding it hard to fix MySQL error? We’d be happy to assist you]
Conclusion
In short, today we saw steps followed by our Support Techs to resolve this MySQL error.
0 Comments