foreverW

最近在使用mysql的时候出现了奇怪的乱码问题,最开始发现mysql的字符集的确存在一些问题。

经过修改配置文件/etc/my.cnf

[mysqld]
character-set-server=utf8

> show variables like "character%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

但是发现并没有解决乱码的问题,又查找了相关的资料发现了,其实mysql中utf8字符集,我们国内默认选择:utf8_general_ci而不是utf8_unicode_ci,但是终端的utf8是utf8_unicode_ci,
所以这就造成了冲突,于是又修改配置文件。

[mysqld]
init_connect=\'SET collation_connection = utf8_unicode_ci\'
init_connect=\'SET NAMES utf8\'
character-set-server=utf8
collation-server=utf8_unicode_ci

修改后运行程序还是没有解决乱码的问题。
经过观察后又发现,虽然设置了配置文件,但是每次启动mysql后字符集还是会发生奇怪的变化。
于是再次修改配置文件。

[mysqld]
init_connect=\'SET collation_connection = utf8_unicode_ci\'
init_connect=\'SET NAMES utf8\'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

skip-character-set-client-handshake 跳过字符设置客户端握手。
文档解释如下

--character-set-client-handshake

Don\'t ignore character set information sent by the client. 
To ignore client information and use the default server character set, 
use --skip-character-set-client-handshake; this makes MySQL behave like MySQL 4.0

分类:

技术点:

相关文章:

  • 2021-06-12
  • 2021-11-15
  • 2022-02-04
  • 2022-01-29
  • 2022-01-10
  • 2021-11-29
  • 2022-12-23
  • 2021-12-30
猜你喜欢
  • 2022-01-03
  • 2021-12-08
  • 2021-11-28
  • 2022-01-13
  • 2021-11-15
  • 2022-01-04
  • 2021-11-18
相关资源
相似解决方案