1. Set the code
LINUX modify vi/etc/my.cnf
WINDOWS my.ini
Add under [client]
default-character-set=utf8
Add under [mysqld]
default-character-set=utf8
Save and restart mysql;
2. Set the character set when creating an object
1 Create a database to specify the character set of the database
mysql>CREATEDATABASE IF NOT EXISTS mydb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
2 Specify the character set when creating the table
CREATETABLE table_name (id int unsigned) DEFAULT CHARACTER SET utf8 COLLATEutf8_general_ci;
3 Specify the character set when creating the field
CREATETABLE table_name2 (id int,name char(10) CHARACTER SET utf8 COLLATE utf8_general_ci);
3. Check the code
1. View MySQL database server and database character set
show variables like'%char%';
2. Check the MySQL database server and database check and sort method (collation rules)
SHOWVARIABLES LIKE 'COLLATION';
3. Check the character set supported by the currently installed MySQL.
show charset;
4. View the current database code:
SHOW CREATE DATABASE db_name;
5. View the table code:
SHOW CREATE TABLE tbl_name;
6. View the field code:
SHOW FULL COLUMNS FROM tbl_name;
Fourth, modify the character set
1. Modify the character set of the database
mysql>usemydb
mysql>ALTERDATABASE mydb CHARACTER SET utf-8(utf8) COLLATE utf8_general_ci
2. Modify the character set of the table
Change the default character set of the table and all character columns (CHAR, VARCHAR, TEXT) to the new character set:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET character_name[COLLATE ...]
如:ALTER TABLE logtest CONVERT TO CHARACTERSET utf8 COLLATE utf8_general_ci;
Just modify the default charset of the table:
ALTER TABLE tbl_name DEFAULT CHARACTER SET character_name[COLLATE...];
如:ALTER TABLE logtest DEFAULT CHARACTERSET utf8 COLLATE utf8_general_ci;
3. Modify the character set of the field:
ALTER TABLE tbl_nameCHANGE c_name c_name CHARACTER SET character_name [COLLATE ...];
如:ALTER TABLE logtest CHANGE title titleVARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci;
Five, use the mysql command to modify the encoding
setcharacter_set_client=utf8; //Client character set
setcharacter_set_connection=utf8; //Link character set
setcharacter_set_database=utf8; //Database character set
setcharacter_set_results=utf8; //result character set
setcharacter_set_server=utf8; //Server character set
set character_set_system=utf8; //system character set
setcollation_connection=utf8; //Link proofreading
setcollation_database=utf8; //Database proofreading
setcollation_server=utf8; //Server collation
setnames character set; also modify character_set_client/character_set_connection/character_set_results character set.
Six, the character set conversion process in MySQL
1. When MySQL Server receives the request, it converts the request data from character_set_client to character_set_connection;
2. Convert the request data from character_set_connection to the character set for internal operations before performing internal operations. The determination method is as follows:
Use the CHARACTER SET setting for each data field;
If the above value does not exist, use the DEFAULT CHARACTER SET setting value of the corresponding data table (MySQL extension, non-SQL standard);
If the above value does not exist, use the DEFAULTCHARACTER SET setting value of the corresponding database;
If the above value does not exist, use character_set_server to set the value.
3. Convert the operation results from the internal operation character set to character_set_results.
Seven, MySQL default character set
MySQL 's character set specification can be refined to a database, a table, a column, and what character set should be used. However, traditional programs do not use such complex configurations when creating databases and data tables, they use the default configuration
So, where does the default configuration come from?
(1) When compiling MySQL, a default character set is specified, which is latin1;
(2) When installing MySQL, you can specify a default character set in the configuration file (my.ini), if not specified, this value is inherited from the one specified at compile time;
(3) When starting mysqld, you can specify a default character set in the command line parameters. If not specified, this value is inherited from the configuration in the configuration file, and character_set_server is set to this default character set at this time;
(4) When creating a new database, unless explicitly specified, the character set of this database is set to character_set_server by default;
(5) When a database is selected, character_set_database is set as the default character set of the database;
(6) When a table is created in this database, the default character set of the table is set to character_set_database, which is the default character set of this database;
(7) When setting a column in the table, unless explicitly specified, the default character set of this column is the default character set of the table;
8. Other matters needing attention
MySQL 's character set support (Character SetSupport) has two aspects: character set (Character set) and sorting method (Collation). The support for character sets is refined to four levels: server (server), database (database)
, data table (table) and connection (connection).
The default_character_set setting in my.cnf only affects the connection character set when the mysql command connects to the server, and will not have any effect on applications using the libmysqlclient library!
SQL function operations on fields are usually performed in the internal operation character set and are not affected by the connection character set setting.
If nothing is modified, then all fields of all tables in all databases are stored in latin1, but if we install MySQL, we generally choose multi-language support, that is to say, the installation program will automatically put in the configuration file default_character_set is set to UTF-8, which ensures that by default, all fields of all tables in all databases are stored in UTF-8.
If you want to batch modify the character set of tables and fields, you can use the following command to generate a sql file, and then execute
SELECT
CONCAT('alter table ',table_name,' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;')
FROM
INFORMATION_SCHEMA.tables
WHERE
TABLE_SCHEMA='database_name' into outfile '/mysql/1.sql';
Set the server character set
set @@character_set_server='utf8';
Reference: http://blog.csdn.net/flcandclf/article/details/24769599 , http://bbs.csdn.net/topics/340027990