Detailed explanation of the Chinese garbled characters problem in MySQL database

Detailed explanation of the Chinese garbled characters problem in MySQL database

When insert into employee values(null,'張三','female','1995-10-08','2015-11-12','Sales',2000,'是個好員工!') ; and garbled characters appear, you can use the statement show variables like 'character%' ; to view the relevant encoding set of the current database.

From the above figure, you can see that MySQL uses character sets in six places: client, connection, database, results, server, and system. Among them, those related to the server side: database, server, system (can never be modified, it is utf-8); those related to the client side: connection, client, results.

client
The character set used by the client.
connection
Set the character set type for connecting to the database. If the program does not specify the character set type used to connect to the database, the default character set on the server is used.
database
The character set used by a library in the database server. If it is not specified when the library is created, the character set specified when the server is installed will be used.
results
The character set used by the database when returning data to the client. If not specified, the server's default character set is used.
server
The default character set specified when the server is installed.
system
The character set used by the database system.

After understanding the above information, let's analyze the cause of the garbled characters. The problem lies in the current CMD client window, because the current CMD client input uses GBK encoding, and the database encoding format is UTF-8. The inconsistent encoding leads to garbled characters. The current encoding format of the CMD client cannot be modified, so the only way is to modify the encoding set of connection, client, and results to inform the server that the currently inserted data uses GBK encoding. Although the server's database uses UTF-8 encoding, it can recognize the GBK encoded data notified to the server and automatically convert it to UTF-8 for storage. You can use the following statement to quickly set the encoding set related to the client:

  • set names gbk;

After the settings are completed, the garbled problem of data inserted or displayed on the client can be solved, but we will soon find that this form of setting is only valid in the current window. When the window is closed and the CMD client is reopened, the garbled problem will appear again; so, how to make a once-and-for-all setting? There is a my.ini configuration file in the MySQL installation directory. By modifying this configuration file, the garbled character problem can be solved once and for all. 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 time, you only need to change the default encoding default-character-set=utf8 to default-character-set=gbk and restart the MySQL service.

Summarize

The above is the Chinese garbled characters problem of MySQL database introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Solution to Chinese garbled characters when connecting Java to Oracle database
  • Solution to the Chinese garbled code problem in Mac Mysql database
  • Springboot and database return data Chinese garbled

<<:  Solution to the problem of being unable to access the Internet after Ubuntu restarts in VMWare

>>:  Mini Program implements custom multi-level single-select and multiple-select

Recommend

Implementation of multiple instances of tomcat on a single machine

1. Introduction First of all, we need to answer a...

JavaScript anti-shake and throttling detailed explanation

Table of contents Debounce Throttle Summarize Deb...

How to install MySQL 8.0.17 and configure remote access

1. Preparation before installation Check the data...

js to realize a simple disc clock

This article shares the specific code of js to im...

How to use skeleton screen in vue project

Nowadays, application development is basically se...

Notes on using the blockquote tag

<br />Semanticization cannot be explained in...

Implementing a table scrolling carousel effect through CSS animation

An application of CSS animation, with the same co...

In-depth analysis of MySQL explain usage and results

Preface In daily work, we sometimes run slow quer...

How to install the latest version of docker using deepin apt command

Step 1: Add Ubuntu source Switch to root su root ...