To change MySQL's default character set, it can be done with the following steps:
By changing MySQL's default character set, you can ensure that the database correctly handles and stores data in different languages and character sets. This avoids problems such as garbled characters, truncated characters, or misordering.
By changing the default character set of MySQL, you can ensure that the database can correctly store and process various characters and language data. The underlying principles involve the following aspects:
-
Open the MySQL configuration file
my.cnf
. This file is usually located at/etc/mysql/my.cnf
or/etc/my.cnf
, depending on the operating system and how MySQL was installed. -
Locate the section in the configuration file
[mysqld]
and add or modify the following lines:[mysqld] character_set_server = utf8mb4 collation_server = utf8mb4_unicode_ci
-
The above example sets the character set to
utf8mb4
, and the collation toutf8mb4_unicode_ci
. You can select the appropriate character set and collation according to your needs. -
Save and close the configuration file.
-
Restart the MySQL service for the changes to take effect. Depending on the operating system, you can use the command
sudo service mysql restart
orsudo systemctl restart mysql
to restart MySQL. -
Character set: A character set defines the set of characters that can be used in a database. MySQL supports multiple character sets, such as UTF-8, Latin1, etc. A character set determines which characters a database can store and how they are encoded and decoded.
-
Collation: A collation defines how characters are sorted and compared. Different collations can affect string comparison results and sort order. For example,
utf8_general_ci
is a case-insensitive collation,utf8_bin
but a case-sensitive collation. -
Character encoding: Character encoding is used to map characters to binary data used by computers for storage and transmission. Common character encodings include UTF-8, ASCII, ISO-8859-1, etc. UTF-8 is a widely used multi-byte encoding capable of representing almost all characters in the world.