Execute sql statement externally on linux, and redirect the result to file

In the project, after processing the data in spark, write the results to mysql. It needs to be used to output the results of the result table to a file. I am too lazy to write a java program again, thinking about whether mysql can support this similar function. There are still some methods I found below:
1: mysql -h host -u account -p'password' -e "select * from database table"> result file path, the following is a demo, you can copy it directly:
mysql -h localhost -uroot -p'123456' -e "select * from test.user"> /test/result.xls
But everyone must also remember that the statement is executed outside mysql! ! ! , Do not execute after entering mysql! ! !
2: This is actually the first inheritance. It is mainly used in scenarios where SQL is more complicated.
Write SQL as a SQL script, and then execute the script: mysql -h localhost -uroot -p123456 <t.sql> /res/result The .xls
script can be written like this:
user test;
select * from user;

In addition, I have seen a way of spreading on the Internet, but it doesn’t work for me. Friends who are interested can study it. The sentence is as follows:
select user, host, password from test.user into outfile'/tmp/user.xls ';

Guess you like

Origin blog.csdn.net/qq_39719415/article/details/96430273