在测试一个项目中,发布一个内容中包含emoji表情就会报错,原因是mysql没有设置支持utf8mb4,windows下修改mysql的my.ini配置文件,Linux下mysql配置文件是my.cnf
有的mysql配置文件在mysql安装路径下,有的在ProgramDate下的mysql文件夹里
将mysql默认编码改为utf8mb4:
1.关闭mysql(也可以修改配置文件后再重启mysql服务)
2.修改my.ini配置文件
在[client] 下添加:
default-character-set = utf8mb4
在[mysql] 下添加:
default-character-set=utf8mb4
在[mysqld] 下添加:
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
找的方法修改完这些重启mysql就可以使用了,但是我的发现重启mysql失败,于是继续百度,发现了问题:
在mysql配置文件[mysqld] 下有这样一句:
# The default character set that will be used when a new schema or table is
# created and no character set is defined
character-set-server=utf8
将utf8修改成utf8mb4即可重启mysql服务,但个人看到这句话和上面添加的内容重复,觉得可以删除,但是我没有删
3.重启mysql服务
可以通过设置里的本地服务中来重启mysql服务,
也可以通过cmd进行重启:
1)、启动:net start mysql57
2)、关闭:net stop mysql57
4.查看是否设置成功
cmd下登录mysql,然后输入命令:
SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';

到此修改完成,再次测试发现已经可以存emoji表情啦
附上修改部分的配置文件代码:
#
[client]
default-character-set = utf8mb4
# pipe=
# socket=0.0
port=3306
[mysql]
no-beep=
default-character-set=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-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
# The next three options are mutually exclusive to SERVER_PORT below.
# skip-networking=
# enable-named-pipe=
# shared-memory=
# shared-memory-base-name=MYSQL
# The Pipe the MySQL Server will use
# socket=MYSQL
# The TCP/IP Port the MySQL Server will listen on
port=3306
# Path to installation directory. All paths are usually resolved relative to this.
# basedir="C:/Program Files/MySQL/MySQL Server 5.7/"
# Path to the database root
datadir=C:/ProgramData/MySQL/MySQL Server 5.7/Data
# The default character set that will be used when a new schema or table is
# created and no character set is defined
character-set-server=utf8mb4
# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
如有错误,欢迎指出。
在项目测试中,发布含emoji表情内容报错,原因是MySQL未支持utf8mb4。介绍了Windows下修改my.ini、Linux下修改my.cnf配置文件的方法,将默认编码改为utf8mb4,还说明了重启服务、查看设置是否成功的操作,最终可实现存储emoji表情。
2460

被折叠的 条评论
为什么被折叠?



