MySql modifies the database encoding to UTF8

Source: Internet
Author: User

It is very important for MySql to change the database encoding to UTF8 when creating a 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 the database, table, field, and page or text should be unified. We can run the 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 sets the encoding Command [SQL] SET character_set_client = utf8; SET character_set_connection = utf8; SET character_set_database = utf8; SET character = 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 that this method is only valid in the current state and fails when the Database Service is restarted. So if you want to avoid garbled characters, you only need to modify my. INI file, from my. start with ini (add or modify tags) [client] default-character-set = utf8 [mysql] default-character-set = utf8 [mysqld] default-character-set = utf8 must be added to all three sections above the default-character -set = utf8, in normal times, we may only add one mysqld item. Restart mysql and execute mysql> show variables like 'character % '. Make sure all Value items are utf8. But here comes the hateful thing, | bytes | utf8 | character_set_connection | utf8 | character_set_database | utf8 | bytes | binary | bytes | utf8 | bytes | latin1 | character_set_system | utf8 pay attention to this Configuration | bytes | when latin1 cannot be set to UTF8 for interaction, garbled characters still occur. Stage 2: Find the following things X: \ % path % \ MySQL Server 5.0 \ bin \ MySQLInstanceConfig.exe and restart the settings, set the default encoding to utf8. this will achieve the desired effect. Mysql> show variables like 'character % '; + bytes + | Variable_name | Value | + bytes + | character_set_client | utf8 | character_set_connection | utf8 | character_set_database | utf8 | character_set_filesystem | binary | u | Tf8 | character_set_server | utf8 | character_set_system | utf8 | character_sets_dir | C: \ Program Files \ MySQL Server 5.0 \ share \ charsets \ | + ------------------------ + rows + 8 rows in set other considerations: 1. Add utf8 when creating a table, the Collation of table fields can be added or 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. webpage xxx. when saving php/jsp files, select utf8 encoding. It is best to add the header ('conten-type: text/html; charset = UTF-8 ') to the page header '); before performing the CRUD operation, execute mysql_query ("set names utf8"); ----------------------- set the Connection database encoding jdbc: mysql: // address: 3306/Database Name? CharacterEncoding = utf8 ----------------------- common coding UTF-8 in java; GBK; GB2312; ISO-8859-1; utf8; gbk; gb2312; latin1

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.