Linux mysql 中文亂碼處理
阿新 • • 發佈:2018-11-14
在專案中,通過tomcat上部署的web伺服器,客戶端通過http向伺服器的mysql插入資料,但插入的中文會變成???這樣的內容,原因有很多
1.伺服器沒有對request和response進行編碼處理,這點好解決,可以通過設定字符集來處理
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //這句話的意思,是讓瀏覽器用utf8來解析返回的資料 response.setHeader("Content-type", "text/html;charset=UTF-8"); service(request, response); }
2.如果這樣仍然會有中文亂碼,可以嘗試在1的基礎上新增filter處理
3.如果設定了filter仍然解決不了中文亂碼,可能就是mysql伺服器出現了問題,可以通過查詢編碼設定來檢視是否有問題@WebFilter("/CharsetFilter") public class CharsetFilter implements Filter { /** * Default constructor. */ public CharsetFilter() { } /** * @see Filter#destroy() */ public void destroy() { } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { } }
這個是設定好的值,如果有中文亂碼問題,一些value的值應該是latin或者是其他的編碼表
4.徹底的解決這個問題,本人通過xftp軟體對linux的檔案進行處理,方便.
找到/etc/my.cnf,替換為一下內容
5.重啟mysql服務即可[mysql] no-auto-rehash default-character-set=utf8 [mysqld] port = 3306 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 default-character-set = utf8 character-set-server = utf8 # Settings user and group are ignored when systemd is used (fedora >= 15). # If you need to run mysqld under a different user or group, # customize your systemd unit file for mysqld according to the # instructions in http://fedoraproject.org/wiki/Systemd user=mysql # Semisynchronous Replication # http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html # uncomment next line on MASTER ;plugin-load=rpl_semi_sync_master=semisync_master.so # uncomment next line on SLAVE ;plugin-load=rpl_semi_sync_slave=semisync_slave.so # Others options for Semisynchronous Replication ;rpl_semi_sync_master_enabled=1 ;rpl_semi_sync_master_timeout=10 ;rpl_semi_sync_slave_enabled=1 # http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html ;performance_schema [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid default-character-set = utf8 [mysql.server] default-character-set = utf8 [client] port = 3306 default-character-set = utf8 socket = /var/lib/mysql/mysql.sock
service mysql stop
service mysql start
或者直接
service mysql restart