Home Database Mysql Tutorial MySql字符集常见问题的解决

MySql字符集常见问题的解决

Jun 07, 2016 pm 04:11 PM
mysql main introduce character set common article solve

以下的文章主要介绍的是MySql字符集的相关问题的解决,其中包括ROR+Mysql5 utf8 搭配问题的实际解决方案 ,Mysql数据库的中文问题的解决与mssqlserver 数据库转换mysql 出现乱码问题 。 更多相关推荐 最近在登陆公司服务器上的数据库时,在命令行里执行查询语

以下的文章主要介绍的是MySql字符集的相关问题的解决,其中包括ROR+Mysql5 utf8 搭配问题的实际解决方案 ,Mysql数据库的中文问题的解决与mssqlserver 数据库转换mysql 出现乱码问题 。

更多相关推荐 最近在登陆公司服务器上的数据库时,在命令行里执行查询语句时候得到的结果只要是中文,就全部是乱码,很让人头疼呀,,查了些资料总结一下.作为自己以后的参考.

MySQL的字符集支持(Character Set Support)有两个方面:MySql字符集(Character set)和排序方式(Collation)。对于字符集的支持细化到四个层次:
服务器(server),数据库(database),数据表(table)和连接(connection)。

1.MySQL默认字符集:MySQL对于字符集的指定可以细化到一个数据库,一张表,一列.传统的程序在创建数据库和数据表时并没有使用那么复杂的配置,它们用的是默认的配置.

(1)编译MySQL 时,指定了一个默认的字符集,这个字符集是 latin1;

(2)安装MySQL 时,可以在配置文件 (my.ini) 中指定一个默认的的字符集,如果没指定,这个值继承自编译时指定的;

(3)启动mysqld 时,可以在命令行参数中指定一个默认的的字符集,如果没指定,这个值继承自配置文件中的配置,此时 character_set_server 被设定为这个默认的字符集;

(4)当创建一个新的数据库时,除非明确指定,这个数据库的MySql字符集被缺省设定为character_set_server;

(5)当选定了一个数据库时,character_set_database 被设定为这个数据库默认的字符集;

(6)在这个数据库里创建一张表时,表默认的字符集被设定为 character_set_database,也就是这个数据库默认的字符集;

(7)当在表内设置一栏时,除非明确指定,否则此栏缺省的字符集就是表默认的字符集;如果什么地方都不修改,那么所有的数据库的所有表的所有栏位的都用 latin1 存储,不过我们如果安装 MySQL,一般都会选择多语言支持,也就是说,安装程序会自动在配置文件中把 default_character_set 设置为 UTF-8,这保证了缺省情况下,所有的数据库的所有表的所有栏位的都用 UTF-8 存储。

