MySQL encoding settings

Source: Internet
Author: User

mysql> SHOW VARIABLES like ' character_set_% ';
+--------------------------+----------------------------+
| variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | Latin1 |
| character_set_connection | Latin1 |
| Character_set_database | Latin1 |
| Character_set_results | Latin1 |
| Character_set_server | Latin1 |
| Character_set_system | UTF8 |
| Character_sets_dir | /usr/share/mysql/charsets/|
+--------------------------+----------------------------+
7 Rows in Set (0.00 sec)

mysql> SHOW VARIABLES like ' collation_% ';
+----------------------+-------------------+
| variable_name | Value |
+----------------------+-------------------+
| collation_connection | Latin1_swedish_ci |
| Collation_database | Latin1_swedish_ci |
| Collation_server | Latin1_swedish_ci |
+----------------------+-------------------+
3 Rows in Set (0.00 sec)
The default is Swedish latin1, which is replaced by our own code, such as UTF8:
The problem of garbled external access data is on this connection connection layer, the workaround is to execute the following sentence before sending the query:

1.SET NAMES ' UTF8 ';

It is equivalent to the following three-sentence instruction:
SET character_set_client = UTF8;
SET character_set_results = UTF8;
SET character_set_connection = UTF8;

This is usually done only by executing the code before the access, and the following is the creation of the database and the data table, set to our own encoding format.
2. CreateDatabase
MySQL> CREATE database name character set UTF8;

3. Create a table
CREATE TABLE ' type ' (
' id ' int (ten) unsigned not NULL auto_increment,
' Flag_deleted ' enum (' Y ', ' n ') character set UTF8 not NULL default ' N ',
' Flag_type ' int (5) Not NULL default ' 0 ',
' type_name ' varchar (character set UTF8 not NULL default ' ',
PRIMARY KEY (' id ')
) DEFAULT Charset=utf8;

4. Modify The database into UTF8.
mysql> ALTER DATABASE name character set UTF8;

5. Modify the table by default with UTF8.
mysql> ALTER TABLE type character set UTF8;

6. Modify the field with UTF8
mysql> ALTER TABLE type modify type_name varchar (CHARACTER SET UTF8;

MySQL encoding settings

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.