mysql數據庫亂碼問題

用mysqldump導出數據庫時,若是客戶端字符集和數據庫字符集不一樣,則導出的文件中中文可能爲亂碼。[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 75
Server version: 5.0.77 Source distributionmysql

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.sql

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

Database changedide

mysql> show create table a;
+-------+---------------------------------------------------------------------------------------+
| Table | Create Table                                                                          |
+-------+---------------------------------------------------------------------------------------+
| a     | CREATE TABLE `a` (
  `name` char(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk |
+-------+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)                  能夠看出,表a的字符集爲gbk
 ui

[root@localhost ~]$ mysqldump -uroot --default-character-set=utf8 test a >a.txt
[root@localhost ~]$ more a.txt
-- MySQL dump 10.11
--
。。。。。。

DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
  `name` char(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk;

--
-- Dumping data for table `a`
--

LOCK TABLES `a` WRITE;
/*!40000 ALTER TABLE `a` DISABLE KEYS */;
INSERT INTO `a` VALUES ('涓浗'),('涓浗');
/*!40000 ALTER TABLE `a` ENABLE KEYS */;
UNLOCK TABLES;
/
。。。。。。
用file命令能夠查看導出文件的字符集:
[root@localhost ~]$ file -i a.txt
a.txt: text/plain; charset=utf-8
this

可用iconv將此文件轉換爲其餘字符集:spa

[root@localhost ~]$ iconv -f utf8 -t gbk a.txt >b.txt
[root@localhost ~]$ more b.txt
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
  `name` char(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk;

--
-- Dumping data for table `a`
--

LOCK TABLES `a` WRITE;
/*!40000 ALTER TABLE `a` DISABLE KEYS */;
INSERT INTO `a` VALUES ('中國'),('中國');
/*!40000 ALTER TABLE `a` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

。。。。。。
查看數據中的亂碼已經變成了中文‘中國’
orm

將utf8轉換爲Latin1的時候會報錯:
[root@localhost ~]$ iconv -f utf8 -t latin1 a.txt >b.txt
iconv: illegal input sequence at position 1034
它怎麼會沒法轉換爲latin1呢?試了其餘的字符集,也是一樣問題,彷佛只能在utf8和gbk或者gb2312之間轉換
utf-8

相關文章
相關標籤/搜索