EmergencyEMERGENCY? Get 24/7 Help Now!

DBA 101: Sometimes forgotten functionality in the MySQL client


Posted on:

|

By:


PREVIOUS POST
NEXT POST
Share Button

The MySQL client has some functionalities some of us never use. Why would you use them and what is the added value of this?

Every DBA and developer has had a moment when he or she needs to connect to a MySQL database using the command line tool. Therefore I’ve written down an explanation of some command line commands you can insert in the CLI, most of them give added value and make your experience with the cli more enjoyable.

prompt

Who has never witnessed the scary feeling of not being connected to the write database when having several terminals open. I do, due to the fact I use the prompt functionality.

Or you can go a bit further and visualise the user, host and active database in:

edit

In some situations editing the query in an editor instead of the cli can have several enhancements. It gives you the ability to fix typos, have a deep look at the queries before you submit them and etc.
If you’d like to edit the query you are making in your default editor instead of using the cli.

The editor appears, in which you can create your query/ies.

After closing this down and putting a delimiter in the cli, this query will be run against the database while outputting in the prompt.

tee

Performing critical maintenance on a database could require to have an entire log of performed queries and actions. You can activate the full output of the MySQL client, including your performed queries. This utility is ideal if you prefer having a log of all of your actions. This could be for documentation stakes or a way to reread your actions if issues would occur.

Example:

This will provide in the output the queries you perform.

Whenever, you have only access to the MySQL interface and you need to access one of your created files to see what the output is, you can do so using the \! keystroke, which will execute system commands.
Running a shell command from the MySQL command line interface:

status

In some cases you’d like seeing the parameters currently active on your MySQL client. Therefore you can actually use the \s command. This command will clarify which of the options are active on the client. The info which is shown should not be confused with SHOW VARIABLES. which is focussed on the connection variables.
Trigger the status information of your connection using \s:

clear

Clearing your current input query. Use \c to clear the input field:

pager

Honestly one of the more useful tools in the mysqlclient is actually pager. For people prone to typing queries while forgetting adding a limit if they don’t need to view the full output.
‘\P less’ will output the query data using the UNIX command less.
You can also choose to output the query results in a parsable format on the filesystem using ‘\P cat > voila.log’.

for example:

This will create the file ‘voila.log’ in which only the output is saved of the query. This solution mimics the ‘SELECT INTO OUTFILE’ query.

During optimisation of your workload, it can be interesting to see if a query you’ve modified has the same query output

Other commands

There are of course several other options you might use. You can get an overview of all the internal cli functions through \?.

Disabling the tee and pager commands described in this blogentry can be done with \t or notee, \n or \nopager.

Share Button
PREVIOUS POST
NEXT POST


Dimitri Vanoverbeke

At the age of 7, Dimitri received his first computer, since then he has felt addicted to anything with a digital pulse. Dimitri has been active in IT professionally since 2003 in which he took various roles from internal system engineering to consulting. Prior to joining Percona, Dimitri worked as a Open Source consultant for a leading Open Source software consulting firm in Belgium. During his career, Dimitri became familiar with a broad range of open source solutions and with the devops philosophy. Whenever he's not glued to his computer screen, he enjoys travelling, cultural activities, basketball and the great outdoors. Dimitri is living with his girlfriend in the beautiful city of Ghent, Belgium.



Tags:

, ,

Categories:
Insight for DBAs, MySQL


Comments

Leave a Reply

Percona’s widely read Percona Data Performance blog highlights our expertise in enterprise-class software, support, consulting and managed services solutions for both MySQL® and MongoDB® across traditional and cloud-based platforms. The decades of experience represented by our consultants is found daily in numerous and relevant blog posts.

Besides specific database help, the blog also provides notices on upcoming events and webinars.

Want to get weekly updates listing the latest blog posts? Subscribe to our blog now! Submit your email address below.

No, thank you. Please do not ask me again.