How to Set Up mysql Chinese encoding in linux

Source: Internet
Author: User

In general, we can choose to set mysql encoding during installation, but it is not as convenient as windows in liunx. Next I will introduce how to modify mysql encoding in liunx.

# Vi/etc/my. cnf

Open my. cnf and modify the 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 do not know where MYSQL is, run

The Code is as follows: Copy code
# Whereis mysql

You can find the location where MYSQL is installed.

To check whether the setting is successful, 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 preceding Code settings are successfully displayed.

Add the following statement to the JSP page (preferably all pages) that sends or reads Chinese characters:

The Code is as follows: Copy code
<% @ Page pageEncoding = "gbk" %>
<% @ Page contentType = "text/html; charset = gbk" %>
<% Request. setCharacterEncoding ("gbk"); %>

Add the following to the

The Code is as follows: Copy code
<Meta http-equiv = "Content-Type" content = "text/html; charset = gbk">

Note that the methods for responding to requests in servlet must also include:

The Code is as follows: Copy code
Response. setContentType ("text/html; charset = gbk ");

(If the request sent by firefox is still latin1 for unknown reasons)
Before inserting the request parameters into the database, you must perform another transcoding (convert latin1 encoding to UTF-8), 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, and can be transcoded every time before the request data is inserted.

Related Article

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.