Skip to content

Instantly share code, notes, and snippets.

@alcides
Created February 11, 2013 20:34
Show Gist options
  • Save alcides/4757369 to your computer and use it in GitHub Desktop.
Save alcides/4757369 to your computer and use it in GitHub Desktop.
Fix MySQL encodings. When migrating a UTF-8 database, encodings got f*ckd up, I had fix every field in all tables. This script automated the process.
#!/bin/bash
USERNAME="myuser";
DATABASE="mydb";
PASSWORD="mypass";
# Convert all fields in all databases using this trick
# convert(convert(convert(firstname USING latin1) USING binary) using utf8)
mysql -u $USERNAME -p$PASSWORD -B -N -e "SHOW TABLES" $DATABASE | while read table
do
echo "table: $table";
mysql -u $USERNAME -p$PASSWORD -B -N -e "select column_name from information_schema.columns where table_name='$table'" $DATABASE | while read col
do
echo " $col";
mysql -u $USERNAME -p$PASSWORD -B -N -e "UPDATE $table SET $col = convert(convert(convert($col USING latin1) USING binary) USING utf8)" $DATABASE
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment