The problem of garbled characters in mysql mainly occurs in the following four aspects:
1. The character encoding problem set by the url of jdbc
2. Mysql server encoding problem
3. The encoding problem of table setting
4. Mysql Client encoding problem
1. Ways to avoid chaos:
1. Make an encoding set when creating a database
CREATE DATABASE `zkcfg` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
2. Create a data set when creating a data table
CREATE TABLE tb_datacfg ( dataId Varchar (200) not null, data Text not null, createTime TIMESTAMP null, creator Varchar (32) null, modifyTime TIMESTAMP null, modifier Varchar (32) null, primary key (dataId) )ENGINE=InnoDB default charset utf8 collate utf8_general_ci;
2. Troubleshooting
1. If the data table has been created, and there is a problem of garbled characters, you need to check it yourself. The methods are as follows:
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/ | +--------------------------+----------------------------+
The above results show that the encoding of database and server are latin1 encoding and need to modify the configuration, use the following statement:
SET character_set_database='utf8'; SET character_set_server='utf8';
2. View the encoding of the database you created
mysql> show create database zkcfg; +----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | zkcfg | CREATE DATABASE `zkcfg` /*!40100 DEFAULT CHARACTER SET latin1 */ | +----------+------------------------------------------------------------------+
The result shows that the encoding of zkcfg data is latin1, which needs to be modified to utf-8. The statement is as follows:
ALTER DATABASE `zkcfg` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
3. Modify the my.cnf file to add the following configuration
[client] port = 3306 socket = /var/lib/mysql/mysql.sock default-character-set=utf8 [mysql] default-character-set=utf8 [server] character_set_server=utf8 [database] character_set_database=utf8
After modifying the my.cnf file, you need to restart the mysqld service;
service mysqld restart
end