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.
[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.
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
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.
Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.
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.
railsでWebアプリケーションを作成しているのですが、rails sでサーバーを起動した際に、Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) のエラーが出てしまいます
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 - 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...