Sunday 2 November 2014

Connect to Remote MySQL server by ssh tunnel

Works between different  OS (Windows, Linux), 
1.  ssh -L 3306: -p 27628 -lroot
( - remote ssh  ip ,  22 - remote port of ssh, root - user of ssh )
Enter pass to key file, if need;
Pass to remote ssh 
and use as localhost connection! 

Create the SSH tunnel manually

The syntax for creating the SSH tunnel is ssh -L [local port]:[database host]:[remote port] \
[username]@[remote host]
  • [local port] The local port your database tool connects to.
    If you have a MySQL installation on your local machine, it runs on port 3306 by default; therefore, don’t use 3306 for the local port. For example, use 3307 instead.
    If you have a PostgreSQL installation on your local machine, it runs on port 5432 by default; therefore, don’t use 5432 for the local port. For example, use 5433 instead.
  • [database host] The hostname or IP address of the database instance that you are tunneling to. If the [remote host] is the database instance you will want to set this to (so it refers to itself). If you used an application instance as [remote host] then you can use the value of "host:" from your database.yml instead.
  • [remote port] The port that your remote database listens for connections on.
    For MySQL databases, this is 3306 by default.
    For PostgreSQL database, this is 5432 by default.
  • [username] the user for the database instance. The default user for the Engine Yard Cloud database is deploy.
  • [remote host] the remote instance your tunnel will connect to the database through. This can be the database instance itself, or any instance within the database environment.
The tunneling command opens an SSH session with the [remote host] specified. The tunnel will work as long as that SSH session is active. If the session window is consuming valuable workspace we recommend minimizing it as running tunnels in the background can lead to multiple tunnels and port conflicts.

To create and test the SSH tunnel for a MySQL database

  1. In a terminal window on your local machine, type:
    ssh -L \
    [email protected] 
    3307 is the local port, is the database host,
    3306 is the listening port, 
    deploy is the database username, and is the remote host.
  2. Before connecting the external database tool such as MySQL Workbench or SQLyog, test the connection with a simple tool such as the database console, mysql.
    (Your tunnel needs to be running for this test.)
    mysql -udeploy -p -P 3307 -h 
    deploy is the database username on the remote host and 
    3307 is the local port.
    You are prompted for your database password.
    Note: for the -h argument, it is necessary to use instead of localhost.

No comments:

Post a Comment