shell执行mysql脚本
执行sql文件
#/bin/bash host='127.0.0.1' dbUser='root' dbPassword='' sql_file='test.sql' city_array=('bj' 'sh' 'tj') type="test" for city in ${city_array[@]}; do dbName=${type}'_'${city} echo $dbName #多库建表 mysql -h $host -u $dbUser -p$dbPassword -f $dbName -e "source $sql_file"; done
直接执行sql
#/bin/bash host='127.0.0.1' dbUser='root' dbPassword='' city_array=('bj' 'sh' 'tj') type="test" for city in ${city_array[@]}; do dbName=${type}'_'${city} echo $dbName #多库增加字段 mysql -h $host -u $dbUser -p$dbPassword -e " use $dbName; ALTER TABLE test_table1 ADD refresh_time int(11) DEFAULT '0' COMMENT '刷新时间'; notee #关闭记录功能 quit"; done
获取sql执行结果
result=`mysql -u$user -p$pass -D $db -e "select host from user;"`
也可以导出来再处理
result=`mysql -u$user -p$pass -D $db -e "select host from user;"` echo $result >2