What is a character set?
A character set is the code a database uses to store letters and symbols. Think of it as a secret alphabet book. If the export uses one book and the import reads with another, letters turn into odd signs like "é". Common sets are utf8mb4 and latin1.
You need SSH access. On a dedicated server you have it. SSH is a safe way to type commands on a remote server.
Step 1: Find the current character set
- Log in to MySQL.
This opens MySQL and asks for the password.mysql -u root -p - Check the database.
This shows the set for the database namedSELECT DEFAULT_CHARACTER_SET_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = 'mydb';mydb. - Type
exitto leave MySQL.
Step 2: Export
- Run this command with your own names.
This saves the database to a file calledmysqldump -u root -p --default-character-set=utf8mb4 mydb > mydb.sqlmydb.sql, using utf8mb4. Use the same set you found in step 1 if it is different, such aslatin1.
Step 3: Import
- Make an empty database with the right set.
This makesmysql -u root -p -e "CREATE DATABASE newdb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"newdb. A collate is the rule for sorting and comparing letters. - Import the file.
This loads the saved data intomysql -u root -p --default-character-set=utf8mb4 newdb < mydb.sqlnewdb.
Warning: Importing into a database that already has tables can overwrite them. Take a backup of the target first. Never run these steps on a live site without a copy.
Check the result
Open your website or run a SELECT on a table with special letters. If you see odd signs, import again with the correct set. Make sure the set in your site's config file matches too.
Quick recap
- A character set decides how letters are stored.
- Use the same set when you export and when you import.
- Use
--default-character-setwithmysqldumpandmysql. - Back up the target database before importing.