安装MySQL好多次了,每次都会纠结于数据库的字符编码配置,所以我决定这一次彻底把它理清。
MySQL的字符编码结构比较细,它大方向分为两个部分:数据存储编码和数据传输编码。本篇讨论数据存储编码部分,数据传输编码在下一篇 MySQL的字符编码体系(二)——数据传输编码 中讨论。
编码层次
数据存储的字符编码配置是指定数据库中存储的数据默认采用什么字符编码。默认字符编码的设置分为四个层次:服务器级、数据库级、数据表级和列级。也就是说,可以为服务器设置一个默认字符编码,再为服务器中的每一个数据库设置不同的默认编码,再为同一个数据库中的每一个数据表设置不同的默认编码,再为同一个数据表中的每一个列设置不同的默认编码。
MySQL数据库服务器的逻辑结构
那这四个层次的编码设置到底如何起作用呢?如果新建数据库时没有指定字符编码,就默认设置为服务器的编码;如果新建数据表时没有指定任何编码,就默认设置为数据库的编码;如果向数据表添加新列或新建数据表时没有特别指定某些列的编码,那么这些列就默认设置为数据表的编码。注意这里四个层次的编码都是作为“默认”的存在,用户创建数据库、表或增加列时直接指定的编码是最优先的。
另一方面,直接改变这四个层次的编码并不会改变它们各自所有下层对象的当前编码。比如修改只Server级,那么所有已经存在的数据库的默认编码不变,数据表、表列以及每一行现有数据记录的字符编码都不变,但是如果新建一个数据库且不指定其默认编码,那它的默认编码就会被设置为Server的默认编码;同样即使修改了所有四个层次的编码,但是数据表中每一条现有记录的字符字段仍然是按原来的编码存储的,但是如果向数据表中新插入一条记录,数据库将根据数据表当前各列的默认编码来存储该条记录的各个字符字段。
设置方法
修改Server以下各级编码的SQL语句如下:
ALTER {DATABASE | SCHEMA} [db_name] [DEFAULT] CHARACTER SET [=] charset_nameALTER TABLE dbl_name [DEFAULT] CHARACTER SET [=] charset_nameALTER TABLE dbl_name MODIFY [COLUMN] col_name {CHAR[(length)] | TEXT} CHARACTER SET charset_name
注意上面第三条修改列字符编码,实际上是通过完全重新定义列属性的方式实现的,语法跟创建新数据表时指定列字段属性一样的。所以如果这里只是想修改列字符编码,那就必须完整地写上创建该列时使用的所有定义修饰。
修改Server默认编码可以通过运行时直接修改变量character_set_server实现,但这样是临时性的,客户端关闭重启后又会自动恢复。要想永久改变Server默认编码需要在my.ini或my.cnf配置文件的“[mysqld]”区域中设定该变量的值,然后重启服务器:
[mysqld]character_set_server=charset_name

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

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.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
