文章目录
前言
笔者最近在完成课程实验作业使用MySQL时需要修改编码类型为utf8,但是由于版本为8.0.27,搜寻到的操作建议都是针对老版本的mysql,并不能解决我的问题,所以在此记录我自己试验出的解决办法,以供参考。
一、运行环境
-> ubuntu 20.04
-> mysql 8.0.27
二、更换步骤 1.查看编码类型
首先进入mysql查看自己的编码类型,如果已经是utf8,则无需修改,如果如图所示,则需要进行修改:
mysql> show variables like "char%";+--------------------------+----------------------------+| Variable_name | Value |+--------------------------+----------------------------+| character_set_client | latin1 || character_set_connection | latin1 || character_set_database | utf8mb4 || character_set_filesystem | binary || character_set_results | latin1 || character_set_server | utf8mb4 || character_set_system | utf8mb3 || character_sets_dir | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+8 rows in set (0.04 sec)
注意:在mysql8.0中,utf8mb3就是传统的utf8
2.更改配置文件
由于mysql8.0不会自动在/etc目录下生成my.cnf配置文件,所以需要复制一个过来,代码如下:
root@65f7e1234594:/usr/local/tabao_db_process# cd /etc/mysql/mysql.conf.d/root@65f7e1234594:/etc/mysql/mysql.conf.d# lsmysql.cnf mysqld.cnf
将这里的mysql.cnf复制到/etc目录下
root@65f7e1234594:/etc/mysql/mysql.conf.d# cp mysql.cnf /etc/root@65f7e1234594:/etc/mysql/mysql.conf.d# cd /etc/
再将文件改名为my.cnf进行编辑
root@65f7e1234594:/etc# mv mysql.cnf my.cnfroot@65f7e1234594:/etc# vim my.cnf
使用vim加入如下内容
[client]default-character-set=utf8[mysql]default-character-set=utf8[mysqld]character-set-client-handshake = FALSEcharacter-set-server = utf8collation-server = utf8_unicode_ciinit_connect='SET NAMES utf8'
保存并重启mysql服务
service mysql restart
重启后再进入查看编码即可发现已经修改成功
mysql> show variables like "char%";+--------------------------+----------------------------+| Variable_name | Value |+--------------------------+----------------------------+| character_set_client | utf8mb3 || character_set_connection | utf8mb3 || character_set_database | utf8mb3 || character_set_filesystem | binary || character_set_results | utf8mb3 || character_set_server | utf8mb3 || character_set_system | utf8mb3 || character_sets_dir | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+8 rows in set (0.02 sec)
补充:linux 中的 my.cnf 相当于 windows 中的 my.init
原创文章,作者:筱凯,如若转载,请注明出处:https://www.jingyueyun.com/ask/887.html