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

Why is it not recommended to use an empty string as a className in Vue?

Table of contents Compare the empty string '&...

Web design must also first have a comprehensive image positioning of the website

⑴ Content determines form. First enrich the conten...

JavaScript Composition and Inheritance Explained

Table of contents 1. Introduction 2. Prototype ch...

N ways to center elements with CSS

Table of contents Preface Centering inline elemen...

Tutorial on installing MySQL database and using Navicat for MySQL

MySQL is a relational database management system ...

Analysis and solutions to problems encountered in the use of label tags

I used the label tag when I was doing something re...

How to insert batch data into MySQL database under Node.js

In the project (nodejs), multiple data need to be...

How to dynamically add ports to Docker without rebuilding the image

Sometimes you may need to modify or add exposed p...

Perfect solution for theme switching based on Css Variable (recommended)

When receiving this requirement, Baidu found many...

MySQL gets the current date and time function

Get the current date + time (date + time) functio...

HTML embed tag usage and attributes detailed explanation

1. Basic grammar Copy code The code is as follows...

JavaScript canvas to achieve mirror image effect

This article shares the specific code for JavaScr...

Description of meta viewport attribute in HTML web page

HTML meta viewport attribute description What is ...

Nginx memory pool source code analysis

Table of contents Memory Pool Overview 1. nginx d...