在Google中搜索“mysql 乱码”,可以查到“1,550,000”条结果;搜索“jsp 乱码”,可以查到“1,450,000”条结果,当然JSP乱码不一定就是MySQL乱码,但多数情况是编码问题(可能是页面编码、数据库编码)。可见,编码问题在我们开发过程中是经常出现的,因此
在Google中搜索“mysql 乱码”,可以查到“1,550,000”条结果;搜索“jsp 乱码”,可以查到“1,450,000”条结果,当然JSP乱码不一定就是MySQL乱码,但多数情况是编码问题(可能是页面编码、数据库编码)。可见,编码问题在我们开发过程中是经常出现的,因此对编码的了解、设置和修改就显得及其重要了。
1. MySQL字符集和校对规则
我们都知道,字符集就是一套文字符号及其编码、比较规则的集合,因为计算机只认识二进制代码,所有我们必须要有一个转换。
MySQL的字符集包括字符集(Character Set)和校对规则(Collation)两个概念。字符集是用来定义MySQL存储字符串的方式,而校对规则则定义了比较字符串的方式。
字符集和校对规则是一对多的关系,每个字符集至少对应一个校对规则,称为默认校对规则。
查看所有字符集的命令:show character set;
或是查看information_schema.character_sets,可以得到所有的字符集和它的默认的校对规则,它的表结构:desc information_schema.character_sets;
查看字符集的校对规则:show collation like 'GBK%';
校对规则命名约定:以其相关的字符集名开始,通常包括一个语言名,并且以_ci(大小写不敏感)、_cs(大小写敏感)或_bin(二元,即比较是基于字符编码的值而与语言无关)结束。
实验命令:
select case when 'A' COLLATE utf8_general_ci = 'a' COLLATE utf8_general_ci then 'YES' else 'NO' end;(YES) select case when 'A' COLLATE gbk_chinese_ci = 'a' COLLATE gbk_chinese_ci then 'YES' else 'NO' end;(YES) select case when 'A' COLLATE gbk_bin = 'a' COLLATE gbk_bin then 'YES' else 'NO' end;(NO)
2. 设置字符集
MySQL的字符集和校对规则有4个级别的默认设置:服务器级、数据库级、表级和字段级。
服务器级:
[mysqld] default-character-set=utf8 [mysql] default-character-set=utf8
或是启动时加上参数:mysqld --default-character-set=utf8
或是编译时加上参数:./configure --width-charset=utf8
查看字符集和校对规则:
show variables like 'character_set_server'; show variables like 'collation_server';
数据库级:
查看字符集和校对规则:
show variables like 'character_set_database'; show variables like 'collation_database';
表级:
查看字符集和校对规则:
show create table t \G
3. 简单修改字符集
简单修改只对以后的数据有影响,如果数据库中没有数据或是不想修改以前的数据,可以使用简单修改。
简单修改命令:
alter database character set utf8; alter table t character set utf8;
个人推荐在创建数据库时明确指定字符集和校对规则,避免受到默认值的影响。
创建数据库时指定:reate database databasename default charset GBK;
创建数据表时指定:
create table tablename( .... ) ENGIND=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
4. 完全修改字符集
当数据库中已有数据,想修改数据集,不能通过简单的修改字符集完成,需要先将原数据导出,经过适当调整后重新导入才可完成。 通过七步完成字符集的完全修改(假设原字符集是latin1,想修改成GBK)。
- 导出表结构:mysqldump -uroot -p --default-character-set=GBK -d databasename>createdb.sql
- --default-character-set=GBK 表示设置以什么字符集连接;
- -d 表示只导出表结构,不导出数据。
- --quick 该选项用于转储大的表,它强制mysqldump从服务器一次一行地检索表中的行而不是检索所有行,并在输出前将它缓存到内存中;
- --extended-insert 使用包括几个values的多行insert语法;
- --no-create-info 不要create table语句;
- --default-character-set=latin1 表示按照原有的字符集导出所有的数据。
最后
建议服务器的字符集参数不要修改,而是是创建数据库进加上字符集,特别是在创建表时记得加上,这样做的目的是为了使修改的影响最小化。

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
