Get the App
SLTechnology News&Howtos  ›  Database  › 

The solution of Chinese garbled code when storing data in mysql

Shulou Source: shulou.com Published: 2022-06-01 04:12:59 09月19日 Update

This article will explain in detail the solution to Chinese garbled code when storing data in mysql. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Solutions to Chinese garbled when mysql stores data: 1. Set the client-related code set [set names gbk;]; 2. In the [my.ini] configuration file, change [default-character-set=utf8] to [= gbk] and restart it.

Solution to Chinese garbled code when storing data in mysql:

The problem lies in the current CMD client window, because the current CMD client input uses GBK encoding, while the database encoding format is UTF-8, which leads to garbled codes due to inconsistent coding.

However, the coding format of the current CMD client can not be modified, so we can only modify the coding set of connection, client and results to tell the server that the data currently inserted is encoded by GBK. Although the database of the server uses UTF-8 coding, it can identify the GBK encoded data of the notification server and automatically convert it into UTF-8 for storage. You can use the following statement to quickly set the client-related code set:

Set names gbk

After the setting is completed, you can solve the problem of garbled code when the client inserts or displays data, but we will soon find that this form of setting will only be valid in the current window, and there will be garbled problem when the CMD client is reopened after the window is closed; so, how to make a setting once and for all?

There is a my.ini configuration file in the MySQL installation directory, and the garbled problem can be solved once and for all by modifying this configuration file. In this configuration file, [mysql] is related to the client configuration and [mysqld] is related to the server configuration. The default configuration is as follows:

[mysql]

Default-character-set=utf8

[mysqld]

Character-set-server=utf8

At this point, you only need to change the default encoding default-character-set=utf8 to default-character-set=gbk, and restart the MySQL service.

This is the end of the solution to Chinese garbled code when saving data in mysql. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Coding data garbled client configuration client service time file server problem end UTF-8 method once and for all method database more format article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft OPPO Reno Shulou Information MariaDB