English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Chinese character garbled code solution for MySQL database under Mac:
When using the framework for database storage operations, we often encounter the problem of Chinese characters being garbled.
For example, when using the SSH framework in Java, we need to configure the encoding filter in the web.xml file, the specific code is:
<span style="font-family:FangSong_GB2312;font-size:14px;"><!-- Form processing garbled characters must be set before the OpenSessionInViewFilter filter --> <filter> <filter-name>CharacterFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></span>
Then when storing data in the database, it was found that the Chinese characters stored in the database are still garbled, at this time, debugging the project found that the parameter value passed to the SQL at the end is already normal Chinese.
So at this time, we need to check the database encoding.
Use SQL commands to view the default encoding format:
<span style="font-family:FangSong_GB2312;font-size:14px;">show variables like "%char%";/span>
Check the encoding format of the test database:
<span style="font-family:FangSong_GB2312;font-size:14px;">show create database test;/span>
Then we modify the MySQL encoding under Max:
Since the default character set of the Mac version of MySql is not utf-8, so you need to follow the following steps to modify
Enter in the terminal
<span style="font-family:FangSong_GB2312;font-size:14px;">sudo -s</span>
Then prompt for a password, this password is the system password!
Enter the following command in the command line
<span style="font-family:FangSong_GB2312;font-size:14px;">cd /usr/local/mysql/support-files</span>
Continue to enter (copy the file to the etc directory)
<span style="font-family:FangSong_GB2312;font-size:14px;">cp my-default.cnf /etc/my.cnf</span>
Continue to enter (enter the etc directory)
<span style="font-family:FangSong_GB2312;font-size:14px;">cd /etc</span>
Continue to enter
<span style="font-family:FangSong_GB2312;font-size:14px;">vimy.cnf</span>
At this time, you will find that the my.cnf file has been opened with the vi tool, and its cursor up, down, left, and right movements are through the h, j, k, l keys on the keyboard, and the Mac system version is different10.9.3The system cursor key is effective!
Then press the j key to move the cursor to the end of [client], and add an attribute: (or use the down arrow key)
<span style="font-family:FangSong_GB2312;font-size:14px;">default-character-set=utf8(This attribute mysql 5.5After that, you cannot add this attribute in
added below mysqld, that is5.5In previous versions, ignore this attribute in later versions)</span>
Continue to press the key to move the cursor to the end of [mysqld], and add3one attribute
<span style="font-family:FangSong_GB2312;font-size:14px;">default-storage-engine=INNODB character-set-server=utf8 collation-server=utf8_general_ci</span>
Press the esc key at the upper left corner of the keyboard to exit edit mode, then enter a colon and the letter x
Restart the MySql service
Now, when you perform database operations again, the Chinese characters can be stored normally!
Thank you for reading, I hope it can help you. Thank you for your support to our website!