Wednesday, July 3, 2024
HomeDatabasesInstall mycli MySQL AutoCompletion on Ubuntu 22.04|20.04|18.04

Install mycli MySQL AutoCompletion on Ubuntu 22.04|20.04|18.04

In this guide, we will do the installation and usage of mycli on Ubuntu 22.04|20.04|18.04 Linux systems. 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.

  • 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

Installing mycli on Ubuntu 22.04|20.04|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

Installing mycli using pip:

# Python 3
sudo apt install python3-pip
sudo pip3 install mycli

# Python 2
sudo apt install python-pip
sudo pip install mycli

or install mycli using easy_install

sudo easy_install mycli

Using mycli MySQL Client Autocompletion tool

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]

  A MySQL terminal client with auto-completion and syntax highlighting.

  Examples:
    - mycli my_database
    - mycli -u my_user -h my_host.com my_database
    - mycli mysql://my_user@my_host.com:3306/my_database

Options:
  -h, --host TEXT               Host address of the database.
  -P, --port INTEGER            Port number to use for connection. Honors
                                $MYSQL_TCP_PORT.
  -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.
  --ssh-user TEXT               User name to connect to ssh server.
  --ssh-host TEXT               Host name to connect to ssh server.
  --ssh-port INTEGER            Port to connect to ssh server.
  --ssh-password TEXT           Password to connect to ssh server.
  --ssh-key-filename TEXT       Private key filename (identify file) for the
                                ssh connection.
  --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                 Output mycli's version.
  -v, --verbose                 Verbose output.
  -D, --database TEXT           Database to use.
  -d, --dsn TEXT                Use DSN configured into the [alias_dsn]
                                section of myclirc file.
  --list-dsn                    list of DSN configured into the [alias_dsn]
                                section of myclirc file.
  -R, --prompt TEXT             Prompt format (Default: "\t \u@\h:\d> ").
  -l, --logfile FILENAME        Log every query and its results to a file.
  --defaults-group-suffix TEXT  Read MySQL config groups with the specified
                                suffix.
  --defaults-file PATH          Only read MySQL options from the given file.
  --myclirc PATH                Location of myclirc file.
  --auto-vertical-output        Automatically switch to vertical output mode
                                if the result is wider than the terminal
                                width.
  -t, --table                   Display batch output in table format.
  --csv                         Display batch output in CSV 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 command and quit.
  --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 -p
Connecting to socket /var/run/mysqld/mysqld.sock, owned by user mysql
Password: 
MariaDB 10.6.7
mycli 1.24.3
Home: http://mycli.net
Bug tracker: https://github.com/dbcli/mycli/issues
Thanks to the contributor - Jakub Boukal
MariaDB root@localhost:(none)>

Typing SHOW and space shows valid options

mycli tab completion 01 min

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.

mycli tab completion 02 min

For more information on using and configuring mycli, check the official documentation.

Best Udemy Video Courses to Learn MySQL / MariaDB Databases:

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

Most Popular

Recent Comments