在阿里云上了买了一个云服务器, 部署了一个程序,发现插入数据库后乱码了,都成了'????'.
一开始怀疑是Tomcat7的原因 , 见文章 : http://blog.csdn.net/johnny901114/article/details/28454117 发现还是乱码.
然后输出,在XShell命令窗口还是输出乱码, 把XShell 窗口设置utf-8 ,发现窗口输出正常. 最后判定是Mysql数据的问题 :
通过命令 :SHOW VARIABLES LIKE 'character%'; 最终发现
character_set_database和character_set_server的默认字符集还是latin1。
character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem |
binary
|
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/
二、最简单的完美修改方法,修改mysql的my.cnf文件中的字符集键值(注意配置的字段细节):
1、在[client]字段里加入
default
-
character
-
set
=utf8,如下:
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default
-
character
-
set
=utf8
2、在[mysqld]字段里加入
character
-
set
-server=utf8,如下:
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character
-
set
-server=utf8
3、在[mysql]字段里加入
default
-
character
-
set
=utf8,如下:
[mysql]
no
-auto-rehash
default
-
character
-
set
=utf8
修改完成后,service mysql restart重启mysql服务就生效。
注意:[mysqld]字段与[mysql]字段是有区别的。这点在网上没人反馈过。
使用SHOW VARIABLES LIKE 'character%';查看,发现数据库编码全已改成utf8。
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/ |