前提
环境是在ubuntu下
方法
1.查看一下mysql默认字符集
- character_set_client为客户端编码方式;
- character_set_connection为建立连接使用的编码;
- character_set_database数据库的编码;
- character_set_results结果集的编码;
- character_set_server数据库服务器的编码;
2.修改配置文件
linux系统下,修改MySQL数据库默认编码的步骤为:
- 修改
/etc/mysql/mysql.conf.d/mysqld.cnf
文件
xhost + #如果xhost +不能执行,报错:xhost : unable to open display “”
sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf
- 在 [mysqld ] 下面加入
#我这里更改为utf8mb4是因为存储emoji表情报错。
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
- 修改mysql.cnf文件
sudo gedit /etc/mysql/conf.d/mysql.cnf
- 在[mysql]下方加入
default-character-set = utf8mb4
3.重启mysql
sudo service mysql stop
sudo service mysql start
4.查看更改后的编码方式
好了,设置完毕,可以愉快的存储数据了,还能保存emoji表情。
后续
如果是已经存在的表的话,可以先查看一下表支持的字符集
show create table entries2;
然后根据情况修改表的字符
alter table table_name convert to character set utf8mb4;