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

How to connect to a remote server and transfer files via a jump server in Linux

Recently, I encountered many problems when deploy...

How to configure domestic sources in CentOS8 yum/dnf

CentOS 8 changed the software package installatio...

Mysql splits string into array through stored procedure

To split a string into an array, you need to use ...

Detailed explanation of several examples of insert and batch statements in MySQL

Table of contents Preface 1.insert ignore into 2....

Detailed explanation of how to view the number of MySQL server threads

This article uses an example to describe how to v...

Mysql master/slave database synchronization configuration and common errors

As the number of visits increases, for some time-...

Detailed tutorial on how to delete Linux users using userdel command

What is serdel userdel is a low-level tool for de...

Comprehensive summary of mysql functions

Table of contents 1. Commonly used string functio...

HTML+VUE paging to achieve cool IoT large screen function

Effect demo.html <html> <head> <me...

MySQL triggers: creating multiple triggers operation example analysis

This article uses an example to describe the crea...

Getting Started: A brief introduction to HTML's basic tags and attributes

HTML is made up of tags and attributes, which are...

Hide div in HTML Hide table TABLE or DIV content css style

I solved a problem tonight that has been botherin...