EnArudinAiur

sql文件批量导入mysql数据库

    有一百多个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就已经够看了。。。然后直接拿过来用就好了。

发表于 2016-08-03 12:09  EnArudinAiur  阅读(536)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章:

  • 2021-07-20
  • 2021-12-07
  • 2021-11-21
  • 2021-11-30
  • 2021-04-19
  • 2021-12-05
  • 2021-12-12
  • 2021-12-06
猜你喜欢
  • 2021-04-07
  • 2021-10-21
  • 2021-10-12
  • 2021-10-24
  • 2021-12-06
  • 2021-08-01
  • 2021-12-19
相关资源
相似解决方案