Table of Contents
1. MySQL字符集和校对规则
2. 设置字符集
3. 简单修改字符集
4. 完全修改字符集
最后
Home Database Mysql Tutorial MySQL字符集常见问题的解决方法

MySQL字符集常见问题的解决方法

Jun 07, 2016 pm 04:24 PM
google mysql character set common method solve

在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)
Copy after login

2. 设置字符集

MySQL的字符集和校对规则有4个级别的默认设置:服务器级、数据库级、表级和字段级。

服务器级:

[mysqld]
default-character-set=utf8
[mysql]
default-character-set=utf8
Copy after login

或是启动时加上参数:mysqld --default-character-set=utf8

或是编译时加上参数:./configure --width-charset=utf8

查看字符集和校对规则:

show variables like 'character_set_server';
show variables like 'collation_server';
Copy after login

数据库级:

查看字符集和校对规则:

show variables like 'character_set_database';
show variables like 'collation_database';
Copy after login

表级:

查看字符集和校对规则:

show create table t \G
Copy after login

3. 简单修改字符集

简单修改只对以后的数据有影响,如果数据库中没有数据或是不想修改以前的数据,可以使用简单修改。

简单修改命令:

alter database character set utf8;
alter table t character set utf8;
Copy after login

个人推荐在创建数据库时明确指定字符集和校对规则,避免受到默认值的影响。

创建数据库时指定:reate database databasename default charset GBK;

创建数据表时指定:

create table tablename(
....
) ENGIND=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Copy after login

4. 完全修改字符集

当数据库中已有数据,想修改数据集,不能通过简单的修改字符集完成,需要先将原数据导出,经过适当调整后重新导入才可完成。 通过七步完成字符集的完全修改(假设原字符集是latin1,想修改成GBK)。

  1. 导出表结构:mysqldump -uroot -p --default-character-set=GBK -d databasename>createdb.sql
  • --default-character-set=GBK 表示设置以什么字符集连接;
  • -d 表示只导出表结构,不导出数据。
  • 手工修改createdb.sql中表结构定义中的字符集(latin1)为新的字符集(GBK);
  • 确保数据库中的数据不再更新,导出所有的数据:mysqldump -uroot -p --quick --no-create-info --extended-insert --default-character-set=latin1 databasename>data.sql
    • --quick 该选项用于转储大的表,它强制mysqldump从服务器一次一行地检索表中的行而不是检索所有行,并在输出前将它缓存到内存中;
    • --extended-insert 使用包括几个values的多行insert语法;
    • --no-create-info 不要create table语句;
    • --default-character-set=latin1 表示按照原有的字符集导出所有的数据。
  • 打开data.sql,将set names latin1修改成set names GBK;
  • 使用新的字符集创建数据库:create database databasename default charset GBK;
  • 创建表:mysql -uroot -p databasename
  • 导入数据:mysql -uroot -p databasename

    最后

    建议服务器的字符集参数不要修改,而是是创建数据库进加上字符集,特别是在创建表时记得加上,这样做的目的是为了使修改的影响最小化。

  • Statement of this Website
    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

    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 Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    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

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1664
    14
    PHP Tutorial
    1269
    29
    C# Tutorial
    1249
    24
    MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

    The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

    Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

    Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

    MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

    MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

    MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

    Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

    Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

    I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

    Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

    Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

    MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

    The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

    Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

    When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

    See all articles