MySQL字符集转换-从LATIN1转到UTF8

时间:2023-01-10 11:18:53

环境:

MySQL:5.1.73

 

1、创建数据库(不指定字符集)

MySQL字符集转换-从LATIN1转到UTF8

默认的字符集为latin1。

 

2、在数据库中新建表并插入数据,模拟一个已经使用了一段时间的库

mysql> use charset;

mysql> create table t1(name varchar(10), age int(2));

mysql> insert into t1 values('yang',25);

 

3、导出数据,准备切换字符集

MySQL字符集转换-从LATIN1转到UTF8

 

查看sql文件里的内容:

MySQL字符集转换-从LATIN1转到UTF8

MySQL字符集转换-从LATIN1转到UTF8

 

4. 替换charset.sql文件中的"CHARSET=latin1"标记

# sed -i s/CHARSET=latin1/CHARSET=utf8/g charset.sql

 

5.将修改后的charset.sql应用到新的数据库中

mysql> create database new_charset default charset utf8;

mysql> use new_charset;

mysql> source charset.sql;

 

6.现在我们的新库new_charset和原表t1都是utf8字符集了。