以下的文章主要介绍的是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>
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>
修改完后,重启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>
(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>
一般就算设置了表的默认MySql字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句:
<ol class="dp-xml"><li class="alt"><span><span>SET NAMES 'utf8'; </span></span></li></ol>
它相当于下面的三句指令:
<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>
不知道什么原因,我在自己机器上把上述三个MySql字符集都设置成utf8后,查询中文结果还是乱码,只有把character_set_results设置成GBK之后才能从命令行正常显示中文.