How to Run MySQL/MariaDB Queries Directly from the Linux Command Line
Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators | 4 Free Shell Scripting eBooks
If you are in charge of managing a database server, from time to time you may need to run a query and inspect it carefully. While you can do that from the MySQL / MariaDB shell, but this tip will allow you to execute the MySQL/MariaDB Queries directly using the Linux command line AND save the output to a file for later inspection (this is particularly useful if the query return lots of records).
Let us look at some simple examples of running queries directly from the command line before we can move to a more advanced query.
To view all the databases on your server, you can issue the following command:
Next, to create a database table named
tutorials
in the database tecmintdb
, run the command below:
We will use the following command and pipe the output to the
tee
command followed by the filename where we want to store the output.
Suggested Read: 20 MySQL/MariaDB Commands for Database Administration in Linux
For illustration, we will use a database named
employees
and a simple join between the employees and salaries tables. In your own case, just type the SQL query between the quotes and hit Enter.
Note that you will be prompted to enter the password for the database user:
View the query results with the help of cat command.
With the query results in a plain text files, you can process the records more easily using other command-line utilities.
No hay comentarios:
Publicar un comentario