Created
February 11, 2013 20:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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