Mysql encoding settings, mysql Encoding
Mysql encoding settings
MYSQL read 73 comments 1 font size: large, medium, small
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 value is latin1 in Sweden, which is replaced with our own encoding, such as utf8:
The problem of garbled external access data lies in the connection layer. The solution is to execute the following statement before sending the query:
1. set names 'utf8 ';
It is equivalent to the following three commands:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
Generally, the problem is solved only by executing this code before access. The following describes how to create a database and a data table and set it to our own encoding format.
2. Create a database
Mysql> create database name character set utf8;
3. Create a table
Create table 'type '(
'Id' int (10) 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 (50) character set utf8 not null default '',
Primary key ('id ')
) Default charset = utf8;
4. Modify the database to utf8.
Mysql> alter database name character set utf8;
5. Use utf8to modify the table by default.
Mysql> alter table type character set utf8;
6. Use utf8 to modify Fields
Mysql> alter table type modify type_name varchar (50) character set utf8;
========================================================== ======================================
?
I recently started using MySql. I used Oracle in the past. It's too expensive.
Encoding is the most difficult problem for MySql. I have studied it and summarized some of the results from other people's experience. If there is anything wrong with coding, please join us...
Procedure:
1. Edit the MySql configuration file
MySql configuration file in Windows is generally in the system directory or in the MySql installation directory named my. ini, you can search, Linux is usually/etc/my. cnf
-- Add three lines under the [mysqld] label
Default-character-set = utf8
Character_set_server = utf8
Lower_case_table_names = 1 // The table name is case-insensitive (this is irrelevant to the encoding)
-- Add a line under the [mysql] tag
Default-character-set = utf8
-- Add a line under the [mysql. server] label
Default-character-set = utf8
-- Add a line under the [mysqld_safe] label
Default-character-set = utf8
-- Add a line under the [client] tag
Default-character-set = utf8
Ii. Restart the MySql Service
For Windows, you can operate in the service manager or use the command line:
Net stop mysql press ENTER
Net start mysql press ENTER
The service name may not be set to mysql. Set the service name as per your needs.
In Linux, service mysql restart is used.
If the startup fails, check whether the configuration file is set incorrectly.
?
?
3. view the setting result
Log on to the MySql command line Client: Open the command line
Mysql-uroot-p press ENTER
Enter Password
After entering mysql, run: show variables like "% char % ";
The display result should be similar to the following:
| Character_set_client | utf8 |
| Character_set_connection | utf8 |
| Character_set_database | utf8 |
| Character_set_results | utf8 |
| Character_set_server | utf8 |
| Character_set_system | utf8 |
| Character_sets_dir |/usr/share/mysql/charsets/|
If the encoding is not utf8, check the configuration file or use the mysql command to set it:
Set character_set_client = utf8;
Set character_set_server = utf8;
Set character_set_connection = utf8;
Set character_set_database = utf8;
Set character_set_results = utf8;
Set collation_connection = utf8_general_ci;
Set collation_database = utf8_general_ci;
Set collation_server = utf8_general_ci;
Some of the above commands are only valid for the current logon, so they are not very useful.
4. Create a database and import data
Before importing an SQL script file, make sure that the script file and content are in UTF-8 encoding format,
Log on to the mysql command line in the same way as above, and use the database name to enter the corresponding database
Set names utf8;
Source SQL script file name;
5. Program connection string (this item is irrelevant to mysql settings and used for Program Development)
For drivers of older jdbc versions, the following similar format can be used for connection character creation:
Jdbc: mysql: /// 127.0.1: 3306/test? UseUnicode = true & characterEncoding = UTF-8
?
?
Vi. Appendix
If you cannot change the database configuration file, you can use the following method (not necessarily all files are valid ):
1. Set the database encoding to UTF-8 during database creation
For example, create database 'test' default character set utf8;
2. When importing Database SQL, make sure that the SQL file is UTF-8 encoded.
Enter the mysql command line and enter set names utf8;
Then go to the database use test;
Import the SQL script source test. SQL;
3. The connection string is similar to the following: (development-related, non-database settings)
Jdbc: mysql: /// 127.0.1: 3306/test? UseUnicode = true & characterEncoding = UTF-8
?
?
?