以下的文章主要介绍的是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之后才能从命令行正常显示中文.

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


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

Dreamweaver CS6
Visual web development tools

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

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
