疯狂编程网

  • 首页
  • 后端
    • GOLANG
    • PHP
  • 前端
  • 客户端
  • 服务器
  • AIGC
  • 开发工具
  • 代码人生
  • 关于本站
    • 联系我们
    • 免责声明
  1. 首页
  2. 服务器
  3. MYSQL
  4. 正文

mysql 8.0设置collation_connection

2023年5月10日 152点热度 0人点赞 0条评论

设置全局

set global collation_connection = utf8mb4_general_ci 

 

设置会话级别

1.配置文件方式,给每个新的连接配置

[mysqld]

init-connect='SET NAMES utf8mb4 COLLATE utf8mb4_general_ci '

2.执行命令

set collation_connection = utf8mb4_general_ci 

或

SET NAMES utf8mb4 COLLATE utf8mb4_general_ci

 

附录:

 

 

 

 

 

 

 

 

SQL Statements for Connection Character Set Configuration

After a connection has been established, clients can change the character set and collation system variables for the current session. These variables can be changed individually using SET statements, but two more convenient statements affect the connection-related character set sytem variables as a group:

  • SET NAMES '<em class="replaceable"><code>charset_name' [COLLATE 'collation_name']

    SET NAMES indicates what character set the client uses to send SQL statements to the server. Thus, SET NAMES 'cp1251' tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement that produces a result set.)

    A SET NAMES '<em class="replaceable"><code>charset_name' statement is equivalent to these three statements:

    <span class="token keyword">SET character_set_client <span class="token operator">= <em class="replaceable">charset_name</em><span class="token punctuation">;
    <span class="token keyword">SET character_set_results <span class="token operator">= <em class="replaceable">charset_name</em><span class="token punctuation">;
    <span class="token keyword">SET character_set_connection <span class="token operator">= <em class="replaceable">charset_name</em><span class="token punctuation">;</span></span></span></span></span></span></span></span></span>

    Setting character_set_connection to charset_name also implicitly sets collation_connection to the default collation for charset_name. It is unnecessary to set that collation explicitly. To specify a particular collation to use for collation_connection, add a COLLATE clause:

    <span class="token keyword">SET <span class="token keyword">NAMES <span class="token string">'<em class="replaceable">charset_name</em>' <span class="token keyword">COLLATE <span class="token string">'<em class="replaceable">collation_name</em>'</span></span></span></span></span>
  • SET CHARACTER SET '<em class="replaceable"><code>charset_name'

    SET CHARACTER SET is similar to SET NAMES but sets character_set_connection and collation_connection to character_set_database and collation_database (which, as mentioned previously, indicate the character set and collation of the default database).

    A SET CHARACTER SET <em class="replaceable"><code>charset_name statement is equivalent to these three statements:

    <span class="token keyword">SET character_set_client <span class="token operator">= <em class="replaceable">charset_name</em><span class="token punctuation">;
    <span class="token keyword">SET character_set_results <span class="token operator">= <em class="replaceable">charset_name</em><span class="token punctuation">;
    <span class="token keyword">SET collation_connection <span class="token operator">= <span class="token variable">@@collation_database<span class="token punctuation">;</span></span></span></span></span></span></span></span></span></span>

    Setting collation_connection also implicitly sets character_set_connection to the character set associated with the collation (equivalent to executing SET character_set_connection = @@character_set_database). It is unnecessary to set character_set_connection explicitly.

Note

Some character sets cannot be used as the client character set. Attempting to use them with SET NAMES or SET CHARACTER SET produces an error. See Impermissible Client Character Sets.

Example: Suppose that column1 is defined as CHAR(5) CHARACTER SET latin2. If you do not say SET NAMES or SET CHARACTER SET, then for SELECT column1 FROM t, the server sends back all the values for column1 using the character set that the client specified when it connected. On the other hand, if you say SET NAMES 'latin1' or SET CHARACTER SET 'latin1' before issuing the SELECT statement, the server converts the latin2 values to latin1 just before sending results back. Conversion may be lossy for characters that are not in both character sets.

标签: mysql
最后更新:2023年5月10日

大明

靠写代码养家的开发者。

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2023 疯狂编程网. ALL RIGHTS RESERVED.

京ICP备2022013580号-1