Solution to the garbled code problem in MySQL 5.x

Solution to the garbled code problem in MySQL 5.x

MySQL is a commonly used open source database software, but it does not seem to be very friendly to first-time users. The default character set in MySQL5.x version is latin1, which is the ISO-8859-1 character set we know. This character set encoding does not include Chinese characters, so when we use it, Chinese characters will appear garbled. This can be solved by modifying the database default character set.

Enter the MySQL command line:

mysql> show variables like '%colla%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | gbk_chinese_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set, 1 warning (0.00 sec)

mysql> show variables like '%char%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

You can see that the default character set is now latin1

Find the MySQL configuration file. The configuration file in Windows is my.ini. My computer is located at C:\ProgramData\MySQL\MySQL Server 5.7. The file that needs to be modified in Linux is my.conf. The specific path depends on your actual installation location. Modify the configuration in the following nodes respectively:

The configuration of the [client] node in the 5.7 I use needs to be added, and the other two nodes [mysql] and [mysql] need to open the comments and change them to utf8.

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysql]
character_set_server=utf8

Then restart MySQL

net stop mysql
net start mysql

Check the character set again, all have been changed to utf8 character set

mysql> show variables like '%colla%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | gbk_chinese_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set, 1 warning (0.00 sec)

mysql> show variables like '%char%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

To ensure encoding consistency, you can also specify the character set when creating a database or table, and specify the connection parameters after the connection string:

?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL 8.0.24 version installation and configuration method graphic tutorial
  • Some improvements in MySQL 8.0.24 Release Note
  • Implementation of MySQL's MVCC multi-version concurrency control
  • The best solution for resetting the root password of MySQL 8.0.23
  • About the configuration problem of MyBatis connecting to MySql8.0 version
  • How to solve the problem that Seata cannot use MySQL 8 version
  • Detailed explanation of DBeaver connecting to MySQL version 8 and above and solving possible problems
  • Bugs encountered when using mybatis-generator with mysql8.0.3 in IDEA
  • Detailed tutorial on installing MySQL 8.0.20 database on CentOS 7
  • Solution to ONLY_FULL_GROUP_BY error in Mysql5.7 and above
  • Solve the installation problem of mysql8.0.19 winx64 version
  • Django 2.2 and PyMySQL version compatibility issues
  • Steps to install MySQL 5.7 in binary mode and optimize the system under Linux
  • Installation of various versions of MySQL 8.0.18 and problems encountered during installation (essence summary)
  • Super detailed teaching on how to upgrade the version of MySQL

<<:  Vue implements the sample code of adding, deleting, modifying and checking the tree structure

>>:  Detailed explanation of Docker Swarm concepts and usage

Recommend

Quickly learn MySQL basics

Table of contents Understanding SQL Understanding...

How to install MySQL 5.7 on Ubuntu and configure the data storage path

1. Install MySQL This article is installed via AP...

Seven Principles of a Skilled Designer (2): Color Usage

<br />Previous article: Seven Principles of ...

Summary of how to use the MySQL authorization command grant

How to use the MySQL authorization command grant:...

JavaScript timer to achieve limited time flash sale function

This article shares the specific code of JavaScri...

How to make an input text box change length according to its content

First: Copy code The code is as follows: <input...

js to realize a simple disc clock

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

Using Docker+jenkins+python3 environment to build a super detailed tutorial

Preface: After the automation is written, it need...

Use overflow: hidden to disable page scrollbars

Copy code The code is as follows: html { overflow...

HTML table only displays the outer border of the table

I would like to ask a question. In Dreamweaver, I...

Implementation of sharing data between Docker Volume containers

What is volume? Volume means capacity in English,...

Two ways to achieve horizontal arrangement of ul and li using CSS

Because li is a block-level element and occupies ...