Convert latin1 tables/fields to utf8

I had some screwup which made all my tables/fields latin1. That sucks, because I didn’t want to manually correct this. I found the following lines of code that fix this problem.

mysql --database=marlon -B -N -e "SHOW TABLES" \
| awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}' \
| mysql --database=marlon &

Also note that if you’re converting from some other obscure character set it’s best to first convert to binary and then convert to utf8, making sure you won’t lose data in the conversion.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation