MySQL
Database Conversion
UTF-8
Character Set
Collation

How to convert an entire MySQL database characterset and collation to UTF-8?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

With the rise of globalization and the need for internationalization in applications, using a consistent character encoding like UTF-8 in databases has become increasingly important. UTF-8 is a variable-width character encoding capable of encoding all possible Unicode code points, making it ideal for storing text data from different languages. This article details how to convert an entire MySQL database's character set and collation to UTF-8, providing technical explanations, examples, and additional considerations.

Understanding MySQL Character Sets and Collations

Character Sets

A character set in MySQL defines the set of characters that can be stored in a table or database. MySQL supports multiple character sets with UTF-8 being one of the most prevalent due to its compatibility with international characters.

Collations

Collation in MySQL is a set of rules used to compare characters in a character set. It determines how string comparison is performed, which impacts ordering and uniqueness of text data.

UTF-8 has a default collation of utf8_general_ci, which is case-insensitive and sufficient for most use cases. However, for more precise linguistic requirements, utf8_unicode_ci may be used.

Steps to Convert Database to UTF-8

Preliminary Considerations

Before you begin converting character sets and collations, ensure to backup your data. Converting might lead to data loss or corruption if not done correctly.

Step 1: Check Current Character Set and Collation

To inspect the character set and collation of the database, tables, and columns, run the following queries:

sql
1-- Check database character set and collation
2SHOW VARIABLES LIKE 'character_set_database';
3SHOW VARIABLES LIKE 'collation_database';
4
5-- Check table character set and collation
6SHOW TABLE STATUS;
7
8-- Check column character set and collation
9SELECT TABLE_NAME, COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME 
10FROM information_schema.COLUMNS 
11WHERE TABLE_SCHEMA = 'your_database_name';

Step 2: Alter Database Character Set and Collation

To change the entire database character set to UTF-8, execute:

sql
ALTER DATABASE your_database_name 
CHARACTER SET utf8mb4 
COLLATE utf8mb4_general_ci;

Note: Consider using utf8mb4, which is a superset of UTF-8 in MySQL, supporting four-byte Unicode characters.

Step 3: Alter Tables and Columns

After setting the database character set, tables and columns should also be converted. The following script alters each table and column. You may need to loop through these for all tables and columns:

sql
1-- Alter all tables
2ALTER TABLE your_table_name 
3CONVERT TO CHARACTER SET utf8mb4 
4COLLATE utf8mb4_general_ci;

Example Script

Here is an example script you might use for automating this process:

sql
1-- Example script using a stored procedure
2DELIMITER //
3CREATE PROCEDURE ConvertToUtf8mb4()
4BEGIN
5  DECLARE done INT DEFAULT 0;
6  DECLARE tbl VARCHAR(255);
7  DECLARE cur CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema = 'your_database_name';
8
9  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
10
11  OPEN cur;
12
13  read_loop: LOOP
14    FETCH cur INTO tbl;
15    IF done THEN
16      LEAVE read_loop;
17    END IF;
18
19    SET @stmt = CONCAT('ALTER TABLE ', tbl, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci');
20    PREPARE stmt FROM @stmt;
21    EXECUTE stmt;
22    DEALLOCATE PREPARE stmt;
23  END LOOP;
24
25  CLOSE cur;
26END //
27DELIMITER ;

Run this stored procedure to perform the conversion across all tables.

Post-Conversion Actions

Verifying Changes

After conversion, verify character sets and collations have been correctly updated:

sql
1-- Verify again to confirm changes
2SHOW VARIABLES LIKE 'character_set_database';
3SHOW TABLE STATUS;
4SELECT TABLE_NAME, COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME 
5FROM information_schema.COLUMNS 
6WHERE TABLE_SCHEMA = 'your_database_name';

Application Logic Adjustments

Ensure your application's configurations are set to use UTF-8, often involving altering connection string settings or ensuring character set declarations are consistent.

Backup and Testing

Regular backups can't be overstated. After converting, deploy extensive testing to check data integrity, and ensure that applications behave as expected.

Key Points Summary

TopicDescription
Character SetDefines the symbols and characters that can be stored.
CollationSpecifies rules for string comparison.
UTF-8/UTF-8mb4Recommended for international text data.
BackupAlways backup data before changes.
VerificationVerify changes to ensure success.
TestingComprehensive testing post-conversion is crucial.

Conclusion

Migrating your MySQL database to use UTF-8 character sets and collations enhances its compatibility with global text data, ensuring your applications remain robust and universally usable. By following the structured steps and considering post-conversion tasks, you can perform this migration smoothly with minimal disruption.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.