Linux set MySQL Chinese coding method detailed

Source: Internet
Author: User

#vi/etc/my.cnf

Open MY.CNF Modify Encoding

Vi/etc/my.cnf

Add under [mysqld]

The code is as follows Copy Code

Default-character-set=utf8

or DEFAULT-CHARACTER-SET=GBK.

Add under [client]

Default-character-set=utf8

or DEFAULT-CHARACTER-SET=GBK.


Restart MySQL.

If you don't know where MySQL is, run

The code is as follows Copy Code
#whereis MySQL

You can find where MySQL is installed.

If you want to check if the settings are successful, you can run the following command:

The code is as follows Copy Code

# mysql-u Root-proot
Welcome to the MySQL Monitor. Commands End With; or G.
Your MySQL Connection ID is 2 to server version:5.0.22

Type ' help, ' or ' h ' for help. Type ' C ' to clear the buffer.

Mysql> Show variables like '%char% ';
+--------------------------+----------------------------+
| variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | GBK |
| character_set_connection | GBK |
| Character_set_database | GBK |
| Character_set_filesystem | binary |
| Character_set_results | GBK |
| Character_set_server | GBK |
| Character_set_system | UTF8 |
| Character_sets_dir | /usr/share/mysql/charsets/|
+--------------------------+----------------------------+
8 rows in Set (0.00 sec)

The above has already shown that the encoding setup was successful.

Add the following statement to the JSP page (preferably all pages) where you want to send Chinese or read Chinese:

The code is as follows Copy Code
<% @page pageencoding= "GBK"%>
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<% request.setcharacterencoding ("GBK"); %>

Add the following in the

The code is as follows Copy Code
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK ">

Note that the same method of responding to requests in the servlet is:

The code is as follows Copy Code
Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");

(This step is performed because the request that Firefox sends is still latin1 for unknown reasons)
Before inserting the requested parameter into the database, another transcoding (converting the latin1 encoding to Utf-8) is performed, for example:

The code is as follows Copy Code
String utf8_str = new String (latin_str. GetBytes ("Latin1"), "GBK");

This step can be written in a filter, not afraid of trouble or you can make a transcoding every time before inserting the request data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.