Home >Database >Mysql Tutorial >How to use mysql to execute sql in the terminal and write the results to a file
When using mysql to execute statements in the terminal, we generally enter mysql first, and then execute the sql statement in it.
For example:
mysql -uroot mysql> use mydb; mysql> select * from user;...
If we need to monitor certain tables and save the query results after execution, we need to manually copy them to a file, which will affect Work efficiency.
Therefore, the following method is provided. You can directly use the shell to call mysql in the terminal to execute sql statements without entering mysql.
mysql provides -e parameters for executing statements directly on the terminal and outputting them.
For example:
mysql -uroot -e 'select * from mydb.user'
When using mysql -e to execute a statement, you can add the -v, -vv, -vvv parameters
-v Display the statement itself
-vv Increase the number of displayed query result rows
-vvv Increase the display execution time
Use output redirection to save the execution results to the file.
For example:
mysql -uroot -e 'select * from mydb.user' > /tmp/mydb_user.txt
After the terminal is executed, the query results will be saved in /tmp/mydb_user.txt.
This article explains how to use mysql to execute sql in the terminal and write the results to a file. For more related content, please pay attention to the php Chinese website.
Related recommendations:
How to compare the table structures of two databases through mysql
Explain how to use mysql binlog
The above is the detailed content of How to use mysql to execute sql in the terminal and write the results to a file. For more information, please follow other related articles on the PHP Chinese website!