Frequently Asked Question

Illegal mix of collations DB-Tabellen-Kollation auf utf8mb umstellen

Last Updated 9 months ago

WordPress:

Oftmals werden durch (veraltete) Plugins falsche Zeichensätze für Datenbanktabellen definiert, was dann zu Fehlern wie "Illegal mix of collations" führt.
Mit folgendem Script kann die Kollation für alle Datenbanktabellen korrigiert werden (funktioniert auch für MySQL 8.4.):

Schnellösung:
Folgende Datei per wget in das WP-Verzeichnis laden und über die Shellkonsole ausführen:
wget https://download.roundaboutweb.info/public/kollation.sh

Beschreibung:
Das Script erstellt einen Dump (Datenbanksicherung) in das Verzeichnis /backup/DATENBANKNAME-DATUM.sql und führt im Anschluss die Korrektur der Tabellenkollation durch.
Im Anschluss muss die heruntergeladene Datei "kollation.sh" manuell wieder gelöscht werden: rm kollation.sh



manuelle Lösung:

#!/bin/bash

#wp-cli herunterladen
wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

charset='utf8mb4'
collate='utf8mb4_unicode_ci'

DATEsecond=$(date "+%Y%m%d%H%M%S")
BACKUP="/backup"
LOG="$BACKUP/kollation.log"

echo $DATEsecond >> $LOG

# Datenbank ermitteln
database="$(php wp-cli.phar config get DB_NAME)"

# Backup erstellen
php wp-cli.phar db export $BACKUP/$database-$DATEsecond.sql >> $LOG

# Charset für Datenbank ändern
echo "Changing charset $database"
php wp-cli.phar db query --skip-column-names "SET sql_mode = ''; ALTER DATABASE $database CHARACTER SET = $charset COLLATE = $collate;"

# Tabellen ermitteln und Charset ändern
for table in $(php wp-cli.phar db query --skip-column-names 'show tables'); do
        echo "Changing charset of table $table"
        php wp-cli.phar db query --skip-column-names "SET sql_mode = ''; ALTER TABLE $table CHARACTER SET $charset COLLATE $collate"

        echo "Converting charset of table $table"
        php wp-cli.phar db query --skip-column-names "SET sql_mode = ''; ALTER TABLE $table CONVERT TO CHARACTER SET $charset COLLATE $collate" >> $LOG
done

rm wp-cli.phar

echo "Collation changed to: $collate" >> $LOG
echo "Scriptdatei noch löschen!"

globale Lösung ohne WP-CLI:

Shell-Datei im Projekt erstellen mit folgendem Inhalt:

#!/bin/bash

database='usr_p000000_1'
user='p000000'
pass='XXXXXXX'
host='db0000.mydbserver.com'

charset='utf8mb4'
collate='utf8mb4_unicode_ci'

echo "Changing charset of database: $database"
mysql --init-command='SET sql_mode = "";' -u$user -p$pass -h$host $database -s -e "ALTER DATABASE $database CHARACTER SET = $charset COLLATE = $collate;"

for table in $(mysql -u$user -p$pass -h$host $database -s --skip-column-names -e 'show tables')
do
  echo ''
  echo "Changing charset of table: $table"
  mysql --init-command='SET sql_mode = "";' -u$user -p$pass -h$host $database -s -e "ALTER TABLE $table CHARACTER SET $charset COLLATE $collate"

  echo "Converting charset of table: $table"
  mysql --init-command='SET sql_mode = "";' -u$user -p$pass -h$host $database -s -e "ALTER TABLE $table CONVERT TO CHARACTER SET $charset COLLATE $collate"
done

echo "Collation changed to: $collate"

Vorab unbedingt eine Sicherung (Dump) der Datenbank erstellen!

Please Wait!

Please wait... it will take a second!