#第一种方法在命令行中直接执行下面代码(.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