1.检查确认mysql编码,正确编码如下图所示
show variables like '%char%'
2.若第一步编码异常,则修改配置文件,重启mysql服务
主要添加如下配置:
# 配置mysql服务端为utf8编码
[mysqld]
character-set-server=utf8
# 配置mysql客户端为utf8编码
[client]
default-character-set=utf8
# 配置mysql命令行操作编码
[mysql]
default-character-set=utf8
window配置文件为mysql安装目录下的my.ini,配置如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
character-set-server=utf8
# 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
# These are commonly set, remove the # and set as required.
basedir = D:/Application/program/database/MySql/mysql-5.7.10-winx64/
datadir = D:/Application/program/database/MySql/mysql-5.7.10-winx64/data/
# port = .....
# server_id = .....
# 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
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
linux配置如下(配置文件路径为:
/etc/my.cnf
):
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
# port = .....
# server_id = .....
socket = /tmp/mysql.sock
#该mysql.sock文件必须放在一个服务端跟客户端都可以访问的位置
log_error = /usr/local/mysql/log/mysql.log
#配置日志文件
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysql]
socket=/tmp/mysql.sock
[mysql.server]
user=mysql
basedir=/usr/local/mysql
[mysqladmin]
socket=/tmp/mysql.sock
[mysql.server]
user=mysql
basedir=/usr/local/mysql
[safe_mysqld]
err-log=/usr/local/mysql/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid
3.若表还是显示中文乱码,或者无法插入中文,则查看表DDL
show create table table_name;
保证表编码为utf8