Java web项目在Linux(Contos)下部署出现问号中文乱码问题

问题描述:
1、数据库原有的汉字数据正常显示
2、在本地添加数据没问题,在服务器上面新添加中文汉字数据显示为???

问题解决过程:
1、首先断定是编码问题,通常默认的编码是UTF-8。
2、本地没问题,线上的tomcat用的是同一个,排除service.xml里面的URIEncoding问题。

添加 URIEncoding=“UTF-8”
,这个是为了解决get请求参数或者路径带中文的时候的编码问题,在windows下如果不添加这个,get请求出现中文参数或者路径一样会出问题,如果是路径,会404,如果是参数,会乱码


3、mysql配置
查看mysql的编码格式,默认mysql的编码配置:server、client都是latin1(拉丁编码集),它对中文是不支持的。
打开Xshell 6 登录mysql:

mysql -uroot -p

查看编码方式:

mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+

我们看到charcter_set_server 的编码还是默认的,所以我们去把这个配置改成utf-8

# vim /etc/my.cnf

#" For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names = 1
character-set-server=utf8
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

按a键进入编辑模式,找到空位加入

character-set-server=utf8

在按a或者i 退出INSERT状态,再按冒号:wq保存退出

138: Can't write viminfo file /root/.viminfo!
Press ENTER or type command to continue

回车退出修改,在重启mysql就OK啦!

参考资料:https://blog.csdn.net/qq_28039297/article/details/76686022

你可能感兴趣的:(linux)