How to solve the issue of garbled characters when importing a SQL file in MySQL?
The reason for the problem of garbled characters usually lies in the inconsistencies between the encoding of the imported SQL file and the MySQL database. Here are some common solutions:
- Use the correct encoding format: When exporting SQL files, make sure to use the same encoding format as the MySQL database, typically using UTF-8 encoding format to avoid garbled characters.
- Specify encoding format: you can use the –default-character-set parameter when importing SQL files to specify the encoding format, for example:
mysql -u username -p database_name --default-character-set=utf8 < file.sql
- To change the encoding of the MySQL database: If the above method doesn’t work, you can try changing the encoding format of the MySQL database to UTF-8 by modifying the configuration in the my.cnf file.
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
- Use a tool to convert the encoding: You can use third-party tools like Notepad++ to convert the encoding format of the SQL file to UTF-8 before importing it again.
By following the above methods, the issue of MySQL importing SQL files with garbled characters should be resolved. If the problem persists, it is recommended to check the encoding format of the SQL file and the MySQL database to see if they are consistent, and make adjustments accordingly.