Last active
May 22, 2019 16:04
-
-
Save ddebin/c0b04fb3c82d1b448f9ec7fe207f01b2 to your computer and use it in GitHub Desktop.
Update MySQL Database charset/collate (and all tables)
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 | |
# cf. https://lxadm.com/MySQL:_changing_database_character_set_and_collate | |
# cf. https://medium.com/@alexBerg/my-war-with-mysql-or-how-did-i-switch-to-full-utf8mb4-73b257083ac8 | |
# Assume MySQL authentication done via .my.cnf | |
DATABASE="db_name" | |
#CHARACTER_SET=utf8 | |
#COLLATE=utf8_unicode_ci | |
CHARACTER_SET=utf8mb4 | |
COLLATE=utf8mb4_unicode_ci | |
echo "ALTER DATABASE $DATABASE CHARACTER SET $CHARACTER_SET COLLATE $COLLATE;" | |
echo "ALTER DATABASE $DATABASE CHARACTER SET $CHARACTER_SET COLLATE $COLLATE;" | mysql | |
TABLES=$(echo SHOW TABLES | mysql -s "$DATABASE") | |
for TABLE in $TABLES ; do | |
echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARACTER_SET COLLATE $COLLATE;" | |
echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARACTER_SET COLLATE $COLLATE;" | mysql "$DATABASE" | |
echo "ALTER TABLE $TABLE DEFAULT CHARSET=$CHARACTER_SET COLLATE $COLLATE;" | |
echo "ALTER TABLE $TABLE DEFAULT CHARSET=$CHARACTER_SET COLLATE $COLLATE;" | mysql "$DATABASE" | |
don |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment