Mysql遷移由於字符集導致亂碼的數據


  有時候會在不注意的情況下創建了字符集為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)

(2).修改字符

  請查看:各種修改Mysql字符集

  注意:選擇目標字符集時,要注意最好大於等於原字符集(字庫更大),否則可能會丟失不被支持的數據。

(3).數據遷移

1)導出test_mv數據庫

[root@youxi1 ~]# mysqldump -uroot -p123456 --quick --extended-insert --default-character-set=latin1 -B test_mv > test_mv_latin1.sql

  說明:--quick用於轉儲大的表,強制mysqldump從服務器一次一行的檢索數據而不是檢索所有行,並輸出當前cache到內存中。

     --extended-insert:使用包括幾個values列表的多行insert語法,這樣文件更小,IO也小,導入數據時會非常快。

     --default-character-set=latin1:按照原有字符集導出數據,這樣導出的文件中,所有中文都是可見的,不會保存成亂碼。

  注意:我這里使用了-B選項將整個數據庫都導出了。如果使用的是-d選項表示導出表結構,而默認選項是導出整張表,默認選項和--no-create-info一起使用則表示只導出表數據。

(2).編輯test_mv_latin1.sql將latin1修改為utf8

  首先做備份,做好備份后再進行如下操作:

[root@youxi1 ~]# vim test_mv_latin1.sql 

:%s/latin1/utf8/g  //替換文件中的所有latin1為utf8,之后保存退出

(3).刪除原來的數據庫,導入修改后的sql文件

[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 15
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> drop database test_mv;
Query OK, 1 row affected (0.02 sec)

mysql> source /root/test_mv_latin1.sql

(4).查看結果

mysql> use test_mv
Database changed
mysql> select * from user_tb;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 學生   |
|    2 | 老師   |
+------+--------+
2 rows in set (0.00 sec)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM