标签:mysql中文乱码
1. 更改mysql数据库字符集
linux下修改/etc/my.cnf
windows下修改my.ini
[mysqld]
#default-character-set = utf8
character_set_server=utf8 #这个是我添加的
init_connect=‘SET NAMES utf8‘
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
[mysqld_safe]
default-character-set = utf8 #这个是我添加的
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
default-character-set = utf8 #这个是我添加的
[mysql.server]
default-character-set = utf8 #这个是我添加的
[mysql]
default-character-set = utf8 #这个是我添加的
mysql> show variables like ‘%char%‘;
| 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/ |
2 数据源url连接配置
在xml配置文件中配置数据库url时,要使用&的转义字符也就是&
<property name="url" value="jdbc:mysql://localhost:3306/customer?useUnicode=true&characterEncoding=UTF8"</property>
3.Tomcat
tomcat默认的字符集是ISO-8829-1,修改成utf-8
修改server.xml文件
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="utf-8"/>
总结:【只有在MySQL中可以使用“utf-8”的别名“utf8”,但是在其他地方一律使用大写“UTF-8”。】
本文出自 “一无所有-天行者” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1672424
标签:mysql中文乱码
原文地址:http://tianxingzhe.blog.51cto.com/3390077/1672424