Technology Encyclopedia Home >When MariaDB is initialized, the character set and byte count are set. Can they be changed?

When MariaDB is initialized, the character set and byte count are set. Can they be changed?

Yes, the character set and byte count (collation) in MariaDB can be changed after initialization, but the process depends on whether you're modifying the server defaults, a specific database, table, or column. Here's how it works:

1. Changing Server Defaults

To modify the default character set and collation for new databases/tables, update the MariaDB configuration file (my.cnf or my.ini) with:

[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

Restart the server to apply changes. Existing databases/tables retain their original settings.

2. Changing a Database's Character Set

Use the ALTER DATABASE command:

ALTER DATABASE database_name 
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

This affects only new tables created in the database.

3. Changing a Table's Character Set

Use the ALTER TABLE command:

ALTER TABLE table_name 
CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

This converts all columns in the table to the new character set.

4. Changing a Column's Character Set

Specify the character set for a column during creation or alteration:

ALTER TABLE table_name 
MODIFY column_name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Example Scenario

If you initially set latin1 as the default but later need utf8mb4 (e.g., for emoji support), you can convert existing data:

ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Tencent Cloud Recommendation

For managed MariaDB services on Tencent Cloud, use TencentDB for MariaDB. It allows easy configuration of character sets during instance creation or via the console. The service also supports online schema changes with minimal downtime.

Note: Changing character sets may require data conversion, which can impact performance. Always back up data before making changes.