Solution of mysql Garbled Code Problem under linux

  • 2021-11-29 16:43:55
  • OfStack

The project proceeds to interact with the server, accessing the server-side jsp through post, accessing the server-side mysql database through jsp, and finally returning to the client with garbled Chinese.

In the whole process, there may be three reasons for errors: post does not set codes or codes do not match, jdbc has problems, and mysql initial code system under linux has problems.

After tedious investigation, the problem was finally determined as mysql coding problem. The following describes how to solve the problem of mysql Chinese garbled code under linux.

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

If the display 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/ |

It has been modified correctly, and the default initial setting of mysql is latin1 instead of utf8.

One workaround is to change the table's properties to utf8 or add the DEFAULT CHARSET=utf8 . Set the table to utf 8. Such a method may fail.

The most fundamental solution is to open the mysql configuration file for modification. Under linux, the name of the mysql configuration file is my. cnf, and the directory is/etc/my. cnf. After opening, follow the following operations:


-- In  [mysqld]  Add under the label 3 Row 
default-character-set = utf8
character_set_server = utf8
lower_case_table_names = 1 // Table names are not case sensitive (this is encoding independent) 
-- In  [mysql]  Add under the label 1 Row 
default-character-set = utf8
-- In  [mysql.server] Add under the label 1 Row 
default-character-set = utf8
-- In  [mysqld_safe] Add under the label 1 Row 
default-character-set = utf8
-- In  [client] Add under the label 1 Row 
default-character-set = utf8

It doesn't matter if all the above labels can't be found. Open the mysql command line again and execute SHOW VARIABLES LIKE 'character_set_%'; If latin1 still exists, execute the following command from 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;

The target result can be obtained by executing the above show command after execution.

After setup, you need to restart mysql. Restart command/etc/init. d/mysqld restart.

The original data table needs to be deleted and rebuilt.

Finished, done.

Summary

1. Modify the/etc/my. cnf file to 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 mysql service:


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

Summarize


Related articles: