1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > shell脚本将mysql数据库中查询结果导出制作成.CSV格式文件;或.txt/.log文件

shell脚本将mysql数据库中查询结果导出制作成.CSV格式文件;或.txt/.log文件

时间:2018-11-28 05:29:57

相关推荐

shell脚本将mysql数据库中查询结果导出制作成.CSV格式文件;或.txt/.log文件

#第一种方法在命令行中直接执行下面代码(.log可替换为其他格式文件)mysql -hlocalhost -P3306 -uroot -p1234 -e"select * from demo.user;" >d:/mywork/shell/1.log

输出结果:

#第二种方式是shell脚本导出.csv#!/bin/bashMYSQL="mysql"#show databases in mysqlecho "database in mysql:"echo "==========="$MYSQL -u root -p1234 << EOFshow databases;EOFecho "==========="#choose a database read -t 60 -p "choose a database:" database#show tables in the databaseecho "tables in $database"echo "==========="$MYSQL -u root -p1234 << EOFuse $databaseshow tables;EOFecho "==========="#choose a tableread -t 60 -p "choose a table:" tablestatement="use $database;select * from $table;"#write the table into 1.log file$MYSQL -u root -p1234 >1.log << EOF$statementEOFecho "Downloading $table from $database in mysql..."sleep 1echo "now converting it to csv file..."sleep 1#cat the 1.log file and convert it to csv filecat 1.log | while read linedoecho $line | tr " " ","done > $database"_"$table.csvsleep 1#remove the temporal file 1.logrm -rf 1.log#echo the infomationecho "Convert $table into $database"_"$table.csv."sleep 1echo "Done successfully!Please check the file!"

输出结果:

转:/jclian91/article/details/78522629

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。