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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Detailed introduction of Samsung S24ai functions Detailed introduction of Samsung S24ai functions Jun 24, 2024 am 11:18 AM

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

See all articles