Home > Database > Mysql Tutorial > body text

How to deal with Chinese garbled characters that appear during MySQL installation

王林
Release: 2024-03-01 17:21:04
Original
961 people have browsed it

How to deal with Chinese garbled characters that appear during MySQL installation

When Chinese garbled characters appear when we install MySQL, it is usually caused by incorrect character set settings in the database. In this case, we need to adjust MySQL's character set to ensure that Chinese characters can be stored and displayed correctly. The following will introduce how to deal with Chinese garbled characters that appear during MySQL installation, and provide specific code examples.

1. Check the current character set settings of MySQL

Before solving the problem of Chinese garbled characters in MySQL, you first need to understand the current character set settings of MySQL. You can view it through the following steps:

SHOW VARIABLES LIKE 'character%';
Copy after login

Run the above SQL statement to view the current character set settings of MySQL, including character_set_database, character_set_server, character_set_client, etc.

2. Modify the character set settings of MySQL

If you find that the character set settings of MySQL are incorrect and cause Chinese garbled characters, you need to make corresponding adjustments. The following are specific code examples:

2.1 Modify the configuration file my.cnf

vim /etc/my.cnf
Copy after login

Add or modify the following content in the configuration file:

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
Copy after login

2.2 Modify the database and table Character set

ALTER DATABASE my_database CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

ALTER TABLE my_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Copy after login

3. Restart the MySQL service

After modifying the character set settings, remember to restart the MySQL service for the changes to take effect:

systemctl restart mysql
Copy after login

4. Test Chinese character storage and Query

Finally, you can test whether MySQL has correctly processed Chinese characters by inserting and querying Chinese characters:

INSERT INTO my_table (column_name) VALUES ('中文测试');

SELECT * FROM my_table WHERE column_name = '中文测试';
Copy after login

Through the above operations, you should be able to solve the Chinese garbled situation that occurs during MySQL installation and ensure that Chinese characters can be stored and displayed correctly. Hope the above code example helps to solve the problem.

The above is the detailed content of How to deal with Chinese garbled characters that appear during MySQL installation. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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