Install mycli MySQL client AutoCompletion on Ubuntu 18.04 LTS

(: December 9, 2018)

In this guide, we will do the installation and usage of mycli on Ubuntu 18.04. Mycli is a MySQL terminal client written in Python and Python Prompt Toolkit library with AutoCompletion and Syntax Highlighting. It works with MySQL, MariaDB, and Percona database servers.

mycli exist to help you write complex queries easily and faster without the hassle of having to remember the whole query syntax. It uses REPL (Read, Eval, Print, Loop)  which pop up suggestion menu as soon as you start typing.

Features of mycli

Here is the complete list of mycli features as listed in the official website.{text-align:left} img{margin:0 auto 0 0}
  • Auto-completion as you type for SQL keywords as well as tables, views, and columns in the database.
  • Syntax highlighting using Pygments.
  • Smart-completion (enabled by default) will suggest context-sensitive completion.
    • SELECT * FROM <tab> will only show table names.
    • SELECT * FROM users WHERE <tab> will only show column names.
  • Support for multiline queries.
  • Favorite queries with optional positional parameters. Save a query using fs alias query and execute it with f alias whenever you need.
  • The timing of sql statements and table rendering.
  • Config file is automatically created at ~/.myclirc on the first launch.
  • Log every query and its results to a file (disabled by default).
  • Pretty prints tabular data (with colors!)
  • Support for SSL connections

How to install mycli on Ubuntu 18.04

mycli can be installed using pip python package manager or from apt repository.

Install mycli from apt repository

sudo apt update
sudo apt install mycli

Install mycli using pip

sudo apt install python-pip
sudo pip install mycli

or use easy_install

sudo easy_install mycli

How to use mycli

Note that the suggestions are context sensitive based on the position of the cursor. eg: Only tables are suggested after the FROM keyword, only column names are suggested after the WHERE clause.

With Smart Completion ON, Only table names from the current database are suggested after the FROM keyword and Column names from the current table are suggested after the WHERE clause.

To view usage man page, use the command:

# mycli  --help
Usage: mycli [OPTIONS] [DATABASE]

  -h, --host TEXT               Host address of the database.
  -P, --port INTEGER            Port number to use for connection. Honors
  -u, --user TEXT               User name to connect to the database.
  -S, --socket TEXT             The socket file to use for connection.
  -p, --password TEXT           Password to connect to the database
  --pass TEXT                   Password to connect to the database
  --ssl-ca PATH                 CA file in PEM format
  --ssl-capath TEXT             CA directory
  --ssl-cert PATH               X509 cert in PEM format
  --ssl-key PATH                X509 key in PEM format
  --ssl-cipher TEXT             SSL cipher to use
  --ssl-verify-server-cert      Verify server's "Common Name" in its cert
                                against hostname used when connecting. This
                                option is disabled by default
  -v, --version                 Version of mycli.
  -D, --database TEXT           Database to use.
  -R, --prompt TEXT             Prompt format (Default: "t [email protected]h:d> ")
  -l, --logfile FILENAME        Log every query and its results to a file.
  --defaults-group-suffix TEXT  Read config group with the specified suffix.
  --defaults-file PATH          Only read default options from the given file
  --auto-vertical-output        Automatically switch to vertical output mode
                                if the result is wider than the terminal
  -t, --table                   Display batch output in table format.
  --warn / --no-warn            Warn before running a destructive query.
  --local-infile BOOLEAN        Enable/disable LOAD DATA LOCAL INFILE.
  --login-path TEXT             Read this path from the login file.
  -e, --execute TEXT            Execute query to the database.
  --help                        Show this message and exit.

See below example which connect to the local MariaDB database server as a root user:

# mycli -h localhost -u root
Version: 1.8.1
Thanks to the contributor - Nathan Taggart
mariadb [email protected]:(none)>

Typing SHOW and space shows valid options

If you switch to a database and type SELECT * FROM followed by <tab>, all options listed should be for the tables in the current database, and WHERE <tab> will only show column names.

For more information on using and configuring mycli, check the official documentation.{text-align:left} img{margin:0 auto 0 0}