1、登录数据库
#!/bin/bash USERNAME=root PASSWORD=1qazXSW@ mysql -u ${USERNAME} -p${PASSWORD} transcoder <<EOF 2>/dev/null show databases; EOF
2、建库建表
#!/bin/bash USER=root PASS=1qazXSW@ DATABASE=test TABLE=test mysql -u${USER} -p${PASS} <<EOF 2>/dev/null create database ${DATABASE}; use ${DATABASE}; create table ${TABLE}( id int, name varchar(100), mark int); desc ${TABLE}; EOF
3、批量插入数据
#!/bin/bash USER=root PASS=1qazXSW@ DATABASE=test TABLE=test data=$1 suc_num=0 fail_num=0 #逐行读取文件数据内容 while read line; do query=`echo $line|awk ‘{printf("%s,\"%s\",%s",$1,$2,$3)}‘` #statement=`echo "insert into $TABLE values($query);"` mysql -u $USER -p${PASS} $DATABASE <<EOF 2>/dev/null insert into $TABLE values($query); EOF #判断插入数据是否成功 if [ $? -eq 0 ];then echo "\"$line\" insert data successful" let suc_num+=1 else echo "\"$line\" insert data failed" let fail_num+=1 fi #echo $query $statement done<$data echo "${suc_num}"条数据插入成功 echo "${fail_num}"条数据插入失败