ERROR 1273 (HY000) at line 38: Unknown collation: 'utf8mb4_uca1400_ai_ci'
Line 38 of a database dump. The import had been running for a while, which is the worst time to learn a thing like this.
What it means
I’m moving WordPress sites off older server images onto a newer, managed one. The sources vary in age. Some of the newer ones run MariaDB 11.x or 12.x. The target runs 10.11.
Newer MariaDB versions can create tables with a family of collations named uca1400, from a more recent Unicode collation algorithm. If a table is created with one, the dump faithfully says so in its CREATE TABLE and COLLATE clauses. MariaDB 10.11 has never heard of them and refuses the statement.
Nothing is wrong with the data. The target just doesn’t speak the dialect.
I hit it on two sites in the same batch. One had 31 tables using the new collation; the other had two. The 31-table one is the template other sites were once cloned from, which probably tells you why it was the newest thing in the fleet.
Don’t edit the dump
The dumps are gigabytes. I wasn’t going to open one in an editor, and I didn’t want to touch the original anyway, because it’s the only copy that’s known good. So the rewrite happens in the pipe, on the way into mysql.
gunzip -c db-export.sql.gz \
| sed -E 's/(utf8mb[34])_uca1400(_nopad)?_[a-z]{2}_[a-z]{2}/\1_unicode_ci/g;
s/([^_a-z0-9])uca1400(_nopad)?_[a-z]{2}_[a-z]{2}/\1unicode_ci/g' \
| sudo mysql --default-character-set=utf8mb4 wp_database
Two substitutions because the collation shows up in two shapes. With a character set prefix (utf8mb4_uca1400_ai_ci), I keep the prefix and swap the rest for unicode_ci. Bare (COLLATE uca1400_ai_ci after a column that already names its character set), I keep the leading character and swap likewise. The _nopad variants are handled by the optional group. Both ignore the accent-insensitive/case-insensitive suffix pairs, which is why the pattern is just two letters, an underscore and two letters.
Only when needed
The sed shouldn’t run on dumps that don’t need it, and it certainly shouldn’t run on a target that does understand the collation. So the script asks the target first.
has_uca1400=$(sudo mysql -N -e "SELECT COUNT(*) FROM information_schema.collations
WHERE collation_name='utf8mb4_uca1400_ai_ci'")
if [ "$has_uca1400" = "1" ]; then
collate_fix() { cat; }
else
collate_fix() { sed -E '...'; }
fi
gunzip -c db-export.sql.gz | collate_fix | sudo mysql wp_database
A shell function that is either a plain cat or the sed keeps the pipeline the same shape in both cases. I also grep the dump for uca1400 first, so the log can say plainly that it found the collations and is mapping them. A silent rewrite of someone’s database is the kind of thing I’d want a record of.
What you give up
unicode_ci is an older collation. Sorting and comparison rules differ slightly from the newer ones, mostly around obscure characters. For a WordPress database full of posts and options I’ve never seen it matter. But if you’ve got a unique index on a text column, a looser comparison could in theory decide two values are the same when they weren’t before. Worth a look before you do this to anything other than WordPress.
The other route is to upgrade the target so it knows the collation. That’s the better answer when you can. When the target is fixed by whoever manages the image, mapping on the way in gets the import to finish.
I’d still rather have found out at line 38 of a test import than at line 38 of the real one. I didn’t, of course. That was the real one.
