Home  >  Article  >  Database  >  mysql character set modification

mysql character set modification

PHPz
PHPzOriginal
2023-05-08 12:43:371170browse

MySQL is a commonly used relational database management system that supports multiple character sets, such as UTF-8, GBK, etc. When using MySQL, sometimes the character set needs to be modified to meet specific needs. This article will introduce how to modify the character set in MySQL.

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. Usually the my.cnf file is located in /etc/my.cnf. You can use the following command to open it:

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

here Some commonly used character set configurations are set, such as default character set, server character set and client character set. 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)

You can see that the character set has been modified to UTF-8MB4 (supported by versions after MySQL 5.5.3), indicating that the modification is successful.

Conclusion

It is sometimes necessary to modify the MySQL character set. Different character sets need to be used in different scenarios. This article describes how to modify the MySQL character set. Although the location and content of the configuration files may vary, the basic idea of ​​modifying the character set is the same. You need to be careful when modifying the character set to avoid unnecessary impact on the system.

The above is the detailed content of mysql character set modification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Install mysql under linuxNext article:Install mysql under linux