How to Connect to MySQL through SSH Tunnel

How to Connect to MySQL through SSH Tunnel

In this guide, we will explain how to create an SSH tunnel and connect to the MySQL server from remote clients.The same instructions apply for MariaDB.

By default, the MySQL server listens only on localhost, which means it can be accessed only by applications running on the same host.

However, in some situations, you might want to connect to the server from remote locations. One option would be to configure the MySQL server to allow remote connections, but that requires administrative privileges, and it may cause security risks.

A more secure alternative would be to create an SSH tunnel from the local system to the server. SSH tunneling is a method of creating an encrypted SSH connection between a client and a server machine through which services ports can be relayed.

In this guide, we will explain how to create an SSH tunnel and connect to the MySQL server from remote clients. The same instructions apply for MariaDB.

Prerequisites

  • SSH Client.
  • SSH access to the system on which the MySQL server runs.

Create an SSH Tunnel on Linux and macOS

The [ssh](https://linuxize.com/post/ssh-command-in-linux/) client is preinstalled on most Linux and Unix-based systems.

If you are using Linux or macOS as your operating system, you can create an SSH tunnel using the following command:

ssh -N -L 3336:127.0.0.1:3306 [USER]@[SERVER_IP]

The options used are as follows:

  • -N - Tells SSH not to execute a remote command.
  • -L 3336:127.0.0.1:3306 - Creates a local port forwarding. The local port (3306), the destination IP (127.0.0.1) and the remote port (3306) are separated with a colon (:).
  • [USER]@[SERVER_IP] - The remote SSH user and server IP address.
  • To run the command in the background, use the -f option.
  • If the SSH server is listening on a port other than 22 (the default) specify the port with the -p [PORT_NUMBER] option.

Once you run the command, you’ll be prompted to enter your SSH user password. After entering it, you will be logged in to the server, and the SSH tunnel will be established. It is a good idea to set up an SSH key-based authentication and connect to the server without entering a password.

Now you can point your local machine MySQL client to 127.0.0.1:3336 enter the remote database login credentials and access the MySQL server.

For example, to connect to the MySQL server using the command line mysql client you would issue:

mysql -u MYSQL_USER -p -h 127.0.0.1

Where MYSQL_USER is the remote MySQL user having privileges to access the database.

When prompted, enter the MySQL user password.

To terminate the SSH tunnel type CTRL+C in the console where the ssh client is running.

mysql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Exploring MySQL Binlog Server - Ripple

How MySQL Ripple binlog server can help you improve the performance of your master server by introducing a proxy server between the master and slave servers.MySQL does not limit the number of slaves that you can connect to the master server in a replication topology.

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)の解決

railsでWebアプリケーションを作成しているのですが、rails sでサーバーを起動した際に、Can't connect to local MySQL server through socket  '/var/lib/mysql/mysql.sock' (2) のエラーが出てしまいます

MySQL Tutorial - Learn MySQL from Scratch

Complete MySQL Course: Beginner to Advanced. MySQL Tutorial - Learn MySQL from scratch and go from beginner to advanced in MySQL.

MySQL Tutorial: A Beginners Guide To Learn MySQL

MySQL Tutorial: A Beginners Guide To Learn MySQL - Let's take a look at a very in-depth MySQL tutorial that explores all the operations and commands that you need to explore your databases...