Tunnel Database Connections via SSH
Connect the mariadb-shell MCP server to a MariaDB server that is only reachable through an SSH host, with a mariadb+ssh:// connection URI.
Database servers are often not reachable directly from a developer machine, but only through an SSH host, such as a bastion host in front of a private network. The MCP server can reach such a server through an SSH tunnel, which MariaDB Shell sets up when the MCP server opens a session for the connection.
You request a tunnel with the mariadb+ssh:// scheme in the connection URI. Everything else about the connection, such as the allow-list, the stored password, and the database account, works as for any other connection.
Requirements
MariaDB Shell 26.9.3 or later. The current plugins require a later version already.
SSH access to the SSH host with a key that the MCP server can use without a prompt. See SSH Authentication.
The host key of the SSH host in your known-hosts file. See Host Keys.
A MariaDB account for the MCP server on the database server. See Database Accounts for the MCP Server.
The Model
In a mariadb+ssh:// URI, the user, host, and port always describe the database: the database account, and the database server with its port. The ssh-* options in the query string describe how the server is reached. There are two cases:
mariadb+ssh://dba@remote-host.com:3306
mariadb+ssh://dba@db-01.internal:3306?ssh-host=bastion.example.comWithout
ssh-host, the database runs on the machine you connect to over SSH. The SSH connection goes to the host in the URI, and the tunnel forwards to the loopback address,127.0.0.1, on that machine. Forwarding to the host name instead wouldn't work for a server that only listens on127.0.0.1, which is a common reason to use a tunnel in the first place. This case needs no options at all.With
ssh-host, the database runs on another machine behind the SSH host, such as a bastion host. The SSH connection goes to the host inssh-host, and the tunnel forwards to the host in the URI, as the SSH host resolves it. This case needs one option.
The host in the URI therefore always means the database server, whether ssh-host is given or not. If it meant the SSH host in one case and the database server in the other, the same URI could be read in two ways.
The options ssh-user, ssh-port, ssh-config-file, and ssh-identity-file complete the description of the SSH connection. The SSH password and the passphrase of a key can't be part of a URI. A URI identifies a connection: it names the connection, serves as the key under which its password is stored, and appears in logs and messages. For the same reason, the database password isn't part of the URI either.
Structure of a Tunnel URI
The following URI uses the most common options:
This URI connects to bastion.example.com over SSH as the user tunnel, and forwards the database connection from there to db.internal:3306, where it signs in as mcp. The SSH host resolves db.internal, so you can use host names of the private network.
ssh-host
The SSH host. If you omit it, the database host is also the SSH host, and the tunnel connects to 127.0.0.1 on that machine.
ssh-user
The user on the SSH host. Default: the operating system user that runs MariaDB Shell. A User setting in the SSH configuration file takes precedence over this default.
ssh-port
The SSH port. Default: 22.
ssh-identity-file
The private key file to authenticate with. If you omit it, the default keys and the SSH agent are used.
ssh-config-file
An OpenSSH configuration file to read the settings of the SSH host from.
If you omit the database port, 3306 is used. The ssh-* options are only valid on a mariadb+ssh:// URI; on a mariadb:// URI, MariaDB Shell rejects them.
Examples
A database server in a private network, reached through a bastion host, with a dedicated key:
A database server that only listens on its loopback interface, reached by SSH on the server itself:
An SSH host defined in your OpenSSH configuration, which also provides the user, port, and key:
For connections to MySQL servers, for example as the source of a migration, use mysql+ssh:// in the same way.
SSH Authentication
The MCP server runs without a terminal and can't prompt for anything. It tries the following authentication methods in this order:
Public key authentication, with the key given in
ssh-identity-file, or else with the default keys in~/.sshand the keys in the SSH agent.Password authentication, with a password stored in the MariaDB Shell credential store.
Keyboard-interactive authentication. This method requires a prompt, so it's not available to the MCP server.
In practice, use one of these options:
A dedicated key without a passphrase, used only for the tunnel, and referenced with
ssh-identity-file. Protect the key file with file permissions, and restrict what the key can do on the SSH host, as described in Restrict the Tunnel Key.A key in the SSH agent, on Linux and macOS. Load the key into the agent before you start the harness, for example:
The MCP server runs as a child process of the harness, so the harness must have access to the agent, typically through the
SSH_AUTH_SOCKenvironment variable.
Host Keys
Before it opens a tunnel, MariaDB Shell checks the host key of the SSH host against your known-hosts file, ~/.ssh/known_hosts. Because the MCP server can't ask you to confirm an unknown host key, it refuses the connection in this case:
Add the host key before the agent uses the connection. mcp setup does this for you: when you add a mariadb+ssh:// connection, the setup opens the tunnel to verify the connection, shows the fingerprint of an unknown host key, and asks you to confirm it. If you confirm, it stores the key in the known-hosts file. Compare the fingerprint with the one your administrator provides before you confirm.
Alternatively, connect to the SSH host once with ssh, and accept the host key there:
If the host key changes later, MariaDB Shell always refuses the connection with Invalid fingerprint detected, because a changed key can mean that the connection is being intercepted. Check with your administrator whether the key was changed, then remove the old entry and accept the new key:
Add the Connection
Add the connection with mcp setup, and enter the mariadb+ssh:// URI when the setup asks for a connection URI:
The password that the setup asks for is the password of the database account, not an SSH password. The setup verifies the connection through the tunnel before it stores the password.
You can also pass the URI on the command line. The setup then asks only for the password. Put the URI in single quotes, because the ? and & characters have a special meaning in the shell:
Always enter the password at the prompt. Don't pass it on the command line or in an environment variable. For details, see Adding Database Connections.
Use the Connection
The agent finds the connection with db.list_connections and connects to it with its mariadb+ssh:// URI. For the agent, a tunneled connection works like any other connection. MariaDB Shell sets up the tunnel as part of opening the session, including when the MCP server reopens a session that was closed for being idle.
The scheme is part of the connection. A mariadb:// URI with the same user and host is a different connection, and the MCP server refuses it unless it's also configured. This way, the agent can't bypass the tunnel by accident.
Restrict the Tunnel Key
An SSH key that can open a tunnel can usually also open a shell on the SSH host. If the key is only used for the tunnel, restrict it in the ~/.ssh/authorized_keys file of the tunnel user on the SSH host:
restrict disables everything the key could otherwise do, port-forwarding allows port forwarding again, and permitopen limits the forwarding to the database server. With these options, the key can't open a shell and can't forward connections to other hosts.
The tunnel controls how the agent reaches the server, not what it can do there. Use a dedicated database account with limited privileges as well, as described in Database Accounts for the MCP Server.
Troubleshooting
The authenticity of host '…' can't be established.
The host key of the SSH host isn't in the known-hosts file.
Add the connection with mcp setup and confirm the key, or connect once with ssh.
Invalid fingerprint detected.
The host key of the SSH host has changed.
Verify the new key with your administrator, then remove the old entry with ssh-keygen -R.
Authentication error opening SSH tunnel or Access denied
The SSH host didn't accept any of the keys, or the key needs a passphrase.
Check ssh-user and ssh-identity-file, or load the key into the SSH agent.
Interactive auth mode is disabled when in disabled wizards mode.
The SSH host only offers keyboard-interactive authentication.
Configure public key authentication for the tunnel user.
The connection option '…' requires an SSH tunnel.
An ssh-* option is used on a URI without +ssh.
Use the mariadb+ssh:// scheme.
not a configured connection
The agent used a URI that differs from the configured one, for example mariadb:// instead of mariadb+ssh://.
Use the URI that db.list_connections reports.
This page is licensed: GPLv2
Last updated
Was this helpful?

