设置mysql字符编码为utf8

  • 2015-08-28
  • 浏览 (115)

在windows下开发的JavaWeb项目,运行测试没有任何问题,后来部署到阿里云租来的linux服务器上遇到了中文乱码的问题,排查后发现是mysql字符编码设置的问题.


进入mysql,用"show variables like '%character%';"可以查看当前的字符编码设置.如果不是utf8的,设置成utf8的就可以解决乱码问题.


在linux下,用 "vi /etc/my.cnf" 编辑文件:

  • 在[client]字段里加入default-character-set=utf8
  • 在[mysqld]字段里加入default-character-set=utf8和init_connect='SET NAMES utf8'


5.7的mysql utf8配置

[mysqld]
character-set-server=utf8 
collation-server=utf8_general_ci 

[mysql]
default-character-set = utf8

[mysql.server]
default-character-set = utf8

[mysqld_safe]
default-character-set = utf8

[client]
default-character-set = utf8


下面是我的my.cnf的配置:

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_allowed_packet=29777216
init_connect='SET NAMES utf8'
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
default-character-set=utf8
init_connect='SET NAMES utf8'

[client]
default-character-set=utf8

正文到此结束