Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to execute a MySQL/MariaDB query in Linux

Shulou Source: shulou.com Published: 2022-06-02 04:59:19 09月23日 Update

If you are responsible for managing the database server, you may need to run the query from time to time and check it carefully. Although you can do this from MySQL/MariaDB Shell, this technique will allow you to execute an MySQL/MariaDB query directly using the Linux command line and save the output to a file for later inspection (this is especially useful when the query returns. A lot of records.

Let's look at some simple examples of running a query directly from the command line, and then move on to a more advanced query. To view all databases on the server, issue the following command:

[linuxidc@localhost ~ / www.linuxidc.com] $mysql-u root-p-e "show databases "+-+ | Database | +-+ | idc_db | | information_schema | | linuxidc_db | | mysql | | performance_schema | | xxx_db | +-+ |

Next, to create a database table named linuxceshi in the database linuxidc_db, run the following command:

[linuxidc@localhost ~ / www.linuxidc.com] $mysql-u root-p-e "USE linuxidc_db; CREATE TABLE linuxceshi (idc_id INT NOT NULL AUTO_INCREMENT, idc_title VARCHAR (100) NOT NULL, idc_author VARCHAR (40) NOT NULL, submissoin_date DATE, PRIMARY KEY (idc_id));"

Check whether the following table has been built or not

[linuxidc@localhost ~ / www.linuxidc.com] $mysql-u root-p-e "use linuxidc_db; desc linuxceshi "+-+-+ | Field | Type | Null | Key | Default | Extra | +-- -+ | idc_id | int (11) | NO | PRI | NULL | auto_increment | | idc_title | varchar | NO | | NULL | | idc_author | varchar (40) | | NO | | NULL | submissoin_date | date | YES | | NULL | | +-+ + |

We will use the following command and pipe the output to the tee command, followed by the file name in which we want to store the output.

For illustration purposes, we will use a linuxceshi table called the linuxidc database. Note that you will be prompted for the password of the database user:

[linuxidc@localhost ~ / www.linuxidc.com] $mysql-u root-p-e "use linuxidc_db; desc linuxceshi;" | tee linuxidc_chaxun.txt

Use the cat command to view the query results.

[linuxidc@localhost ~ / www.linuxidc.com] $mysql-u root-p-e "use linuxidc_db; desc linuxceshi "| tee linuxidc_chaxun.txtEnter password:Field Type Null Key Default Extraidc_id int (11) NO PRI NULL auto_incrementidc_title varchar (100) NO NULLidc_author varchar (40) NO NULLsubmissoin_date date YES NULL [linuxidc@localhost ~ / www.linuxidc.com] $cat linuxidc_chaxun.txtField Type Null Key Default Extraidc_id int (11) NO PRI NULL Auto_incrementidc_title varchar (100) NO NULLidc_author varchar (40) NO NULLsubmissoin_date date YES NULL

Using the query results of a plain text file, you can use other command-line utilities to process records more easily.

Summary

This article has shared a bit of Linux tips, and as a system administrator, you can find useful information about automating daily Linux tasks or performing them more easily.

The original text is from: https://www.linuxprobe.com/linux-mysql-mariadb.html

Tags: Query command data database file output run skill useful server system result and will service check manage practical advanced next task Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Microsoft OPPO Reno Shulou Information