Home  >  Article  >  Backend Development  >  Mysql method to execute sql in the terminal and write the results to a file

Mysql method to execute sql in the terminal and write the results to a file

jacklove
jackloveOriginal
2018-06-15 17:25:202881browse

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 do not need to enter mysql, but directly use the shell to call mysql in the terminal to execute sql statements.

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.

When using mysql to execute statements in the terminal, we usually 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 files, which affects 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 mysql executes sql in the terminal and writes the results to a file. For more related content, please pay attention to the php Chinese website.

Related recommendations are:

php uses token bucket algorithm to implement flow control based on redis

Redis master-slave synchronization, Related operations of read-write separation settings

Introducing the method of MySQL rebuilding table partitions and retaining data

The above is the detailed content of Mysql method 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