LinuxCommandLibrary

mssqlclient.py

Connect to and query Microsoft SQL Server

TLDR

Connect to an MSSQL server using Windows authentication

$ mssqlclient.py -windows-auth [domain]/[username]:[password]@[target]
copy

Connect using SQL server authentication
$ mssqlclient.py [username]:[password]@[target]
copy

Connect using pass-the-hash authentication
$ mssqlclient.py [domain]/[username]@[target] -hashes [LM_Hash]:[NT_Hash]
copy

Connect using Kerberos authentication (requires valid tickets)
$ mssqlclient.py -k [domain]/[username]@[target]
copy

Execute a specific SQL command upon connection
$ mssqlclient.py [username]:[password]@[target] -query "[SELECT user_name();]"
copy

Execute multiple SQL commands from a file
$ mssqlclient.py [username]:[password]@[target] -file [path/to/sql_file.sql]
copy

Connect to a specific database instance (default is None)
$ mssqlclient.py [username]:[password]@[target] -db [database_name]
copy

Display SQL queries before execution
$ mssqlclient.py [username]:[password]@[target] -show
copy

SYNOPSIS

mssql-cli [-S <server>] [-U <username>] [-P <password>] [-d <database>] [-E] [-I] [-Q <query>] [-i <file>] [--prompt <prompt>] [--less-channel-bindings] [--help]

PARAMETERS

-S <server>, --server <server>
    Specifies the SQL Server instance to connect to (e.g., hostname or IP address).

-U <username>, --username <username>
    Provides the username for authentication.

-P <password>, --password <password>
    Provides the password for authentication. Note: Typing passwords on the command line is insecure; consider environment variables or interactive prompts.

-d <database>, --database <database>
    Specifies the initial database to connect to upon login.

-E, --integrated
    Uses Windows Integrated Authentication. On Linux/macOS, this typically implies Kerberos authentication setups.

-I, --no-highlight
    Disables syntax highlighting in the interactive shell.

-Q <query>, --query <query>
    Executes the specified SQL query and then exits the client. Useful for scripting single commands.

-i <file>, --input-file <file>
    Executes SQL commands from the specified input file. Similar to a batch script.

--prompt <prompt>
    Customizes the interactive shell's prompt string.

--less-channel-bindings
    Disables channel bindings during the login process, which can be useful in certain authentication scenarios.

--help
    Displays a help message and exits.

--version
    Shows the version number of mssql-cli and exits.

DESCRIPTION

The `mssqlclient.py` script is the underlying Python engine for `mssql-cli`, a modern, interactive command-line interface designed for Microsoft SQL Server, Azure SQL Database, and Azure Synapse Analytics. It significantly enhances the traditional command-line experience by offering intelligent auto-completion for SQL keywords, database objects (like tables, views, columns, and stored procedures), and context-aware suggestions. Users benefit from immediate feedback through syntax highlighting, making complex queries more readable. Additional features include a persistent command history, support for common key bindings (`vim`/`emacs`), and the ability to execute queries from files or directly from the command line. Developed with cross-platform compatibility in mind, `mssql-cli` provides an efficient and user-friendly way to interact with SQL Server instances from Linux, macOS, and Windows terminals, streamlining database administration and development workflows.

CAVEATS

While `mssqlclient.py` is the core script, users typically interact with the `mssql-cli` wrapper command.
Connectivity on Linux and macOS requires the installation of appropriate ODBC drivers (e.g., `unixODBC` and `msodbcsql17`), which are not bundled with `mssql-cli` and must be set up separately.
Inputting sensitive information like passwords directly on the command line is generally discouraged due to security risks.

INSTALLATION

`mssql-cli` (and thus `mssqlclient.py`) is primarily installed via Python's package installer, `pip`. The recommended command is: pip install mssql-cli. Ensure Python 3.7+ is installed. Proper ODBC driver installation is also crucial for successful database connections on non-Windows systems.

INTERACTIVE MODE

When `mssql-cli` is run without the -Q or -i options, it enters an interactive shell. In this mode, users can type SQL queries and commands directly, benefiting from real-time auto-completion, syntax highlighting, and command history.

CONFIGURATION

`mssql-cli` supports a configuration file (often located at `~/.config/mssql-cli/config`) for persistent customization of settings such as prompt string, key bindings, and display options, allowing users to tailor their experience.

HISTORY

`mssql-cli` was developed by Microsoft as part of its commitment to open-source tools and cross-platform compatibility for SQL Server. Released initially around 2017-2018, it aimed to provide a modern, user-friendly command-line experience similar to popular tools like `pgcli` (for PostgreSQL) and `mycli` (for MySQL). It quickly gained traction for its enhanced features over the traditional `sqlcmd`, especially for users operating in Linux and macOS environments, leveraging Python and libraries like `prompt_toolkit` for its interactive capabilities.

SEE ALSO

sqlcmd(1), psql(1), mysql(1), isql(1)

Copied to clipboard