Skip to content

Instantly share code, notes, and snippets.

@njujerry
Created April 19, 2017 09:06
Show Gist options
  • Save njujerry/67843c393d0020ae9d9e44a9d4af37e9 to your computer and use it in GitHub Desktop.
Save njujerry/67843c393d0020ae9d9e44a9d4af37e9 to your computer and use it in GitHub Desktop.
Mysql乱码问题注意点

Mysql乱码问题注意点

一:建库和建表结构指定的编码不一定就是实际存储的具体数据条目的编码。 例如本次mysql建库和建表都是utf8、但是里面存储的数据却是latin1的编码。

二:修改my.ini之后需要重启mysql。但是重启apache并不能重启mysql。 windows下面重启mysql应该采用先停止再启动的形式,没有直接的restart命令。 具体为:【net stop mysql】和【net start mysql】

三:my.ini中有多个section,比如[client]、[mysql]、[mysqld]等。这些section下面都可以设置默认字符集编码【default-character-set】,但所代表的含义不同。[client]中的default-character-set表示【客户端(第三方应用程序如PHP、JAVA等)与mysql建立连接交互的时候默认编码,对应于系统变量character_set_client、character_set_connection和character_set_results 】。[mysql]中的default-character-set表示【mysql内部程序(如自带的mysql.exe、mysqld.exe、mysqladmin.exe等)与mysql服务器交互采用的默认编码】。[mysqld]中的default-character-set表示【mysql的server启动时要用的编码,对应于character_set_server和character_set_database】,不过需要注意的是,高版本的mysql已经不是用default-character-set字段,而是用【character-set-server】字段。

四:要使得数据库中实际存在的具体数据条目能够被正确解码,必须设置my.ini中的[mysqld]部分的default-character-set为具体数据条目(而不是建库编码和建表结构编码)的编码。

五:客户端向mysql写入具体数据条目时,若不特殊指定编码格式(可通过set names XXX指定),则采用my.ini中的[client]中的default-character-set。

六:客户端读取mysql具体数据条目时,若不特殊指定编码格式,则mysql服务端会将数据按照my.ini中[mysqld]部分指定的default-character-set进行解码,此时若[mysqld]部分的编码设定和数据库中具体数据条目编码不同,将会出现乱码。

七:要让 MySQL 不出现乱码,要点是在于数据库连接的编码方式,而数据库表的编码方式并不重要,MySQL 会自动做好编码转换工作。【表示怀疑】

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment