Last active
December 17, 2015 02:59
-
-
Save ain/5539623 to your computer and use it in GitHub Desktop.
Convert MySQL collation of all fields in database to UTF-8 at once
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 | |
usage="Usage: mysql_collation_to_utf8 database_name username" | |
err[1]="Invalid command" | |
# Check for database name argument | |
if [ -z "$1" ] | |
then | |
echo ${err[1]} | |
echo "$usage" | |
exit 1 | |
else | |
db_name=$1 | |
fi | |
# Check for username argument | |
if [ -z "$2" ] | |
then | |
echo ${err[1]} | |
echo "$usage" | |
exit 1 | |
else | |
username=$2 | |
fi | |
read -s -p "Password: " passwd | |
mysql -u $username -p$passwd --database=$db_name -B -N -e "SHOW TABLES" \ | |
| awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; SET foreign_key_checks = 1; "}' \ | |
| mysql -u $username -p$passwd --database=$db_name & | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment