sql檔案大量匯入mysql資料庫

來源:互聯網
上載者:User

標籤:

    有一百多個sql檔案腫麼破?一行一行地匯入資料庫肯定是極其愚蠢的做法,但是我差點就這麼做了。。。

    網上首先找到的方法是:寫一個xxx.sql檔案,裡邊每一行都是source *.sql ...,之後再mysql中去調用source xxx.sql,結果失敗,失敗原因:Error 2, cannot open file xxxxxxxxx,大概這個樣子。這種方法貌似在windows上是可用的,不過貌似還要把sql檔案放在mysql/bin目錄下才可以生效。 

    之後找到的解決方案是利用shell指令碼,指令碼來源http://www.linuxidc.com/Linux/2015-01/111175.htm

    指令碼代碼

    

#!/bin/bash

file_path="//////////" #要匯入的sql檔案夾
host="localhost" #要匯入的mysql主機
username="root" #mysql的使用者名稱
password="root" #mysql的密碼
dbname="xxxx" #mysql的資料庫名

mysql_source(){

    for file_name in `ls -A $1`

        do

            if [ -f "$1$file_name" ];then

                command="source $1$file_name"

                mysql -h${host} -u${username} -p${password} ${dbname} -e "$command"

        done

}

mysql_source $file_path

    我把裡邊計時的一些東西去了,有個時間什麼的就是能讓自己看到點東西,其實一堆notice和error就已經夠看了。。。然後直接拿過來用就好了。

sql檔案大量匯入mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.