用 SHOW VARIABLES LIKE
'character_set_%'
; 查看一下 显示
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8|
| character_set_connection | utf8|
| character_set_database | latin1 |
| character_set_results | utf8|
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /home/jh/mysql/share/mysql/charsets |
+--------------------------+----------------------------+
再用 SHOW VARIABLES LIKE
'collation_%'
; 查看一下 显示
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
原来如此啊 哈哈 知道错在哪里 剩下的就好办了
解决方法:
依次执行:
set character_set_database =utf8;
set character_set_results =utf8;
set character_set_server =utf8;
set character_set_system =utf8; --此处utf-8也可以
然后执行:
SET collation_server = utf8_general_ci
SET collation_database = utf8_general_ci
执行完之后,请检查mysql下每个数据库,表,字段是否都是utf8,不是则改过来,这样子就不会出现
最笨的方法是重装一下数据库。(一般不要用这种方法呀)
最终解决方法:
1.1 如果是windows版本的mysql,那么在安装的时候,系统就会提示用哪种编码。
如果安装的时候设置错误了,修改mysql安装目录下的my.ini文件:
[mysql]
default
-character-set=utf8
...
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default
-character-set=utf8
配置好后,重启mysql。
1.2 如果是linux版本的mysql
修改mysql的配置文件,使数据库与服务器操作系统的字符集设置一致。
vi /etc/my.cnf 设置(如果没有发现这个文件,就新建1个)
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
default
-character-set=utf8 (增加的关键一句,使得数据库缺省以utf8存储)
当然,修改后,要重启数据库。(这样设置后对新建的数据库表才起作用)
用SHOW VARIABLES LIKE
'character_set_%'
;命令查看到如下内容:
+--------------------------+-----------------------------------------------------------------------+
| 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 | /home/jh/mysql/share/mysql/charsets |
+--------------------------+-----------------------------------------------------------------------+
发现关键项目已经用了utf8,但这样还不够,还要保证客户端也是用utf8的字符集来操作的。
登录的时候,要用以下命令:mysql --
default
-character-set=utf8 -u root -p
再次用SHOW VARIABLES LIKE
'character_set_%'
;命令查看,结果变成了:
+--------------------------+-----------------------------------------------------------------------+
| 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 | /home/jh/mysql/share/mysql/charsets/ |
+--------------------------+-----------------------------------------------------------------------+
这样才能保证客户端所发命令都是基于utf8格式的,比如说建立数据库和表,默认就会以utf8编码,而无须再次指定。(再次说一句对新建的数据库和表起作用)。