How does MySQL change the default character set? What is the underlying principle?

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:

  1. Open the MySQL configuration file my.cnf. This file is usually located at /etc/mysql/my.cnfor /etc/my.cnf, depending on the operating system and how MySQL was installed.

  2. 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
    

  3. The above example sets the character set to utf8mb4, and the collation to utf8mb4_unicode_ci. You can select the appropriate character set and collation according to your needs.

  4. Save and close the configuration file.

  5. Restart the MySQL service for the changes to take effect. Depending on the operating system, you can use the command sudo service mysql restartor sudo systemctl restart mysqlto restart MySQL.

  6. 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.

  7. Collation: A collation defines how characters are sorted and compared. Different collations can affect string comparison results and sort order. For example, utf8_general_ciis a case-insensitive collation, utf8_binbut a case-sensitive collation.

  8. 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.

Guess you like

Origin blog.csdn.net/qq_36777143/article/details/131167714