靈活處理mysql導入數據出錯解決 從SQL文件導入數據庫經常會用到,可能是從mysqldump 的腳本恢復數據庫,也可能是數據庫遷移。往往這樣的SQL文件裡包含很多條語句,如果中途某條語句出錯將會導致整個導入任務終止。遇到這種情況應該怎樣處理呢? 先准備一個演示用的SQL腳本 demo.sql [sql] SET NAMES UTF8; CREATE TABLE cc ( `account_id` mediumint(9) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`account_id`) )ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO cc VALUE(1); INSERT INTO cc (2); INSERT INTO cc VALUE(3); 將SQL文件導入到數據庫test [plain] # cat demo.sql | mysql -u root test ERROR 1064 (42000) at line 9: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2)' at line 1 報告錯誤。從提示信息裡看到,出錯的語句是第9行。檢查一下 [plain] # tail -n +9 demo.sql | head -1 INSERT INTO cc (2); 9 前面的+號表示從文件頭部開始計算的第9行。發現一個句法錯誤。用sed 替換某一行 [plain] # sed -i '9s/.*/INSERT INTO cc VALUE(2);/' demo.sql 替換完成。接著剛才出錯的地方繼續導入 [plain] # tail -n+9 demo.sql | mysql -u root test 看看數據庫,數據都在,導入成功 [sql] mysql> select * from cc; +------------+ | account_id | +------------+ | 1 | | 2 | | 3 | +------------+ 3 rows in set (0.00 sec) 來源 http://blog.csdn.net/afeiqiang/article/details/8823194