Site builders may encounter MySQL collation mismatch errors during queries or may wish to leverage more performant collations. To resolve these issues, normalize the database collation by following the steps below. This process involves converting table collations and configuring Drupal connection settings to enforce consistency for future module installations and updates.
Back up your data: Always perform a full database backup before proceeding.
Test in a non-production environment: Perform these steps in a non-production environment first to ensure the changes do not adversely affect application functionality and performance.
Expect downtime: Changing a column’s collation forces an index regeneration. This operation locks tables and can be time-consuming for large datasets. Place the site in Maintenance Mode or perform the operation during off-peak hours.
Verify limits: Converting to utf8mb4 increases character byte requirements. Ensure index lengths do not exceed MySQL limits (e.g., 767 bytes for older configurations).
The MySQL version dictates what the available collations are.
Ideally, all your environments should be running the same MySQL version.
If running different versions, then a requirement is to use a common collation compatible with all environments. Note that mismatched versions can still cause some behavioral differences.
Example: the utf8mb4_general_ci collation works with both MySQL 5.7 and 8.0, whereas utf8mb4_0900_ai_ci requires MySQL 8.0+.
utf8mb4_0900_ai_ci mismatched collation errors.## Example. Edit the final line with the collation you picked in the previous step(s).
SELECT table_name, table_collation
FROM information_schema.TABLES
WHERE table_schema = DATABASE() ## Replace with an actual DB name if needed.
AND table_collation != 'utf8mb4_0900_ai_ci';
## Example for converting a table to utf8mb4_general_ci.
## Edit to match the desired charset and collation.
ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;## Example to generate SQL statements (which should be verified and then run later)
## to modify all tables that do not match the desired collation utf8mb4_0900_ai_ci
## EDIT the table_schema value of 'EDIT_ME' (or you can use table_schema = DATABASE() to use the current selected DB).
SELECT CONCAT('ALTER TABLE `', table_schema, '`.`', table_name, '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;') AS _sql
FROM information_schema.TABLES
WHERE table_schema = 'EDIT_ME' AND table_collation != 'utf8mb4_0900_ai_ci';Update settings.php to prevent new tables from using incorrect defaults. This requires the addition of the collation key to the $databases array.
For Acquia Cloud: Follow the https://docs.acquia.com/acquia-cloud-platform/overriding-drupal-databases-settings documentationrequire statement:
# Edit to specify the target collation
$databases['default']['default']['collation'] = 'utf8mb4_general_ci';Ensure all developers use the updated settings.php and compatible local MySQL versions to prevent "collation reversion."