search
HomeDatabaseMysql Tutorial关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)_MySQL

最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。

首先:

用show variables like “%colla%”;show varables like “%char%”;这两条命令查看数据库与服务端的字符集设置

如果查看出来都是gbk2312,或 gbk,那么就只能支持简体中文,繁体和一些特殊符号是不能插入的,我们只有修改字符集为UTF-8,

修改方法如下:

用记事本或UitraEdit打开mysql数据库安装目录下的my.ini文件打开, 然后Ctrl+F搜索default-character-set,将后面的字符集修改为UTF8,注意要修改两个地方,一个事客户端的,一个是服务端的。

然后保存,重启mysql服务、、进去继续用show variables like “%colla%”;show varables like “%char%”;着两条语句查询一下字符集。 如图:

到此就配置完成了。

注意:

如果以前建有数据库没有删除的 请用 show database 数据库名;和 show create table 表名;查看一下数据库和表的字符集是否为UTF8 , 因为修改my.ini文件,它不能修改原来数据库的的字符集。在命令行下面可以用

alter database 数据库名 character set “字符集”; 命令来修改数据库字符集

还有一点要注意的是,修改为UTF8以后,在命令行下面中文是乱码的,只输出到页面或控制台是正常的,这个问题我也上网查了一下,貌似命令行下面不支持UTF8,我也不太清楚。

当修改以后,在命令行下面如果要插入中文,可以在插入语句之前执行,set names gbk2312;就可以插入中文了,但是不能插入繁体和一些特殊符号。

以上就是这几天解决乱码的成果。希望各位大虾多多指教。

下面抽点空给大家整理些关于MySQL会出现中文乱码的原因不外乎下列几点。

1.server本身设定问题,例如还停留在latin1
2.table的语系设定问题(包含character与collation)
3.客户端程式(例如php)的连线语系设定问题

强烈建议使用utf8!!!!

utf8可以兼容世界上所有字符!!!!

一、避免创建数据库及表出现中文乱码和查看编码方法

1、创建数据库的时候:

CREATE DATABASE `test` 
CHARACTER SET 'utf8' 
COLLATE 'utf8_general_ci'; 

2、建表的时候

CREATE TABLE `database_user` ( 
`ID` varchar(40) NOT NULL default '', 
`UserID` varchar(40) NOT NULL default '', 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

这3个设置好了,基本就不会出问题了,即建库和建表时都使用相同的编码格式。

但是如果你已经建了库和表可以通过以下方式进行查询。

1.查看默认的编码格式:

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 | 
+--------------------------+-------------+ 

注:以前2个来确定,可以使用set names utf8,set names gbk设置默认的编码格式;

执行SET NAMES utf8的效果等同于同时设定如下:

SET character_set_client='utf8'; 
SET character_set_connection='utf8'; 
SET character_set_results='utf8'; 

2.查看test数据库的编码格式:

mysql> show create database test; 
+------------+------------------------------------------------------------------------------------------------+ 
| Database | Create Database | 
+------------+------------------------------------------------------------------------------------------------+ 
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET gbk */ | 
+------------+------------------------------------------------------------------------------------------------+ 

3.查看yjdb数据表的编码格式:

mysql> show create table yjdb; 
| yjdb | CREATE TABLE `yjdb` ( 
`sn` int(5) NOT NULL AUTO_INCREMENT, 
`type` varchar(10) NOT NULL, 
`brc` varchar(6) NOT NULL, 
`teller` int(6) NOT NULL, 
`telname` varchar(10) NOT NULL, 
`date` int(10) NOT NULL, 
`count` int(6) NOT NULL, 
`back` int(10) 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 | 

二、避免导入数据有中文乱码的问题

1:将数据编码格式保存为utf-8

设置默认编码为utf8:

set names utf8;

设置数据库db_name默认为utf8:

ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 

设置表tb_name默认编码为utf8:

ALTER TABLE `tb_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 

导入:

LOAD DATA LOCAL INFILE 'C:\\utf8.txt' INTO TABLE yjdb; 

2:将数据编码格式保存为ansi(即GBK或GB2312)

设置默认编码为gbk:

set names gbk;

设置数据库db_name默认编码为gbk:

ALTER DATABASE `db_name` DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; 

设置表tb_name默认编码为gbk:

ALTER TABLE `tb_name` DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; 

导入:

LOAD DATA LOCAL INFILE 'C:\\gbk.txt' INTO TABLE yjdb; 

注:1.UTF8不要导入gbk,gbk不要导入UTF8;

2.dos下不支持UTF8的显示;

三、解决网页中乱码的问题

将网站编码设为 utf-8,这样可以兼容世界上所有字符。

  如果网站已经运作了好久,已有很多旧数据,不能再更改简体中文的设定,那么建议将页面的编码设为 GBK, GBK与GB2312的区别就在于:GBK能比GB2312显示更多的字符,要显示简体码的繁体字,就只能用GBK。

1.编辑/etc/my.cnf ,在[mysql]段加入default_character_set=utf8;

2.在编写Connection URL时,加上?useUnicode=true&characterEncoding=utf-8参;

3.在网页代码中加上一个"set names utf8"或者"set names gbk"的指令,告诉MySQL连线内容都要使用utf8或者gbk;

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor