The Stroz

Executing System Commands in MySQL Shell

May 28, 2024
2 minutes

MySQL MySQL Shell

Over the last few years, I have become quite smitten with MySQL Shell. For those who may not be familiar with MySQL Shell, it is a new(ish) command line interface (CLI) for connecting to and managing MySQL instances. During a recent episode of Inside MySQL: Sakila Speaks, Fred and I talked to Miguel Araujo about many of the helpful (and lesser known) features of MySQL Shell. This post is the second in a series about these “hidden gem” features.

The Problem

When working in MySQL Shell, I often need to run system commands for various reasons. I usually opened another tab in my terminal interface to run the needed command. That was until Miguel mentioned a feature of MySQL Shell that allows us to run system commands inside MySQL Shell.

The Solution

The \system (or \!) command in MySQL Shell allows us to run system commands without leaving MySQl Shell to do so.

Listing Files

Here is an example. In a previous post, I discussed being able to run external scripts in MySQL Shell. Let’s say we created a script but cannot recall the name. If we know what directory the files are in, we can run an ls command directly in MySQL Shell.

\system ls -la ~/projects/shell_scripts

The result of this command would look like:

Result of listing files in a directory

Something to note is that MySQL Shell’s “current directory” is the directory we were in when we started MySQL Shell. For example, I could run the following command to get the same result if I was already in the ~/projects/shell_scripts directory and ran the mysqlsh command.

\! ls -la

Editing Files

Let’s assume we are connected to a remote server and must edit one of the files in the shell_scripts directory. We can open the file in an editor right in MySQL Shell. Here is how to open the file demo2.js using Nano.

\! nano demo2.js

This command opens the file demo2.js in Nano.

JavaScript code displayed in Nano

After editing the file and closing Nano, we are brought back to the MySQL Shell interface.

Animated image of opening a file in nano

Using sudo

If there is a command that you would typically use sudo to run, no worries. You add sudo to the command; if necessary, you will be asked to provide a password.

\! sudo ls /

Wrap-Up

MySQL Shell has many features that help make our lives easier. Running system commands, even those needing sudo, without having to leave MySQL Shell can save a lot of time. This behavior is one of those features you appreciate the more you use it.

Photo by Sora Shimazaki.

Related Entries