#第一种方法在命令行中直接执行下面代码(.log可替换为其他格式文件)
mysql -hlocalhost -P3306 -uroot -p1234 -e"select * from demo.user;" >d:/mywork/shell/1.log
输出结果:
#第二种方式是shell脚本导出.csv
#!/bin/bash
MYSQL="mysql"
#show databases in mysql
echo "database in mysql:"
echo "==========="
$MYSQL -u root -p1234 << EOF
show databases;
EOF
echo "==========="
#choose a database
read -t 60 -p "choose a database:" database
#show tables in the database
echo "tables in $database"
echo "==========="
$MYSQL -u root -p1234 << EOF
use $database
show tables;
EOF
echo "==========="
#choose a table
read -t 60 -p "choose a table:" table
statement="use $database;select * from $table;"
#write the table into 1.log file
$MYSQL -u root -p1234 >1.log << EOF
$statement
EOF
echo "Downloading $table from $database in mysql..."
sleep 1
echo "now converting it to csv file..."
sleep 1
#cat the 1.log file and convert it to csv file
cat 1.log | while read line
do
echo $line | tr " " ","
done > $database"_"$table.csv
sleep 1
#remove the temporal file 1.log
rm -rf 1.log
#echo the infomation
echo "Convert $table into $database"_"$table.csv."
sleep 1
echo "Done successfully!Please check the file!"
输出结果:
转:https://blog.youkuaiyun.com/jclian91/article/details/78522629