Solution to MySQL garbled code problem under Linux

Solution to MySQL garbled code problem under Linux

The project interacts with the server, accesses the server-side jsp through post, and jsp accesses the server-side mysql database. Finally, the Chinese text returned to the client is garbled.

In the whole process, there may be three reasons for the error: the post encoding is not set or the encoding is inconsistent, there is a problem with jdbc, and there is a problem with the initial encoding of mysql under linux.

After tedious troubleshooting, it was finally determined that the problem was a MySQL encoding problem. The following describes how to solve the MySQL Chinese garbled problem under Linux.

First enter the mysql command line mode and type mysql -uroot -p to enter. Then type SHOW VARIABLES LIKE 'character_set_%';

If the display content is similar to this:

+--------------------------+----------------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /alidata/server/mysql-5.1.73/share/charsets/ |

The modification has been correct, and the default initial setting of MySQL is latin1 instead of utf8.

One solution is to change the table's properties to utf8 or add DEFAULT CHARSET=utf8 at the end when creating the table. Set the table to utf8 code. This approach may fail.

The most fundamental solution is to open the MySQL configuration file and modify it. The MySQL configuration file under Linux is named my.cnf and the directory is /etc/my.cnf. After opening it, follow the steps below:

--Add three lines under the [mysqld] tag: default-character-set = utf8
character_set_server = utf8
lower_case_table_names = 1 //Table names are case insensitive (this has nothing to do with encoding)
--Add a line under the [mysql] tag: default-character-set = utf8
--Add a line default-character-set = utf8 under the [mysql.server] tag
--Add a line default-character-set = utf8 under the [mysqld_safe] tag
--Add a line default-character-set = utf8 under the [client] tag

It doesn't matter if you can't find all of the above tags. Open the MySQL command line again and execute SHOW VARIABLES LIKE 'character_set_%'; If latin1 still exists, execute the following command in the MySQL command line:

  • set character_set_client = utf8;
  • set character_set_server = utf8;
  • set character_set_connection = utf8;
  • set character_set_database = utf8;
  • set character_set_results = utf8;
  • set collation_connection = utf8_general_ci;
  • set collation_database = utf8_general_ci;
  • set collation_server = utf8_general_ci;

After executing, re-execute the above show command to get the target result.

After the settings are complete, you need to restart mysql, restart command /etc/init.d/mysqld restart.

The original data table needs to be deleted and rebuilt.

Finally completed, done.

summary

1. Modify the /etc/my.cnf file and add the following lines:

[client]
# pipe=
# socket=MYSQL
port=3306
default-character-set=utf8
[mysql]
no-beep
# default-character-set=
default-character-set=utf8
#SERVER SECTION
# ----------------------------------------------------------------------
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
# server_type=3
[mysqld]
character_set_server=utf8

2. Restart the mysql service:

service mysql stop;
service mysql status;
service mysql start;
Or service mysql restart;

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Example solution for writing garbled Chinese characters into MySQL in PHP
  • MySQL character set garbled characters and solutions
  • How to solve the problem of Chinese garbled characters when inserting table data into MySQL
  • Summary of handling JDBC connection mysql garbled code exception problem
  • Detailed explanation of the solution to garbled characters when JDBC connects to MySQL to process Chinese
  • Detailed explanation of the Chinese garbled characters problem in MySQL database
  • Solve the problem of garbled data in MySQL database migration

<<:  A brief analysis of how to set the initial value of Linux root

>>:  Vue implements simple production of counter

Recommend

Detailed tutorial on installing centos8 on VMware

CentOS official website address https://www.cento...

What are your principles for designing indexes? How to avoid index failure?

Table of contents Primary key index Create indexe...

SQL implementation of LeetCode (197. Rising temperature)

[LeetCode] 197.Rising Temperature Given a Weather...

How to output Chinese characters in Linux kernel

You can easily input Chinese and get Chinese outp...

Detailed installation and use of docker-compose

Docker Compose is a Docker tool for defining and ...

The pitfalls and solutions caused by the default value of sql_mode in MySQL 5.7

During normal project development, if the MySQL v...

Do not start CSS pseudo-class names with numbers

When newbies develop div+css, they need to name t...

WeChat applet implements search box function

This article example shares the specific code for...

Turn off the AutoComplete function in the input box

Now we can use an attribute of input called autoco...

Differences and comparisons of storage engines in MySQL

MyISAM storage engine MyISAM is based on the ISAM...

Windows Server 2016 Quick Start Guide to Deploy Remote Desktop Services

Now 2016 server supports multi-site https service...