`

mysql set names 命令和 mysql 字符编码问题

阅读更多
转自:https://www.cnblogs.com/digdeep/p/5228199.html

1. character_set_system

The character set used by the server for storing identifiers. The value is always utf8.

character_set_system 是系统元数据(字段名等)存储时使用的编码字符集,该字段和具体存储的数据无关。总是固定不变的——utf8. 我们可以不去管它。

2. character_set_server

Use charset_name as the default server character set. See Section 10.5, “Character Set Configuration”. If you use this option to specify a nondefault character set, you should also use --collation-server to specify the collation.

该变量设置的 server 级别的(mysqld级别的) 字符集。也就是说设置的是 一个 mysqld 的,所有字符最后存储时,使用的编码字符集。

默认值为 lantin1. 我们一般设置成:utf8、utf8mb4、gbk 等值。

一同设置的还有 server 级别的排序规则:

collation_server:

utf8mb4_bin, utf8mb4_general_ci, utf8_bin, utf8_general_ci

ci 代表: casesensitive ignore 排序时不考虑大小写;而 _bin 结尾的排序时考虑大小写。

3. character_set_database

Every database has a database character set and a database collation. The CREATE DATABASE and ALTER DATABASE statements have optional clauses for specifying the database character set and collation:

CREATE DATABASE db_name
    [[DEFAULT] CHARACTER SET charset_name]
    [[DEFAULT] COLLATE collation_name]

ALTER DATABASE db_name
    [[DEFAULT] CHARACTER SET charset_name]
    [[DEFAULT] COLLATE collation_name]
character_set_database 是单个数据库级别的 字符集设置,该参数允许我们在同一个 mysqd 下面的不同的 database 使用不同的字符集。

比如:

create database db1 character set utf8mb4 collate utf8mb4_bin;

这就设置了 数据库 级别的字符集。如果 create database 语句没有 character 和 collate 参数,那么他们会默认使用:

character_set_server 和 character_collation 的值作为 默认值。

同样对应有数据库级别的排序规则参数:

collation_database
4. character_set_client

The character set for statements that arrive from the client. The session value of this variable is set using the character set requested by the client when the client connects to the server. (Many clients support a --default-character-set option to enable this character set to be specified explicitly. See also Section 10.1.4, “Connection Character Sets and Collations”.)

也就是 mysql client 发送 给 mysqld 的语句使用的 编码字符集。

可以使用 --default-character-set 参数来显示设置。

5. character_set_connection

The character set used for literals that do not have a character set introducer and for number-to-string conversion.

数字到字符转换时的编码字符集。

(用introducer指定文本字符串的字符集:
– 格式为:[_charset] 'string' [COLLATE collation]
– 例如:
• SELECT _latin1 'string';
• SELECT _utf8 '你好' COLLATE utf8_general_ci;
– 由introducer修饰的文本字符串在请求过程中不经过多余的转码,直接转换为内部字符集处理。 )

实际中我们一般没有人去使用 introducer ,所以其实是没有 introducer,所以都会使用 character_set_connection来编码的。

6. character_set_results

The character set used for returning query results such as result sets or error messages to the client.

mysqld 在返回 查询 结果集 或者错误信息到 client 时,使用的编码字符集。

7. set names 'xxx' 命令

可以看到改变的是 character_set_client、character_set_connection、character_set_results

它们都是和 client 相关的。而 真正server端的编码字符集,character_set_server 和 character_set_database ,set names 'xxx' 根本无法修改。

set names 'xxx' 命令可以使 character_set_client、character_set_connection、character_set_results 三者统一:

client (character_set_client) -----> character_set_connection -------> mysqld  ------> client(character_set_results)

减少编码转换的需要。

8. character_set_server 和 character_set_database

二者 的作用其实是相同的,都是设置 字符最终存储到磁盘时,使用的编码字符集。只不过 二者设置的级别不一样而已。character_set_server 设置了 mysqld 级别的存储编码字符集,而character_set_database设置 mysqld 中单个 database 的存储编码字符集。而且character_set_database的默认值就是 character_set_server 的值。

存在三次编码转换过程:

1)mysql client 使用 character_set_client编码的字符------> character_set_connection 编码字符

    ------> mysqld :这里需要从 character_set_connection 编码格式二进制流解码成 字符,然后使用 character_set_server/character_set_database 对字符进行再次编码,生成二进制流,存储时,就是存储再次编码的二进制流数据。

2)读取数据时,会使用 character_set_server/character_set_database 对读取到的二级制流进行 解码成 字符,然后使用 character_set_results 对字符进行二次编码,生成二进制流,发给 mysql client.

所以 使用 set names 'xxx' 命令,结合 character_set_server 参数,可以将 整个过程的 字符集设置成相同的,就不会存在编码转换的过程。

9. default-character-set = charset_name 配置参数

Use charset_name as the default character set for the client and connection(其实还有 character_set_results).

A common issue that can occur when the operating system uses utf8 or another multibyte character set is that output from the mysql client is formatted incorrectly, due to the fact that the MySQL client uses the latin1 character set by default. You can usually fix such issues by using this option to force the client to use the system character set instead.

See Section 10.5, “Character Set Configuration”, for more information.

default-character-set 能够同时指定 client 端 和 connection 的字符,也就是:character_set_client 和 character_set_connection的值,实际上还设置了 character-set-results 的值。

所以 default-character-set 的作用和 set names 'xxx' 的作用是一样的。
分享到:
评论

相关推荐

    解决 docker mysql 中文乱码问题

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

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

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

    mysql乱码解决方案

    linux 系统下mysql中文乱码问题的解决方案 1、在命令行中输入alter database 数据库名 default character set utf8; 设置字符编码方式 2、 set names utf8; 设置显示方式 3 、jdbc中url的路径后跟上?useUnicode=true...

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

    就始终都是和服务器端保持一致了,即便在mysql客户端加上选项 代码如下:–default-character-set=utf8 也不行,除非连接进去后,再手工执行命令 代码如下: set names latin1 ,才会将client、conne

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

    23-MySQL数据库多实例的优势和问题介绍.avi 24-MySQL数据库多实例的门户企业应用场景.avi 25-MySQL数据库多实例的多种配置方案介绍.avi 26-MySQL数据库多实例安装实战讲解.avi 27-MySQL数据库多实例初始化及服务启动...

    mysql必知必会读书笔记.doc

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

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

    先说MySQL的字符集问题。Windows下可通过修改my.ini内的 PHP代码 复制代码 代码如下: [mysql] default-character-set=utf8 //客户端的默认字符集 [mysqld] default-character-set=utf8 //服务器端默认的字符集 假设...

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

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

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

    导致问题产生的原因很简单,无非是命令行的编码和MySQL内部设置的编码出现了偏差。我们右键属性查看以下命令行的编码方式是GBK。 解决: mysql> show variables like 'character%'; -- 模糊查询character开头的全局...

    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

    php链接mysql数据库

    error_reporting(0); ob_start(); session_start(); //打开会话 $HOST="localhost";... mysql_query("SET NAMES 'gb2312'"); //mysql 字符集 mysql_select_db($DBNAME,$dblink); //选择数据库 ?>

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

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

    MySQL 编码机制

    character_set_client ,这是用户告诉MySQL查询是用的什么字符集。 character_set_connection ,MySQL... DISCUZ并没有使用set NAMES character SET NAMES ‘x’语句与这三个语句等价: mysql> SET character_set_cl

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

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

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

    您可能感兴趣的文章:MySQL从命令行导入SQL脚本时出现中文乱码的解决方法MySQL导入sql脚本错误:2006 解决方法mysql导入sql文件报错 ERROR 2013 2006 2002mysql导入sql文件命令和mysql远程登陆使用详解在linux中导入...

    深入Mysql字符集设置 图文版

    在mysql客户端与mysql服务端之间,存在着一个字符集转换器。 character_set_client =>gbk:转换器就知道客户端发送过来的是gbk格式的编码 character_set_connection=>gbk:...#这里的utf8表示服务器端的字符编码   首

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

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

    C# mysql 插入数据,中文乱码的解决方法

    一种是在执行语句前面设置,如: 代码如下:MySQLCommand mCommand = new MySQLCommand(“set names gb2312”, m_Connection);另一种是直接加在连接字符串里,如: 代码如下:”connection” connectionString=”...

    学习日报8.7.docx

    1.SET NAMES utf8;设置字符集 2.CREATE DATABASE 数据库名字;创建数据库 3.SHOW DATEBASE;显示所有数据库 4.USE DATEBASE;使用数据库 5.CREATE TABLE 表名字;创建表 6.SHOW TABLE;显示所有的表名 7.RENAME TABLE ...

    MySQLDBA运维笔记.pdf

    属主和用户组为 mysql 或 root......................................................................................21 1.3.4 删除默认存在的 test 库 .........................................................

Global site tag (gtag.js) - Google Analytics