Last active
April 3, 2022 09:23
Small utility to change a database collation & character set
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 | |
DB_USER="" # your database user | |
DB_PASSWORD="" # your database password | |
DB_NAME="" # your database name | |
CHARACTER_SET="utf8mb4" # your default character set | |
COLLATE="utf8mb4_unicode_ci" # your default collation | |
mysql -u $DB_USER -p$DB_PASSWORD -e "ALTER DATABASE `$DB_NAME` CHARACTER SET = $CHARACTER_SET COLLATE $COLLATE;" | |
echo "$DB_NAME default collation changed to $COLLATE" | |
tables=`mysql -u $DB_USER -p$DB_PASSWORD -e "SELECT tbl.TABLE_NAME FROM information_schema.TABLES tbl WHERE tbl.TABLE_SCHEMA = '$DB_NAME' AND tbl.TABLE_TYPE='BASE TABLE'"` | |
for tableName in $tables; do | |
if [[ "$tableName" != "TABLE_NAME" ]] ; then | |
mysql -u $DB_USER -p$DB_PASSWORD -e "ALTER TABLE $DB_NAME.$tableName CONVERT TO CHARACTER SET $CHARACTER_SET COLLATE $COLLATE;" | |
echo "$tableName - done" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment