The problem of Chinese garbled characters when the project is deployed to Linux

The project is deployed on the virtual machine Centos7,

The database uses mysql-5.7.36,

The version of Tomcat is apache-tomcat-9.0.56,

The garbled code problem occurs. When using Linux to run the project, if the front-end page adds Chinese data to the database, the garbled code problem will occur.

After encountering this problem, I first searched for similar problems on the Internet. There are various ways to solve it, and I use the elimination method first.

Let’s blame the project itself first: I put the project into Tomcat in Windows to run, and the functions are all normal, and there is no problem of garbled characters. After testing, the project said that it does not bear the blame.

I have tested Tomcat under Windows before and everything is normal, so the blame is in the hands of Tomcat under Linux. According to some opinions on the Internet, adding UTF-8 to the encoding methods of server.xml and web.xml under the conf, and restarting the server is of no use. The problem is not here, and the garbled characters should still happen.

Then there is only the database left. At first glance, there is indeed a problem. Attach the mysql comparison chart in windows.

(White is for Linux, black is for Windows)

 First of all, there are two parameters that do not match. The encoding method of database and server is the default of the database. The problem can basically be determined here.

Then there is the solution. Most of the articles I read on the Internet point to the my.cnf file. I searched for a long time, but failed.

The search results are as follows:

[root@192 bin]# mysql --help | grep my.cnf
bash: mysql: command not found...
So I searched the database again, what should I do if there is no my.cnf file, a similar method is to build one myself, and then go to Looking for the default.cnf file, I spent time looking for it, but I couldn't find it. I don't give up, I searched for a long time on the Internet, and found that the answers are all around these two files, but I still see different solutions, that is, add ?characterEncoding=UTF-8 at the end of the JDBC url , I added this sentence in the configuration file, then repackaged it, imported the file and ran it under Linux. After testing, no Chinese garbled characters were generated when adding the database, so this method is very direct and effective. Just add it to the configuration file later.

 However, I still tend to change the database properties file to solve the problem, so that it can be solved once and for all, so I will try other methods in the future.

Original Blogger Blog

Project deployment to linux garbled characters_ZyyIsPig's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/qq_44858888/article/details/122510708
Recommended