SQL file batch import MySQL database

Source: Internet
Author: User

Have more than 100 SQL files swollen? Importing a database row by line is certainly a foolish thing to do, but I almost did it ...

Online first find the method is: Write a xxx.sql file, inside each line is the source *.sql ..., then in MySQL to call the source Xxx.sql, the result fails, failure reason: Error 2, cannot open file Xxxxxxxxx, probably like this. This method seems to be available on Windows, but it seems like you have to put the SQL file in the Mysql/bin directory to take effect.

The solution that was then found was to use the shell script, the script source http://www.linuxidc.com/Linux/2015-01/111175.htm

Script code

#!/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 $ '

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

I put the inside time some things to go, have a moment what is can let oneself see something, actually a bunch of notice and error is already enough to see ... Then just take it and use it.

SQL file batch import MySQL database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.