Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to execute a MySQL/MariaDB query directly from the Linux command line

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to execute the MySQL/MariaDB query directly from the Linux command line. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

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.txt Enter password: Field Type Null Key Default Extra idc_id int (11) NO PRI NULL auto_increment idc_title varchar (100) NO NULL idc_author varchar (40) NO NULL submissoin_date date YES NULL [linuxidc@localhost ~ / www.linuxidc.com] $cat linuxidc_chaxun.txt Field Type Null Key Default Extra idc_id int (11) NO PRI NULL auto_increment idc_title varchar (100) NO NULL idc_author varchar (40) NO NULL submissoin_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.

This is the end of the article on "how to execute MySQL/MariaDB query directly from the Linux command line". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report