Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

经验分享-让MySQL支持Emoji表情 #59

Open
sunmaobin opened this issue Nov 28, 2018 · 0 comments

Comments

@sunmaobin
Copy link
Owner

commented Nov 28, 2018

一、修改MySQL的配置文件

MySQL的配置文件 my.cnf ,所在位置一般在:/etc/my.cnf,如果不在这个目录下,可以搜索下:whereis my.cnf

修改每个对应项为 utf8mb4 编码:

[client]
default-character-set=utf8mb4
 
[mysql]
default-character-set=utf8mb4
 
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'
character-set-client-handshake = false

修改完成后,重启MySQL,然后查看配置是否生效:

SHOW VARIABLES LIKE 'character_set_%'

如果打印结果如下,则表示配置生效:

character_set_client	utf8mb4
character_set_connection	utf8mb4
character_set_database	utf8
character_set_filesystem	binary
character_set_results	utf8
character_set_server	utf8mb4
character_set_system	utf8
character_sets_dir	/usr/share/mysql/charsets/

二、修改数据库表和字段的编码

要使得某个表、某个字段支持Emoji,就修改表的编码、字段的编码为 utf8mb4

# 修改表的编码
ALTER TABLE `my_table` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
 
# 修改字段的编码
ALTER TABLE `my_table` MODIFY COLUMN `my_column` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

三、(可选)修改Sequelize的配置编码

如果你的数据库表是通过Sequelize这种ORM自动生成,那么需要修改其默认的编码。
如果数据库表是自己创建的,则可以不用管Sequelize的配置。

options: {
   dialect: "mysql",
   dialectOptions: {
       charset: "utf8mb4",
       collate: "utf8mb4_unicode_ci",
       supportBigNumbers: true,
       bigNumberStrings: true
    }
}

@sunmaobin sunmaobin added this to the 2018年 milestone Nov 28, 2018

@sunmaobin sunmaobin self-assigned this Nov 28, 2018

@Riteam

This comment has been minimized.

Copy link

commented Dec 4, 2018

wow

@sunmaobin sunmaobin changed the title 经验分享:让MySQL支持Emoji表情 经验分享-让MySQL支持Emoji表情 Jan 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.