Customizing The MySQL Shell Prompt

The style and format of the prompt in MySQL Shell can be customized to meet the desires of the person using it. We can configure the prompt to display different or trimmed-down information about our database connection and what mode we are using. This post will show how to add a visual cue to let you know when you are connected to a production database.
The Setup
Since you are reading about customizing MySQL Shell, you should already have it installed. If you don’t, download and install it before you continue.
Prompt Theme Files
The easiest way to customize the MySQL Shell prompt is to copy an existing prompt theme file. Several prompt files are included with MySQL Shell. These files are located in the following directories:
- Windows -
%programfiles%\MySQL\MySQL Shell 8.0\share\mysqlsh\prompt\
- MacOs -
/usr/local/mysql-shell/share/mysqlsh/prompt/
- Linux -
/usr/share/mysqlsh/prompt/
The list of files in these folders will resemble:
For information on what the different files include, check out README.prompt
.
We copy the prompt file we want to use as our base to the following locations:
- Windows -
%AppData%\Roaming\MySQL\mysqlsh\
- MacOS & Linux -
~/.mysqlsh/
I will use the prompt_256pl.json
for this demo because I like the Powerline font aesthetic. Here is what the default JSON looks like.
If MySQL Shell is already open, we must restart it to add the theme. When we start MySQL Shell with this file as it is, we will see the following:
Here is what it looks like when I connect to a MySQL instance, in this case, over SSH.
Prompt Background Color
When I used this prompt theme, I first noticed that the prompt’s background was a different color from the background of my terminal.
This color difference makes me all twitchy. Since I am not using black as the background color for my terminal, I need to change the background color of the prompt. Yes, I could change the background of my terminal to black, but then you wouldn’t learn how to change the prompt background.
The block of JSON that manages the background color of the prompt (and should be around line 102) is:
The bg
property controls the background color of the prompt. In this case, it is set to 0
, which is black. I could do trial and error to find a color close to the background color, but if I did it that way, I would need to change the color again if I ever chose a different background color for my terminal. So, the easy way to address this color difference is to remove that setting. I changed the prompt
block to:
When I restart MySQL Shell, we can see that the background color for the prompt now matches the terminal background.
If I change my terminal theme, I will not need to change the prompt background again. Take a look below.
Update Prompt Text
The next thing that bothers me is that the host information is a bit long because it includes the SSH host and then the database host. I prefer shorter prompt text, so we are going to shorten this. The block of JSON we will modify to accomplish this is around line 94 in the transport
variable definition.
This block tells MySQL Shell that if the value of %ssh_host%
is an empty string, set the value of the variable named transport
to the value of the %target%
variable—which will usually be the host
and port
separated by a colon (:
).
If the value of %ssh_host%
is any value other than an empty string, the value of transport
is set to the value of %ssh_host%
, followed by a character value for a right arrow, and ending with the value of the %target%
variable.
For my purposes, the port number is not necessary. I am also not a fan of the extra spaces around the arrow. To fix this, I change the if_false
line to the following:
When I restart MySQL Shell and connect to the database again, the prompt looks like the image below. Note that the port is no longer shown, and the spaces around the arrow have been removed.
Visual Cue For Production
I often have multiple database connections running at the same time. Because of this, I want to add a visual cue to let me know when I am connected to the production database. We already have a variable named is_production
in our JSON. The definition for this variable is around line 72 and looks like the JSON below.
The default definition looks for an environment variable named PRODUCTION_SERVERS
to see if the value matches that of the %host%
variable. If it does, the value of is_production
is set to production
. If it does not match, that value of is_production
is set to an empty string.
Instead of setting an environment variable, I will hardcode the values for this demo.
Also, because I am using SSH to connect to my production server, I will change the pattern to use the %ssh_host%
variable. My changes are reflected in the JSON below.
If the SSH host is gl-db-server
, we should see a segment on the prompt indicating we are connected to a production server.
Change Class Values
This prompt is better, but the text color is supposed to be white. I am not sure why it is not, but I think it is because of accessibility, and the colors do not have enough contrast. To change the colors of the production
label, we look at the production
class that is around line 65.
I will change the bg
and fg
colors to have more contrast. I will also change the value of text
to PROD
rather than PRODUCTION
because we just regained some real estate on the prompt, and now we lost some of it again. My new block of JSON looks like this:
I restart MySQL Shell and reconnect to see the changes.
Moving a Segment
In our prompt, the parts that show different information are called ‘segments’. We can add, remove, and move segments in the theme. I want to move the PROD
label toward the end of the prompt to catch my eye easier.
In our JSON file, the segments are defined with an array of JSON objects like the text below.
To move the production, we first remove "%is_production%"
from the classes
property in the first element of the segments
array. This segment should now look like the following:
Next, we create a new JSON object before the last segment with an element in the classes
array with a %Mode%
value. We create a classes
property as an array and add %is_production%
as an element. The last three elements of segments
should resemble the JSON below.
When we restart MySQL Shell and reconnect, the PROD
label is closer to the prompt’s end, making it more easily visible.
README.prompt
The README.prompt
file contains a lot of information about customizing our prompt. I am including the contents of that file below so that you (and I) have a reference that might be easier to locate.
Wrap Up
Customizing the prompt in MySQL Shell can allow us to show information we want and hide information we don’t want. We can also add visual cues to let us know when we are connected to production servers, and we can move the different segments so they fit our needs better. To learn more about customizing the prompt in MySQL Shell, check out the documentation and also take a look at the contents of the README.prompt
file.
Related Entries
- Running External Scripts in MySQL Shell
- Executing System Commands in MySQL Shell
- Getting Help in MySQL Shell
- Sandboxes in MySQL Shell
- Server Upgrade Checks with MySQL Shell
- MySQL Connection Status with MySQL Shell
- Managing MySQL Shell Configuration Options
- Advanced Data Dump with MySQL Shell
- Advanced Data Load with MySQL Shell
- MySQL Shell CLI Integration
- MySQL Shell Table Export Utility
- Copy Data Directly To A MySQL Instance With MySQL Shell
- Create an InnoDB Replica Set With MySQL Shell
- Using MySQL Shell to Get MySQL Diagnostic Information
- Extending MySQL Shell with Startup Scripts
- Extending MySQL Shell with Plugins