Skip to content

Instantly share code, notes, and snippets.

@ain
Last active December 17, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ain/5539623 to your computer and use it in GitHub Desktop.
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
#!/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