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