您好,登錄后才能下訂單哦!
小編給大家分享一下如何解決mysql導入文件中文亂碼的問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
mysql導入文件中文亂碼的解決辦法:首先創建數據庫并制定編碼;然后在導入數據庫文件之前,制定編碼set names utf8即可。
繼續昨天的問題,數據庫配置好后,查詢表發現中文亂碼了,看了網上的幾個方法也沒解決。
感覺是導入的sql文件問題,那么逆向的思考,用命令創建個數據庫,添加些數據,然后導出來看看什么情況。
向表內插入數據的時候發現報錯:ERROR 1366 (HY000): Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'bookname' at row 1
果斷再改,通過命令:alter table book change bookname bookname varchar(32) character set utf8;
改完后也不看了,直接插數據試試:
insert into book (id,bookname,size,price) values (1,"測試",2,3) ;
插入成功,查看數據是不是還中文亂碼,發現并不亂碼了:
那么開始進行下一步,導出sql文件,進入到mysql的bin目錄下,開始通過命令導出,這個過程需要輸入密碼
E:\mysql-5.7.28-winx64\bin>mysqldump -u root -p test > test.sql Enter password: *******
導出的文件,表的編碼格式utf8,與之前導入文件的比較并看不出什么問題。
那么,就是之前導入文件的過程,創建數據庫的過程除了問題,查看之前導入的mydb.sql 的mydb數據庫結構,果然不對:
mysql> use mydb; Database changed mysql> show variables like 'character_set_database'; +------------------------+--------+ | Variable_name | Value | +------------------------+--------+ | character_set_database | latin1 | +------------------------+--------+ 1 row in set, 1 warning (0.00 sec)
遂修改之:alert database mydb character set utf8;
然后再查看表的編碼,發現有鬼:
mysql> show create table sp_user_cart; +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | sp_user_cart | CREATE TABLE `sp_user_cart` ( `cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '涓婚敭', `user_id` int(11) unsigned NOT NULL COMMENT '瀛﹀憳id', `cart_info` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '璐?墿杞﹁?鎯呬俊鎭?紝浜岀淮鏁扮粍搴忓垪鍖栦俊鎭', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `delete_time` timestamp NULL DEFAULT NULL, PRIMARY KEY (`cart_id`) ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8
utf8mb4_unicode_ci 這個東西是怎么出來的...
然后改了編碼,查詢,還是不對,想起來之前創建數據庫,可能是忘記設置編碼,導入的東西本來就亂碼了,也或者是my.ini配置不對,從頭開始用正確流程試試吧。
先查看數據庫的編碼設置:show variables like 'character%';
發現很混亂:
mysql> show variables like 'character%'; +--------------------------+----------------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------------+ | character_set_client | gbk | | character_set_connection | gbk | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | D:\mysql-5.7.29-winx64\share\charsets\ | +--------------------------+----------------------------------------+
開始改,通過一系列的setset character_set_client = utf8;
得到結果
+--------------------------+----------------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | utf8 | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | D:\mysql-5.7.29-winx64\share\charsets\ | +--------------------------+----------------------------------------+
創建數據庫并制定編碼:CREATE DATABASE test2 CHARACTER SET utf8 COLLATE utf8_general_ci;
使用:use test2
在導入數據庫文件之前,制定編碼set names utf8;
導入:source F:xxxx\xxxx\mydb.sql;
不算漫長的等待之后,查詢,不亂碼了
mysql> select * from sp_role; +---------+---------------+--------------------------------------------------------------------------------------------- ------------------------------------------------------+----------------------------------------------------------------- ------------------------+--------------------+ | role_id | role_name | ps_ids | ps_ca | role_desc | +---------+---------------+--------------------------------------------------------------------------------------------- ------------------------------------------------------+----------------------------------------------------------------- ------------------------+--------------------+ | 30 | 主管 | 101,0,104,116,115,142,143,144,121,122,123,149,102,107,109,103,111,129,130,134,135,138,139,14 0,141,112,147,125,110,131,132,133,136,137,145,146,148 | Goods-index,Goods-tianjia,Category-index,Order-showlist,Brand-in dex | 技術負責人 | | 31 | 測試角色 | 101,0,104,105,116,117,115,142,143,144,121,122,123,149,103,111,129,134,138,112,147 | Goods-showlist,Goods-tianjia,Category-showlist,Order-showlist,Or der-dayin,Order-tianjia | 測試角色描述 | | 34 | 測試角色2 | 0,105,116,142,143,122 | NULL | 測試描述12 | | 39 | 大發送到 | 101,0,104,105,116 | NULL | 阿斯蒂芬 | | 40 | test | 102,0,107,109,154,155,145,146,148 | NULL
實戰項目可以繼續進行,遂生法喜。
以上是如何解決mysql導入文件中文亂碼的問題的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。