Two solutions for MySQL garbled

Source: Internet
Author: User
Tags mysql commands

We also provide a detailed description of the MySQL garbled problem in the correct solution to the garbled problem in JSP and solve the Chinese garbled problem in tomcat, I believe you will solve some garbled code problems in JSP after reading it, but some MySQL garbled code problems are still not solved.

I have found some other materials, including myself. I hope this will completely solve the problem of garbled characters in the MySQL database.
 

MySQLThe first method of the garbled solution is an incisive summary:

The VM is often changed, and the MySQL versions of different service providers are different. After data is imported, garbled characters and other problems cannot be displayed normally. I checked a lot of information and summarized my skills:

This is the case when importing data in WINDOWS.

Use MySQL commands

Run the doscommand to enter the MySQL bin directory and enter the MySQL-uroot-p Password Database Name <the database to be restored. For example, we need to name disk D as test. the SQL database is restored to the local test2 database:

MySQL-uroot-p password test2 is used for foreign hosts 4. x series, I feel better, no matter GBK and UTF-8 are not garbled, did not expect the new host MySQL is 5.0 version, after importing data, read out with Php is question mark, garbled text. I remember that I had a previous experience with Garbled text during Switching. The reason must be the difference between MySQL versions.

I had to check the information and found a solution. I added a set names UTF8 sentence after MySQL_connect to eliminate garbled characters in the UTF8 database. For GBK databases, I used SET NAMES GBK, the Code is as follows:

 
 
  1. $ mysql _mylink = mysql_connect($mysql_host, $mysql_user, $mysql_pass);   
  2. mysql_query("SET NAMES 'GBK'"); 

The database character set is UTF-8.

The connection statement uses this

 
 
  1. mysql_query("SET NAMES 'UTF8'");   
  2. mysql_query("SET CHARACTER SET UTF8");   
  3. mysql_query("SET CHARACTER_SET_RESULTS=UTF8'"); 

Another way is to change the MYSQL character set if your machine is running. It is generally valid in MYSQL 4 and MYSQL 5.

The second method of the mysql garbled solution: a good explanation. Try it.

Summary of mysql garbled processing:

(1) Chinese characters are processed normally in java. Displaying garbled characters in the cmd client is a problem with character sets.

(2) The length of the field is long enough. However, when a Chinese character is inserted, the system prompts com. mysql. jdbc. MysqlDataTruncation: Data truncation: Data too long for column.

(3) garbled characters are the final issue of character sets. We should consider the following aspects from Character Set settings: server, client, database, connection, and results.

Solution:

(1) modify the my. ini (MySQL Server Instance Configuration file)

 
 
  1. # CLIENT SECTION  
  2. [client]  
  3. port=3306 
  4. [mysql]  
  5. default-character-set=gbk 
  6. # SERVER SECTION  
  7. [mysqld]  
  8. default-character-set=gbk   

(2) modify the db. opt configuration file under the corresponding database directory in the data Directory

 
 
  1. default-character-set=gbk 
  2. default-collation=gbk_chinese_ci 

(3) Character Set specified in the database connection string

URL = jdbc: mysql: // yourIP/college? User = root & password = yourPassword & useUnicode = true & characterEncoding = gbk

Note: in MySQL 4.1.0, useUnicode = true & characterEncoding = gbk must be added. if the version is later than 5.0, it does not matter if you do not add it!

(4) Specify the character set when creating a database

 
 
  1. create database yourDB CHARACTER SET gbk; 

(5) On the doscommand, Set

 
 
  1. set names gbk 

Okay. Check the above four settings. It should be OK! There will be no issues such as com. mysql. jdbc. MysqlDataTruncation: Data truncation: Data too long for column.

The above content is an introduction to the mysql garbled solution. I hope you will get some benefits.

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.