MySql modifies the database encoding to UTF8 to avoid gibberish _ MySQL

Source: Internet
Author: User
It is important for MySql to modify the database encoding to UTF8 to avoid gibberish. bitsCN. commysql to specify the encoding when creating the database. many developers use the default encoding, which is difficult to prevent. Coding databases can avoid garbled characters caused by import and export.

Webpage data is generally UTF-8 encoded, and the default database is latin. We can modify the default encoding mode of the database to UTF8 to reduce the settings during database creation, and avoid garbled code problems caused by carelessness to the maximum extent.

The standard we follow is that the encoding of databases, tables, fields, and pages or texts should be unified.
We can run the command to view the current database code: mysql> show variables like 'character % ';
We found that many of the corresponding tasks are latin1. our goal is to replace latin1 with UTF8.

Stage 1:
Mysql encoding command

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 that all are changed to utf8.
However, this is just an illusion

This method is valid only in the current status and fails when the database service is restarted.
If you want to avoid garbled characters, you must modify the my. ini file,
Start with my. ini (add or modify the tags)
[Client]
Default-character-set = utf8
[Mysql]
Default-character-set = utf8
[Mysqld]
Default-character-set = utf8
Default-character-set = utf8 must be added to all the preceding three sections. Normally, only mysqld is added.
Restart mysql and run
Mysql> show variables like 'character % ';
Make sure that all Value items are utf8.
But here comes the hateful thing,
| 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 pay attention to this configuration | character_set_server | garbled characters still occur when latin1 cannot be set to UTF8 for interaction.

Stage 2: find the following
X:/% path %/MySQL Server 5.0/bin/MySQLInstanceConfig.exe
Restart the settings and 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 Server 5.0/share/charsets/|
+ -------------------------- + --------------------------------------------------------- +
8 rows in set
Note:

1. add utf8 to the table when creating the table. the Collation of the table field can be added but not added. The default value is utf8_general_ci.

Create table 'tablename4 '(
'Id' int (11) not null AUTO_INCREMENT,
'Varchar1' varchar (255) default null,
'Varbinary1 'varbinary (255) default null,
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8

2. select utf8 encoding when saving the xxx. php/jsp page.
Header ('conten-type: text/html; charset = utf-8 ');
Execute the CRUD operation first.
Mysql_query ("set names utf8 ");

-------------------------
Connect to the database and set the encoding
Jdbc: mysql: // address: 3306/database name? CharacterEncoding = utf8
-------------------------
Common coding UTF-8 in java; GBK; GB2312; ISO-8859-1;
Utf8, gbk, gb2312, and latin1bitsCN.com

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.