Mysql Modify the database code for UTF8 to avoid garbled problems _mysql

Source: Internet
Author: User
MySQL to create a database when the specified encoding is important, many developers have used the default encoding, garbled problem is impossible to guard against. The coding of the database can avoid the garbled problem caused by the pouring of the export to a large extent.

Web page data is generally UTF8 encoded, and the database defaults to Latin. We can modify the database default encoding for UTF8 to reduce the database creation settings, but also to maximize the avoidance of careless caused by the garbled problem.

The standard we follow is that the coding of databases, tables, fields and pages or text should be unified
We can view the current encoding of the database by command: mysql> show VARIABLES like ' character% ';
Found that a lot of the corresponding is latin1, our goal is to use this command next time latin1 can be replaced by UTF8.

First Stage:
MySQL Set encoding command
Copy Code code as follows:

SET character_set_client = UTF8;
SET character_set_connection = UTF8;
SET character_set_database = UTF8;
SET character_set_results = UTF8;
SET character_set_server = UTF8;

Then mysql> show VARIABLES like ' character% '; You can see it all into UTF8.
But it's just a sham.

This method is only valid in the current state and is invalidated when the database service is restarted.
So if you want to get garbled, just modify the My.ini file,
From My.ini (no additions under the label, some modifications)
[Client]
Default-character-set=utf8
[MySQL]
Default-character-set=utf8
[Mysqld]
Default-character-set=utf8
The above 3 section all must add Default-character-set=utf8, usually we may only add mysqld one item.
Then restart MySQL, execute
Mysql> show VARIABLES like ' character% ';
Make sure all the value items are UTF8.
But the damned thing came again,
|character_set_client | UTF8 |
| character_set_connection | UTF8 |
| Character_set_database | UTF8 |
| Character_set_filesystem | binary |
| Character_set_results | UTF8 |
| Character_set_server | Latin1 |
| Character_set_system | UTF8 Note this configuration | Character_set_server | Latin1 can not be set to UTF8 interaction will still appear garbled.

The second stage: find the following stuff
X:\%path%\mysql\mysql Server 5.0\bin\mysqlinstanceconfig.exe
Restart the settings to set the default encoding to UTF8. This will achieve the desired effect.
Mysql> show VARIABLES like ' character% ';
+--------------------------+---------------------------------------------------------+
| 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 | C:\Program Files\mysql\mysql Server 5.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 Rows in Set
Additional precautions:

1, add UTF8, table field collation can add, not overtime default is Utf8_general_ci.
Copy Code code as follows:

CREATE TABLE ' Tablename4 ' (
' id ' int (one) not NULL auto_increment,
' varchar1 ' varchar (255) DEFAULT NULL,
' Varbinary1 ' varbinary (255) DEFAULT NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8

2, the Web page xxx.php/jsp save the choice of UTF8 code, the best page with the first plus
Header (' Conten-type:text/html;charset=utf-8 ');
Perform the CRUD operations before performing the
mysql_query ("Set names UTF8");

-------------------------
Connection Database Settings encoding
jdbc:mysql://Address: 3306/database name? Characterencoding=utf8
-------------------------
The common coding UTF-8 in Java; GBK; gb2312;iso-8859-1;
corresponding to the code utf8;gbk;gb2312;latin1 in MySQL database

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.