有时候会在不注意的情况下创建了字符集为latin1的数据库,导致后续插入的中文显示乱码。这时有两种方法:1.修改数据库与数据表的字符集(只能向上调整,不能向下调整);2.数据迁移。但是两种方法都需要做好备份,谨慎操作。

  创建测试环境:

[root@youxi1 ~]# vim user_tb.sql  //创建一个sql脚本
drop database if exists test_mv;
create database test_mv character set latin1;  //因为我默认的是UTF-8字符所以这里指定字符集
use test_mv;

create table user_tb(
  id int,
  name varchar(20)
)CHARSET=latin1;  //指定字符集的原因和上面一样
/*!40101 SET character_set_client = latin1 */;  //由于我是UTF-8所以需要,否则无法导入汉字。

lock tables user_tb write;
insert into user_tb values (1,'学生'),(2,'老师');
unlock tables;
[root@youxi1 ~]# mysql -u root -p123456 < user_tb.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@youxi1 ~]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test_mv;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from user_tb;  //汉字显示乱码
+------+---------------+
| id   | name          |
+------+---------------+
|    1 | 学生        |
|    2 | 老师        |
+------+---------------+
2 rows in set (0.00 sec)

更多相关文章

  1. Mysql基础之字符集与乱码
  2. JavaScript正则表达式定义字符集
  3. 【整理】更改MSSQL默认字符集
  4. 字符集问题的初步探讨(五)----如何识别导出文件的字符集
  5. 更改SQLServer实例默认字符集
  6. 安卓+servlet+MySql 查询+插入(汉字乱码解决)
  7. 数字金额转换成汉字
  8. java使用PinYin4j将汉字转换为拼音

随机推荐

  1. Linux记录-HDFS副本机制
  2. Linux curl 命令模拟 POST/GET 请求
  3. 检查进程是否仍在运行
  4. LTP--linux稳定性测试 linux性能测试 ltp
  5. Linux Shell编程参考大全
  6. 高级Linux工程师常用软件清单
  7. linux系统更改目录和文件的权限总结
  8. linux基本操作(2)
  9. Linux-HA 入门指南(引用)
  10. Centos 7 利用LVM实现动态扩容