`
dazhilao
  • 浏览: 239746 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

【转】Mysql SET NAMES 字符集解决方案

阅读更多
大家都知道
SET NAMES x
相当于
SET character_set_client = x;
SET character_set_results = x;
SET character_set_connection = x;

是用来设置客户端的字符集的,与服务器端的字符集无关,应该说msyql服务器端存储的字符是一定的。
因此在只要保持set names x 与页面的编码一致 ,无论什么编码都可以正确显示.

以下从MySQL5.0官方文档上摘录了相关内容,并翻译,说明了相关系统变量的用处:

What character set is the statement in when it leaves the client?
statement离开客户端的时候是什么字符集?

The server takes the character_set_client system variable to be the character set in which statements are sent by the client.
客户端送过来的statement,服务器认为它的字符集是系统变量character_set_client的值。

What character set should the server translate a statement to after receiving it?
服务器收到一个statement后,会把它转换成什么字符集?

For this, the server uses the character_set_connection and collation_connection system variables. It converts statements sent by the client from character_set_client to character_set_connection (except for string literals that have an introducer such as _latin1 or _utf8). collation_connection is important for comparisons of literal strings. For comparisons of strings with column values, collation_connection does not matter because columns have their own collation, which has a higher collation precedence.
为了这一用途,服务器使用系统变量character_set_connection和collation_connection。它把客户端传来的 statement,从character_set_client字符集转换成character_set_connection字符集(除非字符串中有类似_latin1或者_utf8的字符集声明)。collation_connection对于字符串的比较是非常重要的。对于字符类型的字段值的比较,collation_connection是不起作用的。因为字段有自己的collation,有更高的优先级。

What character set should the server translate to before shipping result sets or error messages back to the client?
在结果集由服务器传递给客户端之前,需要转换成什么字符集?

The character_set_results system variable indicates the character set in which the server returns query results to the client. This includes result data such as column values, and result metadata such as column names.
character_set_results系统变量表明了服务器返回查询结果时使用的字符集。返回的数据,有比如字段的值和元数据(例如字段名)。

If you are using the mysql client with auto-reconnect enabled (which is not recommended), it is preferable to use the charset command rather than SET NAMES. For example:
如果你使用mysql客户端的自动重连(不推荐使用),最好用charset命令,而不是SET NAMES。例如:

mysql> charset utf8
Charset changed

The charset command issues a SET NAMES statement, and also changes the default character set that is used if mysql reconnects after the connection has dropped.
charset命令发出了一个SET NAMES语句,并且连接断开后自动重连时使用的缺省字符集也被修改了。

The database character set and collation are used as default values if the table character set and collation are not specified in CREATE TABLE statements. They have no other purpose.
如果CREATE TABLE中没有明确指出字符集和collation,那么database字符集和collation将做为缺省值,它们没有其他的用处。

The character set and collation for the default database can be determined from the values of the character_set_database and collation_database system variables. The server sets these variables whenever the default database changes. If there is no default database, the variables have the same value as the corresponding server-level system variables, character_set_server and collation_server.
缺省database的字符集和collation可以通过系统变量character_set_database和 collation_database查看。服务器当缺省database改变时设置这些变量的值。如果没有缺省的database,这些变量的将与对应的服务器级的系统变量-character_set_server和collation_server的值相同。

The table character set and collation are used as default values if the column character set and collation are not specified in individual column definitions. The table character set and collation are MySQL extensions; there are no such things in standard SQL.
表的字符集和collation会作为缺省值,如果列的定义中没有指明字符集和collation的话。

Every “character” column (that is, a column of type CHAR, VARCHAR, or TEXT) has a column character set and a column collation.
每个“字符”字段(即CHAR、VARCHAR或者TEXT类型的字段)都有一个字段字符集和字段校验(collation)。

Every character string literal has a character set and a collation.
每个字符串有一个字符集和一个较验。

A character string literal may have an optional character set introducer and COLLATE clause:
每个字符串有一个可选的字符集introducer和COLLATE子句:

[_charset_name]’string’ [COLLATE collation_name]

Examples:

SELECT ’string’;
SELECT _latin1’string’;
SELECT _latin1’string’ COLLATE latin1_danish_ci;
分享到:
评论

相关推荐

    解决 docker mysql 中文乱码问题

    docker mysql 字符集设置 使用 docker 启动 mysql 容器可能会出现中文乱码的情况,这里记录如何制作支持中文的 mysql 镜像 docker版本:18.06 mysql 版本:5.7 1. 创建 my.cnf 文件 [client] default-character-set=...

    mysql必知必会读书笔记.doc

    mysql字符集 字符集是字符加上编码 校对规则是对编码比较的一套规则 校对规则的一些命名规则 1.两个不同的字符集不能使用同一种校对规则 2.校对规则命名的习惯 (相关字符集)_(地区语言名)_(cs大小写敏感/ci大小写不...

    mysqli_set_charset和SET NAMES使用抉择及优劣分析

    最近公司组织了个PHP安全编程的培训, 其中涉及到一部分关于Mysql的”SET NAMES”和mysql_set_charset (mysqli_set_... 首先, 很多人都不知道”SET NAMES”到底是做了什么, 我之前的文章深入MySQL字符集设置中, 曾经介

    PHP 设置MySQL连接字符集的方法

    mysql_set_charset()。... Using mysql_query() to execute SET NAMES .. is not recommended. 您可能感兴趣的文章:MySQL查询随机数据的4种方法和性能对比MySQL的指定范围随机数函数rand()的使用技巧

    php读取mysql乱码,用set names XXX解决的原理分享

    Windows下可通过修改my.ini内的 PHP代码 复制代码 代码如下: [mysql] default-character-set=utf8 //客户端的默认字符集 [mysqld] default-character-set=utf8 //服务器端默认的字符集 假设我们把两个都设为utf8,...

    深入Mysql字符集设置 图文版

    在mysql客户端与mysql服务端之间,存在着一个...注:以上三个字符集可以使用set names gbk来统一进行设置 例子: create table test( name varchar(64) NOT NULL )charset utf8;#这里的utf8表示服务器端的字符编码   首

    MySQL skip-character-set-client-handshake导致的一个字符集问题

    今天帮同事处理一个棘手的事情,问题是这样的: 无论在客户机用哪个版本的mysql客户端连接服务器,发现只要服务器端设置了 代码如下:character-set-server = utf8 ...set names latin1 ,才会将client、conne

    2017最新老男孩MySQL高级专业DBA实战课程全套【清晰不加密】,看完教程月入40万没毛病

    14-更改生产线上MySQL数据字符集的方案讲解.avi 第五部 MySQL备份、增量备份及数据恢复基础实战(12节) 01-备份数据的意义及重要性.avi 02-mysqldump多个命令参数备份实战.avi 03-mysqldump逻辑备份的工作原理.avi...

    php链接mysql数据库

    error_reporting(0); ob_start(); session_start(); //打开会话 $HOST="localhost"; $USER="admin";... $dblink=@mysql_connect($HOST,$USER... //mysql 字符集 mysql_select_db($DBNAME,$dblink); //选择数据库 ?>

    MySQL 编码机制

    character_set_client ,这是用户告诉MySQL查询是用的什么字符集。 character_set_connection ,MySQL接受到用户查询后,按照character_set_client将其转化为character_set_connection设定的字符集。 character_set_...

    mysql通过my.cnf修改默认字符集为utf-8的方法和注意事项

    选项配置 配置文件路径: /full/path/mysql/bin/my.cnf (默认为/etc/my.cnf ) [client] ...init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-charact

    MySQL存储数据乱码的问题解析

    mysql的字符集设置有多个层级,在mysql...设置session字符集为utf8:set names utf8,设置客户端显示字符集为utf8,然后从表中select出有乱码的数据。 上面显示,在character_set_client跟客户端的字符集一致的情况下

    asp MYSQL出现问号乱码的解决方法

    这样的问题是因为 数据库字符集,表字符集,字段字符集都设为:gbk_chinese_ci 注意数据库连接串里面的 Stmt=Set Names ‘GBK’ ,一定要有这一句。 下面是asp链接mysql的代码。请注意,后面有个GBK。这样就是...

    MySQL如何解决DOS窗口乱码问题

    character_set_client:客户端的字符集 character_set_connection:连接的字符集 character_set_results:查询结果的字符集 可以将上面三者分别设置为GBK:set xxx = gbk; 可以简便地:set names gbk; 当然,上面

    mysql数据库乱码之保存越南文乱码解决方法

    mysql_query(‘SET NAMES ‘.MYSQL_ENCODE,$conn) or die(‘字符集设置错误’.mysql_error()); 搞了大半小时,没有搞定。 insert 的数据都是仍然乱码,突然想,是不是字段不能保存这些越南文。 我看一下数据库字符集...

    学习日报8.7.docx

    设置字符集 2.CREATE DATABASE 数据库名字;创建数据库 3.SHOW DATEBASE;显示所有数据库 4.USE DATEBASE;使用数据库 5.CREATE TABLE 表名字;创建表 6.SHOW TABLE;显示所有的表名 7.RENAME TABLE 原名TO 新名;修改...

    centos下mysql主从同步快速设置步骤分享

    Master:/etc/my.cnf [mysqld] server-id = 1 log-bin innodb_flush_log_at_trx_commit=1 sync_binlog=1 datadir=/var/lib/mysql character-set-server=utf8 init_connect=’SET NAMES utf8’设定了默认字符集为utf8...

    MySQLDBA运维笔记.pdf

    mysql 总结........................................................................................................................................6 1.1 数据库的种类.......................................

    解析MYSQL 数据库导入SQL 文件出现乱码的问题

    导入数据时,如果目标数据库或表是UTF-8字符集的,而导入SQL中有中文,可能在最终结果中出现乱码,此时只需在导入的SQL文件第一行加入如下内容 即可。/*!40101 SET NAMES utf8 */; 您可能感兴趣的文章:MySQL从...

Global site tag (gtag.js) - Google Analytics