Database garbled __ Database

Source: Internet
Author: User
Tags mysql insert create database mysql view
I small rookie one, in order to self-learning and Exchange PHP (Jquery,linux,lamp,shell,javascript, Server) and a series of knowledge, small rookie created a group. People who wish to visit this blog can come in and exchange. Seek common development. Build a platform. I blog also has a lot of technical documents, I hope you can provide some help. QQ Group: 191848169 qq:450225664


MySQL will appear in Chinese garbled reasons are the following:
1.server itself set the problem, for example still stay in Latin1
2.table language setting problem (including character and collation)
3. Client program (for example, PHP) of the line-language setting problem
It is highly recommended to use UTF8!!!!
UTF8 can be compatible with all the characters in the world!!!!

Linux under MySQL insert Chinese display garbled solution

mysql-uroot-p return Enter password

Enter the MySQL view status as follows:

MySQL cannot be inserted in Chinese

The default is the client and server are used latin1, so will be garbled.

Solution:

Mysql>user MyDB;

Mysql>alter database mydb Character Set utf8;!

The other way is to directly modify the default MySQL configuration file

in a Debian environment, completely solve MySQL cannot insert Chinese and Chinese garbled method

The above mentioned the use of temporary methods to change the database's character set settings, display Chinese, but later found in some systems can not be successful, such as I used Debian 7.0, looking for a long time to find a solution for the Debian system, Debian 7.0 tested successfully, Other methods, such as modifying client and MySQL to join Default-character-set=utf8, apply only to systems prior to 5.5.

Ultimate Solution, once and for all:

Debian under the MySQL string modified for UTF8 (reference: Rainysia's Column)

First, enter MySQL (mysql-uroot-p), view the current database character set (status;)

Second, view the current system version (LSB_RELEASE-A)

MySQL cannot be inserted in Chinese

Third, #vim/etc/mysql/my.cnf. (5.5 prior system) join Default-character-set=utf8 under "client"

Join Default-character-set=utf8 under "mysqld"

Notice: Note If you can not start the error after modification try to change the Default-character-set=utf8 to Character_set_server=utf8, just add to the mysqld below. Client doesn't need to add.

Four, #vim/etc/mysql/my.cnf. (after 5.5 system) modify as follows:

[Client]
Default-character-set=utf8

[Mysqld]
Default-storage-engine=innodb
Character-set-server=utf8
Collation-server=utf8_general_ci

Five, restart MySQL (/etc/init.d/mysql stop/etc/init.d/mysql start)

Six, build success, enter MySQL view character set (Mysql>show variables like ' character_set_% ';)

MySQL cannot insert Chinese first, avoid creating databases and tables appear in Chinese garbled and view coding methods
1, the creation of the database when: "Create db ' Test '
CHARACTER SET ' UTF8 '
COLLATE ' Utf8_general_ci ';
2. Create table ' Database_user ' (
' ID ' varchar ' not NULL default ',
' UserID ' varchar not NULL default ',
) Engine=innodb DEFAULT Charset=utf8;

These 3 settings are OK, the basic will not be a problem, that is, the same encoding format is used to build the library and build the table.
But if you have built libraries and tables, you can query them in the following ways.
1. View the default encoding format:
Mysql> Show variables like "%char%";
+--------------------------+---------------+
| variable_name | Value |
+--------------------------+---------------+
| character_set_client | GBK |
| character_set_connection | GBK |
| Character_set_database | UTF8 |
| Character_set_filesystem | binary |
| Character_set_results | GBK |
| Character_set_server | UTF8 |
| Character_set_system | UTF8 |
+--------------------------+-------------+
Note: The previous 2 to determine that you can use the set names Utf8,set names GBK to set the default encoding format;

The effect of executing set NAMES UTF8 is equivalent to setting the following:
SET character_set_client= ' UTF8 ';
SET character_set_connection= ' UTF8 ';
SET character_set_results= ' UTF8 ';

2. View the encoding format of the test database:
Mysql> Show create DATABASE test;
+------------+------------------------------------------------------------------------------------------------+
| Database | Create Database |
+------------+------------------------------------------------------------------------------------------------+
| Test | CREATE DATABASE ' test '/*!40100 DEFAULT CHARACTER SET GBK * |
+------------+------------------------------------------------------------------------------------------------+

3. View the encoding format of the YJDB database:
Mysql> Show CREATE TABLE yjdb;
| Yjdb | CREATE TABLE ' Yjdb ' (
' SN ' int (5) not NULL auto_increment,
' type ' varchar not NULL,
' BRC ' varchar (6) Not NULL,
' Teller ' int (6) Not NULL,
' telname ' varchar not NULL,
' Date ' int (ten) is not NULL,
' Count ' int (6) is not NULL,
' Back ' int (ten) not NULL,
PRIMARY KEY (' sn '),
UNIQUE KEY ' sn ' (' sn '),
UNIQUE KEY ' sn_2 ' (' SN ')
) Engine=myisam auto_increment=1826 DEFAULT CHARSET=GBK row_format=dynamic |

Second, to avoid importing data with Chinese garbled problem
1: Save the Data encoding format as Utf-8
Set default encoding to UTF8:
Set names UTF8;
Set database db_name defaults to UTF8:
ALTER DATABASE ' db_name ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
Set table tb_name default encoding is UTF8:
ALTER TABLE ' tb_name ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
Import:
LOAD DATA local INFILE ' c:\\utf8.txt ' into TABLE yjdb;
2: Save the Data encoding format as ANSI (that is, GBK or GB2312)
Set default encoding to GBK:
Set names GBK;
Set the database db_name default encoding is GBK:
ALTER DATABASE ' db_name ' DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
Set table Tb_name default encoding is GBK:
ALTER TABLE ' tb_name ' DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
Import:
LOAD DATA local INFILE ' c:\\gbk.txt ' into TABLE yjdb;

Note: 1. UTF8 do not import GBK,GBK do not import UTF8;
The display of UTF8 is not supported under 2.dos;
Third, solve the problem of garbled Web pages

Set the site code to Utf-8 so that all the characters in the world are compatible.
If the site has been operating for a long time, there are many old data, can not change the setting of Simplified Chinese, then it is recommended that the encoding of the page set to GBK, GBK and GB2312 is the difference between: GBK can display more characters than GB2312, to show the traditional character of simplified code, can only use GBK
1. Edit/etc/my.cnf, add Default_character_set=utf8 in [MySQL] section;
2. When writing the connection URL, add the useunicode=true&characterencoding=utf-8 parameter;
3. Add a "Set names UTF8" or "Set names GBK" instructions to the Web page code to tell MySQL that the connection content is to be used
UTF8 or GBK;

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.