解决macos系统mysql中文乱码的问题

问题描述

通过debug一个数据库保存的实体对象,controller层映射实体并没有乱码如下图所示:
在这里插入图片描述
最终保存到数据库里是以???保存到数据库中的。
在这里插入图片描述
很清晰可以看出并不是后台中文乱码,而是保存到数据库中出现了乱码。

解决方法一

  • 查看mysql的字符集
show variables like '%char%';

查询出的字符集结果如下所示:
在这里插入图片描述
将上图中的character_set_server设置为utf8即可。

  • 设置mysql的字符集
    由于新版mysql并没有存在/etc/my.cnf这个文件,所以在/etc/路径下创建my.cnf文件并设置如下内容即可。
# 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.


[client]
default-character-set=utf8

[mysqld]
character-set-server=utf8

[mysqldump]
user=root
password=123456

# 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 = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# 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
  • 重启mysql
    找到系统偏好设置将mysql服务关闭并重启即可。在这里插入图片描述
  • 再次查看字符集
    在这里插入图片描述
    重新再次保存数据,发现中文并没有乱码问题得以解决。

解决方法二

也有临时更改数据库字符集和表的方法,如下所示:

  • 更改数据库test的字符集
mysql> alter database test default character set utf8;
Query OK, 1 row affected (0.00 sec)
  • 更改数据库test中表tb_person的字符集
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_person      |
+----------------+
1 row in set (0.00 sec)

## 查看表字符集
mysql> show create table tb_person;
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table     | Create Table                                                                                                                                                                                                                                                           |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_person | CREATE TABLE `tb_person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(125) NOT NULL,
  `age` int(10) NOT NULL,
  `salary` decimal(10,2) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

## 将latin1修改为utf8
mysql> alter table tb_person default character set utf8;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值