Home >Database >Mysql Tutorial >How to use mysql to execute sql in the terminal and write the results to a file

How to use mysql to execute sql in the terminal and write the results to a file

jacklove
jackloveOriginal
2018-06-08 15:47:411779browse

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.

Terminal execution sql

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

Write the execution results to the file

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

Explain the relevant content of PHP using the token bucket algorithm to implement flow control based on redis


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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn