MySql modifies the database encoding to UTF8 bitsCN.com
MySql modifies the database encoding to UTF8
It is very important to specify the encoding when creating a mysql 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.
You can run the following command to view the current encoding of the database:
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
[SQL]
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
Note this configuration | character_set_server | latin1 cannot be set to UTF8. garbled characters still occur during 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.
[SQL]
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;
Corresponds to the UTF-8, gbk, gb2312, and latin1 codes in the mysql database.
BitsCN.com