SoFunction
Updated on 2025-04-07

Modify the implementation of MySQL character set

To permanently modify the values ​​of the three variables, character_set_client, character_set_connection, and character_set_results, so that they use utf8 by default (or better utf8mb4), you usually need to set it in the MySQL client's startup command or configuration file, rather than in the MySQL server configuration file (or the [mysqld] section in    ).

Here are a few modification methods:

1. Modify the MySQL client startup command

When you start the MySQL client from the command line, you can add--default-character-set=utf8(or--default-character-set=utf8mb4)parameter. For example:

mysql --default-character-set=utf8mb4 -u username -p

2. Modify the MySQL client configuration file

If you are using MySQL client configuration file (such as~/./etc/In[client]Part), you can add or modify the following lines:

[client]
default-character-set=utf8mb4

After saving the file, any MySQL client that uses this configuration file will be used by default.utf8mb4Character set.

3. Set up in MySQL session

While this is not permanent, you can manually set these variables in each MySQL session:

SET NAMES 'utf8mb4';

Or, set them separately:

SET character_set_client = 'utf8mb4';
SET character_set_connection = 'utf8mb4';
SET character_set_results = 'utf8mb4';

But please note that these settings only affect the current session.

4. Modify application configuration

If your application is connected to a MySQL database, make sure that the correct character set is specified in the application's connection string or configuration file. This is usually done in the database URL, connection string, or a dedicated database configuration section.

5. Things to note

  • Please note that useutf8mb4Instead ofutf8It ensures that you can store all Unicode characters, including emoji.
  • After modifying the configuration file, you may need to restart the MySQL service or log in to the MySQL client again to make the changes take effect.
  • Before making changes in a production environment, make sure that you have sufficiently tested in the test environment.

This is the end of this article about modifying the implementation of MySQL character set. For more related contents of MySQL character sets, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!