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 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
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.
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 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!