mysql> create database wsyht_latin1 default character set latin1; #创建为拉丁字符
mysql> use wsyht_latin1
mysql> create table t1(id int,age int(3),name char(10));
mysql> insert into t1 values(1,25,'wsyht'),(2,26,'peter');
mysql>
mysql> select *from t1;
+------+------+-------+
| id | age | name |
+------+------+-------+
| 1 | 25 | wsyht |
| 2 | 26 | peter |
+------+------+-------+
2 rows in set (0.00 sec)
mysql> insert into t1 values (3,'27','张三');
mysql> select *from t1;
+------+------+-------+
| id | age | name |
+------+------+-------+
| 1 | 25 | wsyht |
| 2 | 26 | peter |
| 3 | 27 | ?? |
+------+------+-------+
3 rows in set (0.00 sec)
mysql> set names latin1; #临时set name设置系统及库表的字符集,临时修改
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t1 values (3,'27','李四');
Query OK, 1 row affected (0.00 sec)
mysql> select *from t1;
+------+------+--------+
| id | age | name |
+------+------+--------+
| 1 | 25 | wsyht |
| 2 | 26 | peter |
| 3 | 27 | ?? |
| 3 | 27 | 李四 |
+------+------+--------+
4 rows in set (0.00 sec)
法2
[root@mysql mnt]# cat test.sql #先set,再执行sql语句,临时修改
set names latin1;
insert into t1 values (8,'29','杨工');
mysql> source /mnt/test.sql
Query OK, 1 row affected (0.00 sec)
mysql> select *from t1;
+------+------+--------+
| id | age | name |
+------+------+--------+
| 1 | 25 | wsyht |
| 2 | 26 | peter |
| 3 | 27 | ?? |
| 3 | 27 | 李四 |
| 3 | 28 | 王五 |
| 8 | 29 | 杨工 |
+------+------+--------+
8 rows in set (0.00 sec)
法3:
[root@mysql mnt]# cat test.sql
set names latin1;
insert into t1 values (7,'28','朱六');
[root@mysql mnt]# mysql -uroot -pwsyht123 wsyht_latin1 < test.sql
mysql> select *from t1;
+------+------+--------+
| id | age | name |
+------+------+--------+
| 1 | 25 | wsyht |
| 2 | 26 | peter |
| 3 | 27 | ?? |
| 3 | 27 | 李四 |
| 3 | 28 | 王五 |
| 7 | 28 | 朱六 |
| 8 | 29 | 杨工 |
+------+------+--------+
8 rows in set (0.00 sec)
法4:
[root@mysql mnt]# cat test.sql
insert into t1 values (4,'28','王五');
[root@mysql mnt]# mysql -uroot -pwsyht123 --default-character-set=latin1 wsyht_latin1 < /mnt/test.sql
mysql> select *from t1;
+------+------+--------+
| id | age | name |
+------+------+--------+
| 1 | 25 | wsyht |
| 2 | 26 | peter |
| 3 | 27 | ?? |
| 3 | 27 | 李四 |
| 3 | 27 | 张三 |
+------+------+--------+
6 rows in set (0.00 sec)
法5:
临时:
set names latin1
永久:
更改my.cnf客户端模块的参数,可以实现set names latin1的效果,并且永久生效
1)更改客户端的方法
[client]
default-character-set=latin1
提示:无需重启服务,退出重新登陆就生效,相当于set name latin1;
2)更改服务端的方法
[mysqld]
default-character-set=utf8 适合5.1及以前版本
character-set-server=utf8 适合5.5
提示:需要重启
查看当前MySQL支持的字符集
mysql -uroot -pwsyht -e "show character set;"
mysql -uroot -pwsyht123 -e "show character set;"|egrep "gbk|utf8|latin1"
库表
create database wsyht default character set uft8 collate utf8_general_ci;
不乱码的思想:建议中英文选择utf-8
linux
[root@mysql mnt]# cat /etc/sysconfig/i18n
LANG="zh_CN.UTF-8"
set name latin1
总结:lain1->uft8
1、建表的语句和数据导出,sed批量修改为utf8
mysqldump -uroot -pwsyht123 test > /opt/t1.sql #导出test数据库所有数据包括表结构
2、修改Mysql服务端和客服端编码为utf8
vim /opt/t1.sql
:%s/latin1/utf8/gc
:wq
3、查看更改的t1.sql数据库
egrep -v "#|\*|--|^$" /opt/t1.sql
4、删除原有的库表及数据
drop database test
4、导入新建库及建表的语句
mysql -uroot -pwsyht123 test < /opt/t1.sql
5、查看字符集
mysql -uroot -pwsyht123 -e "show variables like '%character%'"