做开发时很多时候会碰到乱码的问题;
有的时候可能是从后台传到数据库时乱码,这时候可能就是数据库默认的字符集编码有问题
默认的字符集为
mysql配置文件为my.ini(在linux下为my.cnf)
[mysqld]
# 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.
# 设置mysql的安装目录
basedir = D:/soft/mysql-5.6.36-winx64
# 设置mysql数据库的数据的存放目录,必须是data
datadir = D:/soft/mysql-5.6.36-winx64/data
port =3306
# 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
在doc下进入mysql
输入show variables like 'character%';查看字符集
结果如下:
+--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | latin1 | | character_set_system | latin1 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+
解决方案:
修改配置文件
mysql配置文件为my.ini(在linux下为my.cnf)
找到[mysqld] 添加:
character-set-server=utf8init_connect='SET NAMES utf8'
然后在最后面添加
[client]
default-character-set=utf8
修改后为:
[mysqld]
# 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.
# 设置mysql的安装目录
basedir = D:/soft/mysql-5.6.36-winx64
# 设置mysql数据库的数据的存放目录,必须是data
datadir = D:/soft/mysql-5.6.36-winx64/data
port =3306
character-set-server=utf8
init_connect='SET NAMES utf8'
# 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服务
然后在查看mysql编码
+--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+
搞定。