Home > Database > Mysql Tutorial > How to modify the mysql character set

How to modify the mysql character set

WBOY
Release: 2023-05-26 19:31:04
forward
2509 people have browsed it

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)
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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)
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template