MySQL5.7插入中文数据,报错:Incorrect string value: ‘\xB8\xDF\xAE\xF9\x80 at row 1
- 首先检查数据库的字符编码,因为默认下载的MySQL数据库的字符集为
latin1
show create database <数据库名>
mysql> show create database information_company;
+---------------------+------------------------------------------------------------------------------+
| Database | Create Database |
+---------------------+------------------------------------------------------------------------------+
| information_company | CREATE DATABASE `information_company` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+---------------------+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
- 修改数据库的编码格式
alter database <数据库名> character set utf8;
mysql> alter database information_company character set utf8;
Query OK, 1 row affected (0.00 sec)
- 然后不保护以外的话插入数据应该还会报错…
- 这个时候我们就需要修改
my.ini
文件 - 本人下载的是MySQL5.7
my.ini
文件默认是在C:\ProgramData\MySQL\MySQL Server 5.7
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rj3TNplQ-1635473580693)(/picture/ProgramData.png)]
- 打开“my.ini”。(修改之前尽量备份)。找到
[client]
在下面添加default-character-set=utf8mb4
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
[client]
default-character-set=utf8mb4
- 在找到
[mysqld]
在下面添加character-set-server=utf8mb4
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
# server_type=3
[mysqld]
character-set-server=utf8mb4
# collation-server=utf8_general_ci
-
然后拷贝一份到我们MySQL的安装目录(默认安装在
C:\Program Files\MySQL\MySQL Server 5.7
)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qx7veb65-1635473580696)(/picture/安装目录.png)] -
最后重启mysql服务
右键此电脑 —> 管理
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DUVrCAOn-1635473580698)(/picture/服务.png)]
在键盘上敲入mysql,找到mysql服务,点击重新启动
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GIjS6DmP-1635473580700)(/picture/重新启动.png)]