Fix database character set and collation

Convert a MySQL database and all its tables to utf8mb4 on TurboStack to fix garbled characters after an import or migration.

If text shows up garbled after an import or migration - accented letters turning into stray symbols, or emoji disappearing - the database is usually in an older character set such as latin1 or three-byte utf8. Converting it to utf8mb4 (full Unicode, including emoji) fixes this.

Convert a database and all its tables

Connect over SSH and convert in two parts - the database default, then each table. This small script does both for one database:

DB="prod_db"
CHARSET="utf8mb4"
COLLATION="utf8mb4_unicode_ci"

mysql -e "ALTER DATABASE \`$DB\` CHARACTER SET $CHARSET COLLATE $COLLATION;"
mysql -N -s -e "SHOW TABLES" "$DB" | while read TABLE; do
  echo "Converting $DB.$TABLE"
  mysql "$DB" -e "ALTER TABLE \`$TABLE\` CONVERT TO CHARACTER SET $CHARSET COLLATE $COLLATION;"
done

utf8mb4_unicode_ci is a good general-purpose collation. Some applications expect a specific one (for example utf8mb4_general_ci or utf8mb4_0900_ai_ci) - match what your application's documentation asks for.

Verify

Check that the database reports the new character set:

mysql -e "SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name='prod_db';"

Then reload the site and confirm the previously garbled text now displays correctly.