如果你也遇到了这个问题,咱先不谈原因,在PC自带的cmd中(或者是mysql安装版安装后的Command Line客户端,又或者是工作用的SecureCRT)试试效果。进入mysql环境,从头开始操作。假设你的客户端编码是gbk或者utf8(这么说太不严谨了,怎么能假设呢,但是一般来
如果你也遇到了这个问题,咱先不谈原因,在PC自带的cmd中(或者是mysql安装版安装后的Command Line客户端,又或者是工作用的SecureCRT)试试效果。进入mysql环境,从头开始操作。假设你的客户端编码是gbk或者utf8(这么说太不严谨了,怎么能假设呢,但是一般来说假如安装后没动过,cmd是gbk编码,mysql安装后的Command Line客户端没装不记得,CRT看看Session Options里面的编码设置,一般也会设置成utf8),执行一些语句:
1. 设置编码客户端、连接、返回结果的字符集,先设置成latin1,
2. 然后执行下面的看下各个字符是不是这样的,
如果你的character_set_client、character_set_connection、character_set_results不是latin1,可以这样执行,把他们单个分别设置成latin1,比如设character_set_client,其他两个一样,确保这三个均是latin1(第一步的sql语句实际做的就是这件事),
3. 单独创建一个数据库db_latin1,当然是很简单的了,测试嘛,创建时就设置数据库的编码的为latin1,
4. 在它下面创建一张表tab_latin1,字符集也设置成latin1,这里不设置字符也行,数据库级已经设置了,这里只创建一个name字段,
5. 插入一些中文字符到表中,先说明,本机的cmd编码是gbk,查看方法是右键属性->选项,看下当前代码页即可知道,
6. 查看下结果
看吧,正常显示中文了~~~
OK,都到这儿了你就不想知道“为什么我那样设置就是不行”么,当然得往下看看是不。上图:
我们知道mysql是客户端-服务器软件,每次操作都是客户端向服务端发送请求,然后可能会返回一些结果,这之间插入的字符经过了一系列转换。首先供我们编辑的客户端本身就有一种编码,比如PC端的命令行默认是gbk,PC自带notepad新建文本文件默认是ANSI,常用的文本编辑器如notepad++,我们可能会设置默认编码为utf8,就是说在编辑器上编辑,你所看到的本身就是一种编码了。
1. 在客户端编辑后,首先转化为client对应的字符集,即上面打印出的character_set_client变量指示的字符集;
2. 向数据库服务发送请求,发送过程中,转化为connection对应连接字符集,即character_set_connection变量对应字符集;
3. 存储到数据库中,转化为数据库存储的字符集,可能是server级别(character_set_server)、database级别(character_set_database)或者表级别和列级别(这里还要细说下);
4. 数据库收到请求,执行查询得到结果,再次转化为results对应字符集,即character_set_results变量所指,该结果返回到客户端上;
5. 结果来了,是按照results字符集编码的,那我们让这个结果显示的客户端工具它支持什么样的编码也很重要,这决定了它如何去解码结果。假如这个结果是utf8编码,返回给某客户端了,但这个客户端只有ANSI编码,那当然不能显示正常,比如它返回到SecureCRT,结果显示不正常,但是CRT支持多种编码,我们手动将它调成utf8编码,那它就又显示正常了,所以严格来说这一步算不上,只是跟客户端条件有关,毕竟当我们知道后将客户端调整成正常的编码或者本来就支持转换results的编码后,这一步就不存在了。
在上面的第3步中,从连接字符集编码转化为数据库存储使用的编码时,要分几种情况,一般我们在装mysql时,特别是32位安装版本时,中间有一个选择编码的步骤,大多会选择utf8编码,这时系统就可能会把一系列的字符集变量均设置成了utf8,比如character_set_server、character_set_connection、character_set_database等等。也就是说这个character_set_server变量在你启动mysql服务的事先就被设置好了,我们可以称它为服务器级编码,那我们在建表前,先得创建数据库,在创建数据库时,我们知道可以显式指定编码的,比如最开头时我创建时显式指定采用latin1字符集,也可以不指定,如果不指定的话,它将采用服务器级的字符集,即character_set_server,同理在创建表时,也可不指定编码,不指定的话,采用数据库级编码,级character_set_database,更加同理在创建表中列字段时也可指定编码,不指定编码的话将采用表级别字符集,因此有这么一个继承关系在这:
character_set_server => character_set_database => character set in table(无此变量) => character set column(无此变量)
mysql创建表可以细化到这四个层次,不是每一层都必须指定,默认使用上一级的字符集(字符校对规则也是这样的,collation,稍后说明)。
那么有没有可能character_set_server没有指定呢,如果任何地方都没指定,特别是非安装版中,如果忘了,mysql在编译时默认采用latin1,为了应对这种情况,特别是非安装版本中在配置mysql时,经常需要手动配置mysql配置文件mysql.ini,其中就有大概这么一项:
在配置文件中默认采用的字符集,因此如果指定了character_set_server默认就会采用它,这样其他层次都不指定的话依次继承。

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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