本例子环境为centos 7.4 yum安装 mariadb 5.5.56


环境准备:创建latin1编码的库,表,数据

mysql --default-character-set=latin1

show variables like 'char%';

mysql 拉丁1 转换成 utf8_第1张图片

create database latin;

show create database latin;

use latin

create table student (id int,name varchar(10),age int,class varchar(20));

insert into student values (1,'张三',18,'数学');

insert into student values (2,'李四',28,'语文');

show create table student;

select * from student;


正式开始:

1备份数据

mysqldump --default-character-set=latin1 latin >latin.sql


2修改服务端配置文件

vi /etc/my.cnf  在[mysqld]段添加

character_set_server = utf8


3重新启动服务生效

systemctl stop mariadb

systemctl start mariadb


4更改字符编码为utf8

sed -n s/latin1/utf8/gp latin.sql

sed -i s/latin1/utf8/g latin.sql


5创建新库导入数据

mysql --default-character-set=utf8

show variables like 'char%';

create database utf8;

show create database utf8;

use utf8

source /root/latin.sql


6验证

select * from student;