Step one: Check the current character set
First you need to check the current MySQL character set. You can use the following command:
mysql> show variables like 'character_set_database'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | character_set_database | utf8 | +------------------------+-------+ 1 row in set (0.00 sec) mysql> show variables like 'character_set_server'; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | character_set_server | utf8 | +----------------------+-------+ 1 row in set (0.00 sec)
Two variables are shown here. character_set_database
represents the character set of the database, character_set_server
represents the character set of the server. You can see that the current character set of MySQL is UTF-8.
Step 2: Modify the character set
You can modify the character set in the MySQL configuration file my.cnf. Use the following command to open the my.cnf file usually located in /etc/my.cnf:
sudo vim /etc/my.cnf
Find the character set-related configuration in the file, which can be adjusted according to your needs:
[mysql] default-character-set = utf8 [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_general_ci [client] default-character-set = utf8
Some commonly used The character set configuration has been set here, including the default character set, server character set, client character set, etc. Adjust according to your needs.
After the modification is completed, restart the MySQL service:
sudo service mysql restart
Step 3: Confirm the modification result
After modifying the character set, confirm whether the modification was successful. You can check the character set of MySQL again:
mysql> show variables like 'character_set_database'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | character_set_database | utf8mb4 | +------------------------+-------+ 1 row in set (0.00 sec) mysql> show variables like 'character_set_server'; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | character_set_server | utf8mb4 | +----------------------+-------+ 1 row in set (0.00 sec)
From the perspective of successful modification, the character set has been changed to UTF-8MB4 (applicable to MySQL version 5.5.3 and above), which is already obvious.
The above is the detailed content of How to modify the mysql character set. For more information, please follow other related articles on the PHP Chinese website!