LinuxCommandLibrary

mysqlsh

Interact with MySQL Server instances

TLDR

Start MySQL Shell in interactive mode

$ mysqlsh
copy

Connect to a MySQL server
$ mysqlsh --user [username] --host [hostname] --port [port]
copy

Execute an SQL statement on the server and exit
$ mysqlsh --user [username] --execute '[sql_statement]'
copy

Start MySQL Shell in JavaScript mode
$ mysqlsh --js
copy

Start MySQL Shell in Python mode
$ mysqlsh --py
copy

Import JSON documents into a MySQL collection
$ mysqlsh --import [path/to/file.json] --schema [schema_name] --collection [collection_name]
copy

Enable verbose output
$ mysqlsh --verbose
copy

SYNOPSIS

mysqlsh [OPTIONS] [connection_string | user@host[:port][/database]]

PARAMETERS

--help, -?
    Displays comprehensive help information about the command options.

--version, -V
    Displays the mysqlsh version number.

--host=hostname, -h hostname
    Specifies the MySQL server hostname or IP address to connect to.

--port=port_num, -P port_num
    Specifies the TCP/IP port number for the connection to the MySQL server.

--user=username, -u username
    Connects to the MySQL server as the specified user.

--password[=password], -p[password]
    Prompts for a password if none is given, or uses the provided password for the connection.

--socket=path, -S path
    Connects via the specified Unix domain socket file (primarily for local connections).

--uri=connection_uri
    Specifies the connection details using a comprehensive URI string format (e.g., mysqlx://user:password@host:port/database).

--execute=statement, -e statement
    Executes the given SQL, JavaScript, or Python statement and exits.

--file=path
    Executes commands or scripts from the specified file and exits upon completion.

--sql
    Sets the default language mode to SQL for interactive sessions or executed files.

--javascript, -js
    Sets the default language mode to JavaScript for interactive sessions or executed files.

--python, -py
    Sets the default language mode to Python for interactive sessions or executed files.

--log-level=level
    Sets the verbosity level for logging output (e.g., ERROR, WARNING, INFO, DEBUG).

--json
    Forces result sets to be output in JSON format, overriding the default display format.

--result-format=format
    Specifies the desired output format for result sets (e.g., TABLE, JSON, RAW, TABBED, XML, HTML).

--connect
    Attempts to establish a connection to the server immediately upon mysqlsh startup.

--batch
    Disables interactive mode, making it suitable for scripting and non-interactive environments.

--verbose, -v
    Increases the verbosity of messages displayed during execution, providing more detailed output.

DESCRIPTION

mysqlsh is the official client for MySQL, providing an an interactive command-line interface with advanced capabilities.
It supports three modes: SQL, JavaScript, and Python, allowing users to interact with MySQL servers using their preferred language. Beyond standard SQL querying, mysqlsh integrates the powerful AdminAPI, designed for managing MySQL InnoDB Cluster and InnoDB ReplicaSet, facilitating automated setup, provisioning, and monitoring of high-availability solutions. It also includes utility functions for common administrative tasks like data import/export, schema validation, and server configuration.
Its versatile nature makes it an essential tool for database administrators, developers, and DevOps engineers working with MySQL databases, from simple queries to complex cluster management.

CAVEATS

Security: Avoid specifying passwords directly on the command line (`-ppassword`) as they may be visible in process listings. Prefer being prompted (`-p` without an argument) or using secure configuration files (e.g., ~/.mysqlsh/connections.json or keyring).
Connection URI: If using the `--uri` option, ensure that connection strings containing sensitive information are handled securely and not exposed in logs or history.
AdminAPI Requirements: The advanced AdminAPI features (for InnoDB Cluster and InnoDB ReplicaSet management) have specific MySQL server version and configuration requirements that must be met for proper functionality.

INTERACTIVE MODES

mysqlsh supports three interactive modes: SQL (default for `\sql`), JavaScript (default for `\js`), and Python (default for `\py`). Users can switch between modes using commands like `\sql`, `\js`, `\py` within the shell. Each mode provides language-specific functionalities and helpers, allowing for versatile interaction with MySQL.

ADMINAPI

The shell's AdminAPI is a powerful set of functions for programmatically managing MySQL high-availability solutions. It simplifies operations such as creating, deploying, monitoring, and scaling InnoDB Clusters and ReplicaSets, significantly reducing manual configuration and potential errors in complex setups.

UTILITIES

mysqlsh bundles several built-in utilities for common database tasks, accessible via the `util` global object (e.g., `util.dumpInstance()`, `util.loadDump()`, `util.checkForServerUpgrade()`). These utilities streamline administration, migration, and diagnostic workflows, enhancing productivity for DBAs and developers alike.

HISTORY

MySQL Shell (mysqlsh) was introduced by Oracle with MySQL 8.0. Its primary motivation was to provide a modern, programmatic interface to MySQL, addressing the limitations of the traditional mysql client for advanced administration tasks and integration with scripting languages.
It was designed from the ground up to be the definitive tool for managing MySQL InnoDB Cluster and InnoDB ReplicaSet, making high-availability deployment and management significantly simpler and more robust. Since its inception, it has evolved to include various utility functions, enhanced output formats, and broader language support, becoming the recommended tool for complex MySQL operations.

SEE ALSO

mysql(1), mysqldump(1), mysqladmin(1), mysqlpump(1)

Copied to clipboard