Software & Applications

How to fix Unknown collation: 'utf8mb4_0900_ai_ci' when import database mysql

The error Unknown collation: 'utf8mb4_0900_ai_ci' occurs when you're trying to import a MySQL database dump that was created using a newer version of MySQL, typically MySQL 8.0, into an older version of MySQL that does not support the collation utf8mb4_0900_ai_ci.

Here are some ways to fix the issue:

Option 1: Modify the Dump File Before Importing
You can modify the database dump file manually or with a text editor to replace the unsupported collation with one that is supported by your current MySQL version.

#Steps:
1. Open the dump file in a text editor.
   - Use a text editor like Notepad++, Sublime Text, or VSCode for large files.

2. Search and replace utf8mb4_0900_ai_ci with utf8mb4_general_ci.
   - Find all occurrences of utf8mb4_0900_ai_ci and replace them with utf8mb4_general_ci (or another collation supported by your MySQL version).

3. Search and replace utf8mb4_0900_bin with utf8mb4_bin.
   - If your dump file includes utf8mb4_0900_bin, replace it with utf8mb4_bin.

4. Save the file and re-import it into your database.

Option 2: Use MySQL Workbench or phpMyAdmin to Adjust Collation
You can change the database collation in a visual tool like MySQL Workbench or phpMyAdmin:

1. Before exporting the database, choose a collation compatible with your MySQL version, such as utf8mb4_general_ci.
2. Re-export the database and ensure the new dump file does not use any unsupported collations.

Option 3: Use the sed Command (Linux/MacOS)
If you're on Linux or MacOS, you can use the sed command to replace the collations in the dump file:

sed -i 's/utf8mb4_0900_ai_ci/utf8mb4_general_ci/g' your_dump_file.sql
sed -i 's/utf8mb4_0900_bin/utf8mb4_bin/g' your_dump_file.sql

After running the above commands, try re-importing the database.

Option 4: Update MySQL Version
If possible, you can upgrade your MySQL server to version 8.0 or higher, which supports the utf8mb4_0900_ai_ci collation. This would resolve the issue without needing to modify the dump file.

Option 5: Force a Different Collation During Import
You can force a different collation by setting the default collation when importing:

SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;

Run this command in the MySQL shell or as part of your import process before importing the dump file. This forces MySQL to use a different collation during the import.

Conclusion
- If you have control over the export process, re-export the database using an older collation.
- If you're stuck with an existing dump file, either modify the file or update your MySQL version to support the newer collation.

Let me know if you need further clarification on any of these steps!

Thanks for visit my website