2.查看默认MySql字符集(默认情况下,mysql的字符集是latin1(ISO_8859_1)通常,查看系统的字符集和排序方式的设定可以通过下面的两条命令:

<ol class="dp-xml">
<li class="alt"><span><span>mysql</span><span class="tag">></span><span> SHOW VARIABLES LIKE 'character%';  </span></span></li>
<li><span>+--------------------------+---------------------------------+  </span></li>
<li class="alt"><span>| Variable_name | Value |  </span></li>
<li><span>+--------------------------+---------------------------------+  </span></li>
<li class="alt"><span>| character_set_client | latin1 |  </span></li>
<li><span>| character_set_connection | latin1 |  </span></li>
<li class="alt"><span>| character_set_database | latin1 |  </span></li>
<li><span>| character_set_filesystem | binary |  </span></li>
<li class="alt"><span>| character_set_results | latin1 |  </span></li>
<li><span>| character_set_server | latin1 |  </span></li>
<li class="alt"><span>| character_set_system | utf8 |  </span></li>
<li><span>| character_sets_dir | D:"mysql-5.0.37"share"charsets" |  </span></li>
<li class="alt"><span>+--------------------------+---------------------------------+  </span></li>
<li>
<span>mysql</span><span class="tag">></span><span> SHOW VARIABLES LIKE 'collation_%';  </span>
</li>
<li class="alt"><span>+----------------------+-----------------+  </span></li>
<li><span>| Variable_name | Value |  </span></li>
<li class="alt"><span>+----------------------+-----------------+  </span></li>
<li><span>| collation_connection | utf8_general_ci |  </span></li>
<li class="alt"><span>| collation_database | utf8_general_ci |  </span></li>
<li><span>| collation_server | utf8_general_ci |  </span></li>
<li class="alt"><span>+----------------------+-----------------+ </span></li>
</ol>
Copy after login

3.修改默认字符集

(1) 最简单的修改方法,就是修改mysql的my.ini文件中的MySql字符集键值,

<ol class="dp-xml">
<li class="alt"><span><span class="attribute">default-character-set</span><span> = </span><span class="attribute-value">utf8</span><span> </span></span></li>
<li>
<span class="attribute">character_set_server</span><span> = </span><span class="attribute-value">utf8</span><span> </span>
</li>
</ol>
Copy after login

修改完后,重启mysql的服务,service mysql restart

使用 mysql> SHOW VARIABLES LIKE 'character%';查看,发现数据库编码均已改成utf8

<ol class="dp-xml">
<li class="alt"><span><span>+--------------------------+---------------------------------+  </span></span></li>
<li><span>| Variable_name | Value |  </span></li>
<li class="alt"><span>+--------------------------+---------------------------------+  </span></li>
<li><span>| character_set_client | utf8 |  </span></li>
<li class="alt"><span>| character_set_connection | utf8 |  </span></li>
<li><span>| character_set_database | utf8 |  </span></li>
<li class="alt"><span>| character_set_filesystem | binary |  </span></li>
<li><span>| character_set_results | utf8 |  </span></li>
<li class="alt"><span>| character_set_server | utf8 |  </span></li>
<li><span>| character_set_system | utf8 |  </span></li>
<li class="alt"><span>| character_sets_dir | D:"mysql-5.0.37"share"charsets" |  </span></li>
<li><span>+--------------------------+---------------------------------+ </span></li>
</ol><br>
Copy after login

(2) 还有一种修改字符集的方法,就是使用mysql的命令

<ol class="dp-xml">
<li class="alt"><span><span>mysql</span><span class="tag">></span><span> SET </span><span class="attribute">character_set_client</span><span> = </span><span class="attribute-value">utf8</span><span> ;  </span></span></li>
<li>
<span>mysql</span><span class="tag">></span><span> SET </span><span class="attribute">character_set_connection</span><span> = </span><span class="attribute-value">utf8</span><span> ;  </span>
</li>
<li class="alt">
<span>mysql</span><span class="tag">></span><span> SET </span><span class="attribute">character_set_database</span><span> = </span><span class="attribute-value">utf8</span><span> ;  </span>
</li>
<li>
<span>mysql</span><span class="tag">></span><span> SET </span><span class="attribute">character_set_results</span><span> = </span><span class="attribute-value">utf8</span><span> ;  </span>
</li>
<li class="alt">
<span>mysql</span><span class="tag">></span><span> SET </span><span class="attribute">character_set_server</span><span> = </span><span class="attribute-value">utf8</span><span> ;  </span>
</li>
<li>
<span>mysql</span><span class="tag">></span><span> SET </span><span class="attribute">collation_connection</span><span> = </span><span class="attribute-value">utf8</span><span> ;  </span>
</li>
<li class="alt">
<span>mysql</span><span class="tag">></span><span> SET </span><span class="attribute">collation_database</span><span> = </span><span class="attribute-value">utf8</span><span> ;  </span>
</li>
<li>
<span>mysql</span><span class="tag">></span><span> SET </span><span class="attribute">collation_server</span><span> = </span><span class="attribute-value">utf8</span><span> ;  </span>
</li>
</ol>
Copy after login

一般就算设置了表的默认MySql字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句:

<ol class="dp-xml"><li class="alt"><span><span>SET NAMES 'utf8'; </span></span></li></ol>
Copy after login

它相当于下面的三句指令:

<ol class="dp-xml">
<li class="alt"><span><span>SET </span><span class="attribute">character_set_client</span><span> = </span><span class="attribute-value">utf8</span><span>;  </span></span></li>
<li>
<span>SET </span><span class="attribute">character_set_results</span><span> = </span><span class="attribute-value">utf8</span><span>;  </span>
</li>
<li class="alt">
<span>SET </span><span class="attribute">character_set_connection</span><span> = </span><span class="attribute-value">utf8</span><span>; </span>
</li>
</ol>
Copy after login

不知道什么原因,我在自己机器上把上述三个MySql字符集都设置成utf8后,查询中文结果还是乱码,只有把character_set_results设置成GBK之后才能从命令行正常显示中文.
 


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 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
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

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.

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.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

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.

See all articles